Message类

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

  • attr:消息属性,即消息的来源属性
    • system:系统消息
    • time:时间消息
    • tickle:拍一拍消息
    • self:自己发送的消息
    • friend:好友消息
    • other:其他消息
  • type:消息类型,即消息的内容属性
    • 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 wxautox.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': '肯德基'} 

✨ get_all_text

获取消息中所有文本内容

text_list = msg.get_all_text()

返回值

  • 类型:List[str]

roll_into_view

将消息滚动到视野内

msg.roll_into_view()

SystemMessage

系统消息,没有特殊用法

固定属性:

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

TickleMessage

拍一拍消息,继承自SystemMessage

固定属性:

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

特有属性:

属性类型描述
tickle_liststr拍一拍消息列表

TimeMessage

时间消息

固定属性:

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

特有属性:

属性类型描述
timestr时间 YYYY-MM-DD HH:MM:SS

HumanMessage

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

固定属性:

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

特有属性:

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

click

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

msg.click()

select_option

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

msg.select_option("复制")

返回值

quote

引用该消息,并回复

msg.quote("回复内容")

参数

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

返回值

forward

转发该消息

msg.forward("转发对象名称")

参数

参数名类型默认值描述
targetsUnion[List[str], str]转发对象名称
timeoutint3超时时间,单位为秒

返回值

✨tickle

拍一拍该消息发送人

msg.tickle()

返回值

✨delete

删除该消息

msg.delete()

返回值

✨download_head_image

下载该消息发送人的头像

msg.download_head_image()

返回值

  • 类型:Path
  • 描述:下载路径Path对象

FriendMessage

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

✨sender_info

获取发送人信息

msg.sender_info()

返回值

  • 类型:Dict[str, str]

✨at

@该消息发送人

msg.at('xxxxxx')

参数

参数名类型默认值描述
contentstr必填要发送的内容
quoteboolFalse是否引用该消息

返回值

✨add_friend

添加该消息的发送人为好友

msg.add_friend()

参数

参数名类型默认值描述
addmsgstrNone添加好友时的附加消息,默认为None
remarkstrNone添加好友后的备注,默认为None
tagslistNone添加好友后的标签,默认为None
permissionLiteral[‘朋友圈’, ‘仅聊天’]‘朋友圈’添加好友后的权限,默认为’朋友圈’
timeoutint3搜索好友的超时时间,默认为3秒

返回值

✨multi_select

多选该消息,仅作合并转发使用,如果不进行合并转发,请勿调用该方法

msg.multi_select()

参数:无

返回值:无

SelfMessage

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

固定属性:

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

TextMessage

文本消息。继承自HumanMessage

固定属性:

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

QuoteMessage

引用消息。继承自HumanMessage

固定属性:

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

特有属性:

属性名类型属性值描述
quote_contentstr引用消息内容引用消息内容

✨download_quote_image

下载引用消息中的图片,返回图片路径

msg.download_quote_image()

参数

参数名类型默认值描述
dir_pathstrNone下载路径,默认为None
timeoutint10超时时间,默认为10秒

返回值: Path: 视频路径,成功时返回该类型

✨click_quote

点击引用框体

msg.click_quote()

参数:无

返回值:无

ImageMessage

图片消息。继承自HumanMessage

固定属性:

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

download

下载图片,返回图片路径

msg.download()

参数

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

返回值

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

VideoMessage

视频消息。继承自HumanMessage

固定属性:

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

download

下载视频,返回视频路径

msg.download()

参数

参数名类型默认值描述
dir_pathUnion[str, Path]None下载视频的目录,不填则默认WxParam.DEFAULT_SAVE_PATH
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消息属性

特有属性:

属性名类型属性值描述
✨addressstr地址信息该消息卡片的地址信息

✨LinkMessage

链接消息。继承自HumanMessage

固定属性:

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

✨get_url

获取链接地址

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

返回值

  • str: 链接地址

✨EmotionMessage

表情消息。继承自HumanMessage

固定属性:

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

✨MergeMessage

合并消息。继承自HumanMessage

固定属性:

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

✨get_messages

获取合并消息中的所有消息

msg.get_messages()

返回值

  • List[str]: 合并消息中的所有消息

✨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 isintance(content, str):
        # 文本内容
        print(content)
    elif isintance(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

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