itchat中文文档
def login(self, enableCmdQR=False, picDir=None, qrCallback=None,
loginCallback=None, exitCallback=None):
''' 像web版微信一样登陆
用于登陆
- 将下载并打开一个二维码
- 然后记录扫码状态,程序暂停等待你微信确认登陆
- 最终微信将登陆成功并展示你的昵称
可选参数
- enableCmdQR: 在命令行展示二维码,默认不开启
- 整数可以适应奇怪的字符长度
- picDir: 存储二维码的路径,默认代码同级目录
- qrCallback: 二维码回调,可接受参数 uuid, status, qrcode
- loginCallback: 登陆回调,登陆成功之后的回调
- 如果没有设置,将清除屏幕并删除二维码
- exitCallback: 退出后的回调
- 它包含退出调用
示例
#python代码
import itchat
itchat.login()
它被定义在 components/login.py
当然,登陆的每一步都可以外部调用
- 你可以扫描源代码看看
- 根据你自己的需要修改
'''
raise NotImplementedError()
def get_QRuuid(self):
''' 获取 uuid 供二维码使用
uuid 是二维码的象征符号
- 为了登陆,你需要首先获得 uuid
- 下载二维码时,你应该将 uuid 传给它
- 检查登陆状态时, uuid 也是必须的参数
如果 uuid 超时,只能重新获取
它被定义在 components/login.py
'''
raise NotImplementedError()
def get_QR(self, uuid=None, enableCmdQR=False, picDir=None, qrCallback=None):
''' 下载并展示二维码
可选参数
- uuid: 如果没有设置 uuid,将使用你最新获取的 uuid
- enableCmdQR: 是否在命令行展示二维码
- picDir: 二维码保存路径
- qrCallback: 二维码回调,可接受参数 uuid, status, qrcode
它被定义在 components/login.py
'''
raise NotImplementedError()
def check_login(self, uuid=None):
''' 检查登陆状态
可选参数:
- uuid: 如果没有设置 uuid,将使用你最新获取的 uuid
返回值:
- 返回类型:string
- 返回值的含义
- 200: 登陆成功
- 201: 等待登陆确认
- 408: uuid 超时
- 0 : unknown error
作用:
- 设置 syncUrl and fileUrl
- 设置 BaseRequest
阻塞直到以上参数被设置完成
它被定义在 components/login.py
'''
raise NotImplementedError()
def web_init(self):
''' 获取初始化所需信息
作用:
- 设置自己的账户信息
- 设置 inviteStartCount
- 设置 syncKey
- 获取部分联系人
它被定义在 components/login.py
'''
raise NotImplementedError()
def show_mobile_login(self):
''' 展示网页版微信登陆标志
手机微信上将展示网页版微信已登录标志
即使不调用此方法,一段时间后也会加上网页版微信登陆标志
它被定义在 components/login.py
'''
raise NotImplementedError()
def start_receiving(self, finishCallback=None):
''' 打开心跳循环线程并接收消息
可选参数:
- finishCallback: 退出后的回调
- 它包含退出回调
作用:
- messages: 格式化信息并传递给被注册的 fns
- contact : 收到信息后群将被跟新
它被定义在 components/login.py
'''
raise NotImplementedError()
def get_msg(self):
''' 获取消息
for fetching
- 此方法模块有时使用
- 有新消息待接收时
- 或者其他任何时间,比如
- 使用返回的 synccheckkey 更新 synckey
它被定义在 components/login.py
'''
raise NotImplementedError()
def logout(self):
''' 退出
如果程序核心还存活
此方法将退出微信网页版
并且程序将准备下一次的登陆
它被定义在 components/login.py
'''
raise NotImplementedError()
def update_chatroom(self, userName, detailedMember=False):
''' 更新群
关于群联系人
- 群联系人需要更新以获得详细信息
- 详细信息指 群成员,加密id...
- 心脏循环自动更新更详细
- 群成员 uin 也将被覆盖
- 被调用一次,更新后的信息将被存储
for options
- userName: 群成员昵称
- detailedMember: 是否获取联系人
它被定义在 components/contact.py
'''
raise NotImplementedError()
def update_friend(self, userName):
''' update chatroom
for friend contact
- 被调用一次,更新后的信息将被存储
for options
- userName: 好友昵称
它被定义在 components/contact.py
'''
raise NotImplementedError()
def get_contact(self, update=False):
''' fetch part of contact
for part
- all the massive platforms and friends are fetched
- 如果 update = True, 则只获取星标群
for options
- update: 如果未设置,则返回本地值
返回值:
- 群列表 list
它被定义在 components/contact.py
'''
raise NotImplementedError()
def get_friends(self, update=False):
''' 获取好友信息 list
for options
- update: 如果未设置,则返回本地值
for results
- 返回好友信息 list
它被定义在 components/contact.py
'''
raise NotImplementedError()
def get_chatrooms(self, update=False, contactOnly=False):
''' 获取群列表
for options
- update: 如果未设置,则返回本地值
- contactOnly: 如果设置了,则只返回星级群
for results
- 返回群信息 list
它被定义在 components/contact.py
'''
raise NotImplementedError()
def get_mps(self, update=False):
''' 获取公众号列表
for options
- update: 如果未设置,则返回本地值
for results
- 返回公众号信息 list
它被定义在 components/contact.py
'''
raise NotImplementedError()
def set_alias(self, userName, alias):
''' 为朋友设置别名
for options
- userName: 用户昵称
- alias: 别名
它被定义在 components/contact.py
'''
raise NotImplementedError()
def set_pinned(self, userName, isPinned=True):
''' 锁定好友或群
for options
- userName: 用户昵称或群昵称
- isPinned: 是否锁定
它被定义在 components/contact.py
'''
raise NotImplementedError()
def add_friend(self, userName, status=2, verifyContent='', autoUpdate=True):
''' 添加朋友或接受朋友
for options
- userName: 用户昵称
- status:
- 添加 status = 2
- 接受 status = 3
- ticket: 问候语
- userInfo: 好友的其他信息(用于添加到本地存储)
它被定义在 components/contact.py
'''
raise NotImplementedError()
def get_head_img(self, userName=None, chatroomUserName=None, picDir=None):
''' 文件存放处
for options
- 如果要获取聊天室标题:仅设置聊天室用户名
- 如果要获取好友标题:仅设置用户名
- 如果要获取聊天室成员标题:请同时设置
它被定义在 components/contact.py
'''
raise NotImplementedError()
def create_chatroom(self, memberList, topic=''):
'''创建群
for creating
- 严格限制调用频率
for options
- memberList: 群成员列表
- topic: 新群主题
它被定义在 components/contact.py
'''
raise NotImplementedError()
def set_chatroom_name(self, chatroomUserName, name):
''' 设置群名称
for setting
- 它会更新群信息
- 这意味着详细信息将在心脏循环中返回
for options
- chatroomUserName: 群名
- name: 新的群名称
它被定义在 components/contact.py
'''
raise NotImplementedError()
def delete_member_from_chatroom(self, chatroomUserName, memberList):
''' 从群删除成员
for deleting
- 不能删除自己
- 如果删自己,则什么也不会删除
- 严格限制频率
for options
- chatroomUserName: 群名
- memberList: 群成员信息列表
它被定义在 components/contact.py
'''
raise NotImplementedError()
def add_member_into_chatroom(self, chatroomUserName, memberList,
useInvitation=False):
''' 添加群成员
for adding
- 您不能将自己或该群已有成员添加到群中
- 如果是,则不添加任何人
- 如果成员在添加后超过40个,则必须使用邀请
- 严格限制频率
for options
- chatroomUserName: 群名
- memberList: 群成员信息列表
- useInvitation: 如果不需要邀请,请将此设置为使用
它被定义在 components/contact.py
'''
raise NotImplementedError()
def send_raw_msg(self, msgType, content, toUserName):
''' 一个方法发送多条消息
for demo
.. code:: python
@itchat.msg_register(itchat.content.CARD)
def reply(msg):
itchat.send_raw_msg(msg['MsgType'], msg['Content'], msg['FromUserName'])
这里有一些小把戏,你可以自己发现
但是记住,他们只是骗局
它被定义在 components/messages.py
'''
raise NotImplementedError()
def send_msg(self, msg='Test Message', toUserName=None):
''' 发送纯文本消息
for options
- msg: 如果msg中有非 ascii 字符,则应为unicode
- toUserName: 对方昵称
它被定义在 components/messages.py
'''
raise NotImplementedError()
def upload_file(self, fileDir, isPicture=False, isVideo=False):
''' 将文件上传到服务器并获取mediaId
for options
- fileDir: 准备发送的文件
- isPicture: 文件是否为图片
- isVideo: 文件是否为视频
for return values
将返回一个返回值
如果成功, mediaId 将返回,字段是 r['MediaId']
它被定义在 components/messages.py
'''
raise NotImplementedError()
def send_file(self, fileDir, toUserName=None, mediaId=None):
''' 发送文件消息
for options
- fileDir: 准备发送的文件
- mediaId: 文件的mediaId,upload_file方法可返回
- 如果设置,可避免文件上传两次
- toUserName: 对方用户昵称
它被定义在 components/messages.py
'''
raise NotImplementedError()
def send_image(self, fileDir, toUserName=None, mediaId=None):
''' 发送图片
for options
- fileDir: 准备发送的图片文件
- 如果是一个gif,则命令类似 'xx.gif'
- mediaId: 文件的mediaId,upload_file方法可返回
- 如果设置,可避免文件上传两次
- toUserName: 对方用户昵称
它被定义在 components/messages.py
'''
raise NotImplementedError()
def send_video(self, fileDir=None, toUserName=None, mediaId=None):
''' 发送视频消息
for options
- fileDir: 准备发送的文件目录(不可有中文),如:'./vedio/text_vedio.mp4'
- 如果设置了 mediaId , 则可不用设置 fileDir
- mediaId: 文件的mediaId,upload_file方法可返回
- 如果设置,可避免文件上传两次
- toUserName: 对方用户昵称
for return
发送结果,json格式
for example
ret = itchat.send_video('./vedio/text_vedio.mp4', toUserName=msg['FromUserName'])
print("返回值:",ret)
它被定义在 components/messages.py
'''
raise NotImplementedError()
def send(self, msg, toUserName=None, mediaId=None):
''' 所有发送函数的包装函数
for options
- msg: 不同字符串开头的消息,标识不同的消息类型
- 可选类型: ['@fil@', '@img@', '@msg@', '@vid@']
- 分别代表文件类型 文件、图像、纯文本、视频
- 如果他们都不匹配,则以纯文本形式发送
- toUserName: 好友的用户名
- mediaId: 如果设置,则不会重复上传
它被定义在 components/messages.py
'''
raise NotImplementedError()
def dump_login_status(self, fileDir=None):
''' 将登录状态转储到特定文件
for option
- fileDir: 转储登录状态的文件
它被定义在 components/hotreload.py
'''
raise NotImplementedError()
def load_login_status(self, fileDir,
loginCallback=None, exitCallback=None):
''' 从特定文件加载登录状态
for option
- fileDir: 用于加载登录状态的文件
- loginCallback: 登陆回调,登陆成功之后的回调
- 如果没有设置,将清除屏幕并删除二维码
- exitCallback: 退出后的回调
- 它包含退出调用
它被定义在 components/hotreload.py
'''
raise NotImplementedError()
def auto_login(self, hotReload=False, statusStorageDir='itchat.pkl',
enableCmdQR=False, picDir=None, qrCallback=None,
loginCallback=None, exitCallback=None):
''' 像web版微信一样登陆
用于登陆
- 将下载并打开一个二维码
- 然后记录扫码状态,程序暂停等待你微信确认登陆
- 最终微信将登陆成功并展示你的昵称
可选参数
- hotReload: 是否启动热重载(保存登陆状态,一段时间之内免登陆)
- statusStorageDir: 用于存储登录状态的目录
- enableCmdQR: 在命令行展示
- 整数可以适应奇怪的字符长度
- picDir: 存储二维码的路径,默认代码同级目录
- qrCallback: 二维码回调,可接受参数 uuid, status, qrcode
- loginCallback: 登陆回调,登陆成功之后的回调
- 如果没有设置,将清除屏幕并删除二维码
- exitCallback: 退出后的回调
- 它包含退出调用
示例:
..code::python
import itchat
itchat.auto_login()
它被定义在 components/register.py
当然,登录的每一步都可以在外部调用
- 你可以扫描源代码看看
- 根据你自己的需要修改
'''
raise NotImplementedError()
def configured_reply(self):
''' 如果定义了消息的方法,则确定消息的类型并答复
然而,我使用一种奇怪的方法来确定msg是否来自大型平台
我还没有找到更好的解决办法
我担心的主要问题是新朋友在电话中的不匹配
如果你有什么好主意,请提交一个 issue. 我将不胜感激。
'''
raise NotImplementedError()
def msg_register(self, msgType,
isFriendChat=False, isGroupChat=False, isMpChat=False):
''' 一个装饰构造器
根据给定的信息返回特定的装饰器
for example:
#可以通过print(msg)方法查看消息里的所有数据,然后进行提取,
@itchat.msg_register(itchat.content.TEXT) #只对个人消息有效
def print_content(msg):
print(msg['Text'])
'''
raise NotImplementedError()
def run(self, debug=True):
''' 开始自动回复(程序挂起,保持运行)
for option
- debug: 如果 True,调试信息将显示在屏幕上
它被定义在 components/register.py
'''
raise NotImplementedError()
def search_friends(self, name=None, userName=None, remarkName=None, nickName=None, wechatAccount=None):
return self.storageClass.search_friends(name, userName, remarkName, nickName, wechatAccount)
''' 搜索用户信息
for option
- name: 群聊全名
- userName: 好友名称含有的字符
- remarkName:备注名
- nickName:昵称
- wechatAccount:微信号
- 参数可以一同使用
for example
# 获取自己的用户信息,返回自己的属性字典
itchat.search_friends()
# 获取特定UserName的用户信息
itchat.search_friends(userName='@abcdefg1234567')
它被定义在 components/register.py
'''
def search_chatrooms(self, name=None, userName=None):
return self.storageClass.search_chatrooms(name, userName)
''' 搜索群聊
for option
- name: 群聊全名
- userName: 群聊名称含有的字符
- 两个参数同时使用,则只有 userName 参数生效
for example
# 获取特定名称的群聊,返回值为一个字典
itchat.search_chatrooms(userName='@abcd1234')
# 获取名字中含有特定字符的群聊,返回类型 list
itchat.search_chatrooms(name='xyz')
它被定义在 components/register.py
'''
def search_mps(self, name=None, userName=None):
return self.storageClass.search_mps(name, userName)
''' 搜索公众号
for option
- name: 公众号全名
- userName: 公众号名称含有的字符
- 两个参数同时使用,则只有 userName 生效
for example
# 获取特定UserName的公众号,返回值为一个字典
itchat.search_mps(userName='@abcd1234')
# 获取名字中含有特定字符的公众号,返回类型 list
itchat.search_mps(name='xyz')
它被定义在 components/register.py
'''
感谢原作者的翻译,转载一下方便查看。原地址
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构