Message类

消息类中,有两个固定属性:

  • attr:消息属性,即消息的来源属性
    • system:系统消息
    • self:自己发送的消息
    • friend:好友消息
    • other:其他消息
  • type:消息类型,即消息的内容属性
    • time:时间消息
    • text:文本消息
    • quote:引用消息
    • voice:语音消息
    • image:图片消息
    • video:视频消息
    • file:文件消息
    • location:位置消息
    • link:链接消息
    • emotion:表情消息
    • merge:合并转发消息
    • personal_card:个人名片消息
    • note: 笔记消息
    • other:其他消息

selffriend又可以跟消息类型所组合,所以所有消息类别如下:

自己发送的消息SelfMessage对方发来的消息FriendMessage
文本消息TextMessageSelfTextMessageFriendTextMessage
引用消息QuoteMessageSelfQuoteMessageFriendQuoteMessage
语音消息VoiceMessageSelfVoiceMessageFriendVoiceMessage
图片消息ImageMessageSelfImageMessageFriendImageMessage
视频消息VideoMessageSelfVideoMessageFriendVideoMessage
文件消息FileMessageSelfFileMessageFriendFileMessage
✨位置消息LocationMessageSelfLocationMessageFriendLocationMessage
✨链接消息LinkMessageSelfLinkMessageFriendLinkMessage
✨表情消息EmotionMessageSelfEmotionMessageFriendEmotionMessage
✨合并消息MergeMessageSelfMergeMessageFriendMergeMessage
✨名片消息PersonalCardMessageSelfPersonalCardMessageFriendPersonalCardMessage
✨笔记消息NoteMessageSelfNoteMessageFriendNoteMessage
其他消息OtherMessageSelfOtherMessageFriendOtherMessage

简单的使用示例:

from wxautox4.msgs import *

... # 省略获取消息对象的过程

# 假设你获取到了一个消息对象
msg = ...

# 当消息为好友消息时,回复收到
# 方法一:
if msg.attr == 'friend':
    msg.reply('收到')

# 方法二:
if isinstance(msg, FriendMessage):
    msg.reply('收到')

Message

消息基类,所有消息类型都继承自该类

属性(所有消息类型都包含以下属性):

属性名类型描述
typestr消息内容类型
attrstr消息来源类型
infoDict消息的详细信息
idstr消息UI ID(不重复,切换UI后会变)
✨hashstr消息hash值(可能重复,切换UI后不变)
senderstr消息发送者
contentstr消息内容

chat_info

获取该消息所属聊天窗口的信息

chat_info = msg.chat_info()

返回值

  • 类型:dict
  • 描述:聊天窗口信息
  • 返回值示例:
# 好友
{'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': '肯德基'}

返回值

  • 类型:List[str]

roll_into_view

将消息滚动到视野内

msg.roll_into_view()

SystemMessage

系统消息,没有特殊用法

固定属性:

属性名类型属性值描述
attrstrsystem消息属性

TimeMessage

时间消息,继承系统消息

固定属性:

属性名类型属性值描述
attrstrsystem消息属性
typestrtime消息类别
timestryyyy-hh-mm HH:MM:SS时间,yyyy-hh-mm HH:MM:SS
from wxautox4.msgs import TimeMessage

msg = ...
if isinstance(msg, TimeMessage):
    print(msg.time)
    # 2026-01-01 12:30:00

HumanMessage

人发送的消息,即自己或好友、群友发送的消息

固定属性:

属性名类型属性值描述
attrstrfriend消息属性

特有属性:

属性类型描述
sender_remarkstr群消息中,该消息发送人自己设置的“我在本群的昵称”

click

点击该消息,一般特殊消息才会有作用,比如图片消息、视频消息等

msg.click()

select_option

右键该消息,弹出右键菜单,并选择指定选项

msg.select_option("复制")

返回值

quote

引用该消息,并回复

msg.quote("回复内容")

参数

参数名类型默认值描述
textstr引用内容
atUnion[List[str], str]@用户列表
timeoutint3超时时间,单位为秒

返回值

forward

转发该消息

# 开源版
msg.forward("张三")

# ✨附带消息message参数仅plus版本有效
msg.forward("张三", message="转发会议材料给你,请查收")

参数

参数名类型默认值描述
targetsUnion[List[str], str]转发对象名称
✨messagestrNone要附加的消息(仅✨Plus版本支持该参数)
timeoutint3超时时间,单位为秒

返回值

✨tickle

拍一拍该消息发送人

msg.tickle()

返回值

FriendMessage

好友、群友发送的消息,即聊天页面中,左侧人员发送的消息。继承自HumanMessage

✨sender_info

获取发送人信息

msg.sender_info()

返回值

  • 类型:Dict[str, str]

SelfMessage

自己发送的消息,即聊天页面中,右侧自己发送的消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
attrstrself消息属性

TextMessage

文本消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestrtext消息属性

QuoteMessage

引用消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestrquote消息属性

特有属性:

属性名类型属性值描述
quote_contentstr被引用消息内容被引用消息内容
quote_nicknamestr被引用消息发送人昵称被引用消息发送人昵称

download_quote_image

下载引用消息中的图片或视频

msg.download_quote_image()

参数

参数名类型默认值描述
dir_pathUnion[str, Path]None下载目录,不填则默认WxParam.DEFAULT_SAVE_PATH
timeoutint10下载超时时间,单位为秒

返回值

  • Path: 文件路径,成功时返回该类型
  • None: 引用内容不是图片或视频时返回该类型

说明

仅当引用内容为图片或视频时可下载,其他类型返回 None

ImageMessage

图片消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestrimage消息属性

download

下载图片,返回图片路径

msg.download()

参数

参数名类型默认值描述
dir_pathUnion[str, Path]None下载图片的目录,不填则默认WxParam.DEFAULT_SAVE_PATH
originalboolFalse是否下载原图片,默认否

返回值

  • Path: 图片路径,成功时返回该类型
  • WxResponse: 下载结果,失败时返回该类型

VideoMessage

视频消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestrvideo消息属性

download

下载视频,返回视频路径

msg.download()

参数

参数名类型默认值描述
dir_pathUnion[str, Path]None下载视频的目录,不填则默认WxParam.DEFAULT_SAVE_PATH
originalboolFalse是否下载原视频,默认否
timeoutint10下载超时时间

返回值

  • Path: 视频路径,成功时返回该类型
  • WxResponse: 下载结果,失败时返回该类型

VoiceMessage

语音消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestrvoice消息属性

to_text

将语音消息转换为文本,返回文本内容

msg.to_text()

FileMessage

文件消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestrfile消息属性

download

下载文件,返回文件路径

msg.download()

参数

参数名类型默认值描述
dir_pathUnion[str, Path]None下载文件的目录,不填则默认WxParam.DEFAULT_SAVE_PATH
force_clickboolFalse是否强制点击文件消息(当自动下载不可用时指定,否则会打开该文件)
timeoutint10下载超时时间

返回值

  • Path: 文件路径,成功时返回该类型
  • WxResponse: 下载结果,失败时返回该类型

✨LocationMessage

位置消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestrlocation消息属性

✨LinkMessage

链接消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestrlink消息属性

✨get_url

获取链接地址

msg.get_url()
参数名类型默认值描述
timeoutint10下载超时时间

返回值

  • str: 链接地址

✨EmotionMessage

表情消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestremotion消息属性

✨MergeMessage

合并消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestrmerge消息属性

✨PersonalCardMessage

名片消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestrpersonal_card消息属性

✨add_friend

添加好友

msg.add_friend()
参数名类型默认值描述
addmsgstrNone添加好友时的附加消息
remarkstrNone添加好友后的备注
tagsList[str]None添加好友后的标签
permissionLiteral['朋友圈', '仅聊天']'朋友圈'添加好友后的权限
timeoutint3搜索好友的超时时间

返回值

✨NoteMessage

笔记消息。继承自HumanMessage

固定属性:

属性名类型属性值描述
typestrnote消息属性

✨get_content

获取笔记内容

from pathlib import Path

note_content_list = msg.get_content()
for content in note_content_list:
    if isinstance(content, str):
        # 文本内容
        print(content)
    elif isinstance(content, Path):
        # 文件路径,文件、视频、图片等
        print('文件路径:', content)

✨save_files

保存笔记中的文件

msg.save_files()
参数名类型默认值描述
dir_pathUnion[str, Path]None保存路径

返回值

  • WxResponse: 是否保存成功,若成功则data为保存的文件路径列表

✨to_markdown

将笔记转换为Markdown格式

msg.to_markdown()
参数名类型默认值描述
dir_pathUnion[str, Path]None保存路径

返回值

  • Path: markdown文件路径

OtherMessage

其他暂未支持解析的消息类型