# Chat类 - chatautox

# Chat类

## Chat 类属性

在了解`Chat`类的方法之前，我想先介绍一下为什么要做这个类。
`chatautox(x)`这个项目的原理是模拟人工对微信客户端的操作，拿取到的所有信息都是人眼可见的部分，
所以当我们想监听某个人或群消息的时候，需要把这个人的聊天窗口独立出来，以确保UI元素不会因为微信主窗口切换聊天而丢失，
同时也不需要每来一条信息都切换聊天窗口去获取。
所以，`Chat`类就是用来创建一个独立的聊天窗口，并获取这个聊天窗口的信息。

| 属性         | 类型  | 描述          |
| ---------- | --- | ----------- |
| who        | str | 当前子窗口的聊天对象名 |
| chat\_type | str | 聊天窗口类型      |

### 聊天窗口类型 chat\_type

获取当前聊天窗口的类型，返回值为字符串，取值范围如下：

- friend：好友
- group：群聊
- service：客服
- official：公众号

```text
chat_type = chat.chat_type
```

## Chat 类方法

### 显示窗口 Show

```text
chat.Show()
```

### 获取聊天窗口信息 ChatInfo

```text
info = chat.ChatInfo()
```

**返回值**：

- 类型：`dict`
- 描述：聊天窗口信息
- 返回值示例：

```text
# 好友
{'chat_type': 'friend', 'chat_name': '张三'}

# 群聊
{'group_member_count': 500, 'chat_type': 'group', 'chat_name': '工作群'}

# 客服
{'company': '@肯德基', 'chat_type': 'service', 'chat_name': '店长xxx'}

# 公众号
{'chat_type': 'official', 'chat_name': '肯德基'}
```

### ✨@所有人 AtAll

```python
group = '工作群'
content = """
通知：
下午xxxx
xxxx
"""

wx.AtAll(content, group)
```

msg (str): 发送的消息

​ who (str, optional): 发送给谁. Defaults to None.

​ exact (bool, optional): 是否精确匹配. Defaults to False.

**参数**：

| 参数    | 类型   | 默认值   | 描述     |
| ----- | ---- | ----- | ------ |
| msg   | str  | None  | 发送的消息  |
| who   | str  | None  | 发送给谁   |
| exact | bool | False | 是否精确匹配 |

**返回值**：

- 类型：[`WxResponse`](/doc-site/docs/class/other/index.md#wxresponse)
- 描述：是否发送成功

### 发送消息 SendMsg

```python
wx.SendMsg(msg="你好", who="张三", clear=True, at="李四", exact=False)
```

**参数**：

| 参数    | 类型                      | 默认值   | 描述                                 |
| ----- | ----------------------- | ----- | ---------------------------------- |
| msg   | str                     | 必填    | 消息内容                               |
| who   | str                     | None  | 发送对象，不指定则发送给当前聊天对象，**当子窗口时，该参数无效** |
| clear | bool                    | True  | 发送后是否清空编辑框                         |
| at    | Union\[str, List\[str]] | None  | @对象，不指定则不@任何人                      |
| exact | bool                    | False | 搜索who好友时是否精确匹配，**当子窗口时，该参数无效**     |

**返回值**：

- 类型：[`WxResponse`](/doc-site/docs/class/other/index.md#wxresponse)
- 描述：是否发送成功

### 发送文件 SendFiles

```python
wx.SendFiles(filepath="C:/文件.txt", who="张三", exact=False)
```

**参数**：

| 参数       | 类型   | 默认值   | 描述                                 |            |
| -------- | ---- | ----- | ---------------------------------- | ---------- |
| filepath | str  | list  | 必填                                 | 要复制文件的绝对路径 |
| who      | str  | None  | 发送对象，不指定则发送给当前聊天对象，**当子窗口时，该参数无效** |            |
| exact    | bool | False | 搜索who好友时是否精确匹配，**当子窗口时，该参数无效**     |            |

**返回值**：

- 类型：[`WxResponse`](/doc-site/docs/class/other/index.md#wxresponse)
- 描述：是否发送成功

### 获取当前聊天窗口的所有消息 GetAllMessage

```text
messages = wx.GetAllMessage()
```

**返回值**：

- 类型：List\[[Message](#message-%E7%B1%BB%E6%96%B9%E6%B3%95)]
- 描述：当前聊天窗口的所有消息

### ✨添加群成员 AddGroupMembers

```python
wx.ChatWith('工作群')
wx.AddGroupMembers(members=["张三", "李四"])
```

**参数**：

| 参数      | 类型                      | 默认值  | 描述        |
| ------- | ----------------------- | ---- | --------- |
| members | Union\[str, List\[str]] | None | 成员名或成员名列表 |

**返回值**：

- 类型：[`WxResponse`](/doc-site/docs/class/other/index.md#wxresponse)
- 描述：是否添加成功

注意

该方法与3.9版本参数做出了一些变动，3.9支持传入`group`参数来切换，现在改为配合`ChatWith`方法来切换后再添加

### ✨修改群聊名称 SetGroupName

```text
chat.SetGroupName(value="新群名")
```

**参数**：

| 参数    | 类型  | 默认值 | 描述     |
| ----- | --- | --- | ------ |
| value | str | 必填  | 新的群聊名称 |

**返回值**：

- 类型：[`WxResponse`](/doc-site/docs/class/other/index.md#wxresponse)
- 描述：是否修改成功

### ✨修改群聊备注 SetGroupRemark

```text
chat.SetGroupRemark(value="群备注")
```

**参数**：

| 参数    | 类型  | 默认值 | 描述     |
| ----- | --- | --- | ------ |
| value | str | 必填  | 新的群聊备注 |

**返回值**：

- 类型：[`WxResponse`](/doc-site/docs/class/other/index.md#wxresponse)
- 描述：是否修改成功

### ✨修改群公告 SetGroupAnnouncement

```text
chat.SetGroupAnnouncement(value="群公告内容")
```

**参数**：

| 参数    | 类型  | 默认值 | 描述      |
| ----- | --- | --- | ------- |
| value | str | 必填  | 新的群公告内容 |

**返回值**：

- 类型：[`WxResponse`](/doc-site/docs/class/other/index.md#wxresponse)
- 描述：是否修改成功

### ✨修改我在群里的昵称 SetGroupMyNickname

```text
chat.SetGroupMyNickname(value="我的群昵称")
```

**参数**：

| 参数    | 类型  | 默认值 | 描述     |
| ----- | --- | --- | ------ |
| value | str | 必填  | 新的群内昵称 |

**返回值**：

- 类型：[`WxResponse`](/doc-site/docs/class/other/index.md#wxresponse)
- 描述：是否修改成功

### 关闭窗口 Close

```text
wx.Close()
```

<div class="doc-page-nav"><a class="doc-page-nav__prev" href="/docs/class/wechat">上一页 WeChat类</a><a class="doc-page-nav__next" href="/docs/class/message">下一页 Message类</a></div>
