Message类 - chatautox

Message类

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

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

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

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

简单的使用示例:

from chatautox.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 chatautox.msgs import TimeMessage

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

HumanMessage

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

固定属性:

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

特有属性:

属性类型描述
senderstr群消息中,该消息发送人显示的名称

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要附加的消息
timeoutint3超时时间,单位为秒

返回值

tickle

拍一拍该消息发送人

msg.tickle()

返回值

✨download_head_image

下载该消息发送人的头像

msg.download_head_image()

✨edit_info

编辑该消息发送人的备注和标签

msg.edit_info(add_tags=['同事', '北京'], remove_tags=['老同学'], remark='张三(北京)')

参数

参数名类型默认值描述
add_tagsList[str]None要添加的标签列表
remove_tagsList[str]None要移除的标签列表
remarkstrNone要设置的备注,传入空字符串可清除备注

返回值

说明

add_tagsremove_tagsremark 三个参数不能同时为 None,否则返回失败

FriendMessage

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

sender_info

获取发送人信息

msg.sender_info()

返回值

  • 类型:Dict[str, str]

✨delete_friend

删除该消息发送人(联系人)

msg.delete_friend()

# 删除联系人但不清除聊天记录
msg.delete_friend(clear=False)

参数

参数名类型默认值描述
clearboolTrue是否同时清除聊天记录

返回值

✨add_friend

添加该消息发送人为好友(适用于群聊中尚未添加的成员)

msg.add_friend(addmsg='你好,我是xxx', remark='张三', tags=['同事'], permission='朋友圈')

参数

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

返回值

自己发送的消息,即聊天页面中,右侧自己发送的消息。继承自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
  • Plus 版功能(方法名前缀 ✨),需激活会员后可用

✨download_quote_file

下载引用消息中的文件

msg.download_quote_file()

参数

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

返回值

  • Path: 文件路径,成功时返回该类型
  • None: 引用内容不是文件时返回该类型
  • WxResponse: 下载失败时返回该类型

说明

  • 通过右键「定位到原文位置」找到原文件气泡后下载,完成后自动回到引用位置
  • 仅当引用内容为文件时可下载(如 [文件]xxx.pdf),图片/视频请使用 download_quote_image
  • Plus 版功能(方法名前缀 ✨),需激活会员后可用

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消息属性

✨get_messages

获取合并转发中的纯内容列表(文本字符串或媒体文件路径)。会自动打开聊天记录窗口、滚动列表,图片/视频会尝试保存到本地。

items = msg.get_messages()
for item in items:
    print(item)

返回值

  • List[str]:内容列表(路径为 / 分隔的字符串)

✨get_messages_detailed

获取含发送者的明细列表,格式为 [[sender, content, None], ...]content 为文本或 Path 对象。

for sender, content, _ in msg.get_messages_detailed():
    print(sender, content)

✨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文件路径

✨create_note

通过收藏页创建新笔记;forward_to 有值时再转发给指定联系人。流程与录制脚本一致:依次绑定微信主窗口笔记编辑窗 →(可选)微信发送给 → 再回到微信主窗口。

WeChat 上可直接调用(无需聊天中的笔记消息);NoteMessage 上用法相同,详见参数说明。

from chatautox import WeChat

wx = WeChat()
wx.create_note('测试', '123')

# 不转发:不传 forward_to,或传 None / 空字符串 / 空列表
wx.create_note('测试')
wx.create_note('测试', None)
wx.create_note('测试', '')

# NoteMessage 实例
msg.create_note('测试', '123')

content 支持纯文本,或与 get_content() 类似的混合列表(按顺序逐项写入剪贴板并粘贴:文本用 Unicode,图片用 CF_DIB,文件/视频用 CF_HDROP):

# 默认保留收藏中的笔记(delete_from_favorites=False)
wx.create_note('测试', '123')
wx.create_note('测试', '123', delete_from_favorites=True)  # 转发后从收藏删除

# 文本 + 本地图片/文件 + http 链接
wx.create_note([
    '标题说明',
    r'D:\images\photo.jpg',
    r'D:\videos\clip.mp4',
    {'type': 'text', 'content': '备注'},
    {'type': 'image', 'content': r'D:\a.png'},
    'https://example.com/pic.jpg',  # 自动下载到临时文件后粘贴
], '123')
参数名类型默认值描述
contentUnion[str, List[Union[str, Path, dict]]]笔记正文:字符串,或混合列表(文本 / Path / 本地路径 / http(s) 链接 / {'type':'text'|'image'|'file','content':...}
forward_toOptional[Union[str, List[str]]]None转发目标联系人(备注名/昵称);不传、None 或空则不转发,笔记保留在收藏中
delete_from_favoritesboolFalse转发后是否从收藏中删除该笔记(未转发时无效)
category_indexint1收藏分类列表项序号(从 1 起)
restore_indexint2「恢复」按钮序号(从 1 起)
forward_item_indexint2待转发收藏项序号(从 1 起)
cleanup_item_indexint2删除时收藏项序号(从 1 起)
save_waitfloat3.0笔记 Ctrl+S 保存后等待秒数

返回值

  • WxResponse: 是否创建成功(含转发时是否转发成功)

✨MiniAppMessage

小程序消息(聊天卡片中含「小程序」字样的链接消息)。继承自LinkMessage

NoteMessage(微信原生笔记)不同:小程序 typeminiapp,需先用 identify_kind() 判断是否支持,再调用 get_content(),二者互不冲突。

固定属性:

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

content 为卡片摘要文本(如「小程序商品笔记 …」),用于识别具体小程序类型。

支持的小程序

chatautox 内置自动解析以下 3 类(其它小程序 identify_kind() 返回 None,不会误解析):

identify_kind() 返回值小程序名称消息摘要关键词(任一命中)
weishang_album微商相册、微购相册、种草相册Lite微商相册微购相册种草相册Lite小程序微商相册
product_note_pro产品笔记pro、私域产品笔记产品笔记私域产品笔记小程序产品笔记小程序私域产品笔记
product_note商品笔记商品笔记小程序商品笔记(且未被识别为 pro)

识别优先级:微商相册 → 产品笔记pro → 商品笔记

✨identify_kind

判断当前消息是否为内置支持的小程序,并返回类型标识。

kind = msg.identify_kind()
# 'weishang_album' | 'product_note_pro' | 'product_note' | None

返回值

  • str:内置支持的小程序类型
  • None:未内置(仅可读取 msg.content 摘要)

✨get_content

点击小程序卡片,打开对应小程序窗口并解析内容(文本 + 本地图片/视频路径)。

from chatautox.param import WxResponse
from pathlib import Path

if not msg.identify_kind():
    print('未内置支持:', msg.content)
else:
    result = msg.get_content(wait=3)
    if isinstance(result, WxResponse):
        print('解析失败:', result.get('message'))
    else:
        for item in result:
            if isinstance(item, str):
                print('文本:', item)
            elif isinstance(item, Path):
                print('文件:', item)
参数名类型默认值描述
waitfloat3点击卡片后等待小程序窗口打开的秒数

返回值

  • List[Union[str, Path]]:有序内容列表,成功时返回
  • WxResponse:不支持的小程序、解析失败或内容为空时返回

说明

  • get_content() 内部会先 click() 打开小程序窗口,再等待 wait 秒后开始解析,无需在调用前单独 msg.click()
  • 调用前建议先用 identify_kind() 判断,避免对未支持小程序无效点击
  • 解析耗时较长,监听回调中建议单独处理,避免阻塞其它消息

OtherMessage

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

上一页Chat类下一页朋友圈