Python ItChat微信机器人

问题

想要将本地监控日志发送到微信或者其他通讯工具上,GitHub上搜了搜,看到了这个:ItChat

简单使用

ItChat功能包括:微信消息获取、重复回答、向指定用户或群组发送消息、图灵机器人等。调用的web版微信的接口,并能够在扫码登录后保持一定时间的登录状态。

消息获取并全部转发给特定用户:
itchat.auto_login(hotReload=True)
users = itchat.search_friends(name=u'此地一为别')
userName = users[0]['UserName']

@itchat.msg_register(itchat.content.TEXT)
def print_content(msg):
    print msg.text
    itchat.send(msg.text, toUserName = userName)
  • itchat.auto_login(hotReload=True):保持在线状态
  • itchat.search_friends(name=u'此地一为别'):根据好友名获取好友相关Info,该方法定义为:def search_friends(self, name=None, userName=None, remarkName=None, nickName=None, wechatAccount=None):,各参数含义可见参考
  • itchat.msg_register,修饰器定义为:def msg_register(self, msgType, isFriendChat=False, isGroupChat=False, isMpChat=False):,可以指定是好友还是群聊、公众号信息等
  • itchat.send(msg.text, toUserName = userName):发送消息,当userName为'filehelper'时,发送给微信文件传输助手
  • @itchat.msg_register(itchat.content.TEXT)获取的不仅为text类型的文本,也可以为:
    • 文本对应itchat.content.TEXT
    • 图片对应itchat.content.PICTURE
    • 语音对应itchat.content.RECORDING
    • 名片对应itchat.content.CARD
向特定群组发送消息
def sendToGroup(msgContentList, groupNickName):
    if len(msgContentList) == 0:
        print 'No msg need send'
        return True

    groups = itchat.search_chatrooms(name = groupNickName)
    print groups
    groupName = groups[0]['UserName']
    for msgContentItem in msgContentList:
        itchat.send(msgContentItem, toUserName = groupName)

itchat.auto_login(hotReload=True)
sendToGroup(warnMessageDict['ok'], u'机器人')
sendToGroup(warnMessageDict['pending'], u'机器人')
  • 发送前判断是否存在待发送消息
  • itchat.search_chatrooms:获取群聊相关Info,定义为def search_chatrooms(self, name=None, userName=None):,各参数含义可见参考
  • warnMessageDict是读取的监控日志信息形成的字典
将文本消息原封不动的返回
@itchat.msg_register(itchat.content.TEXT)
def print_content(msg):
    return msg['Text']

itchat.auto_login()
itchat.run()

注意开启自动重复回复时使用run方法。

参考文章

itchat API
ItChat GitHub
微信获取好友、公众号、群聊的信息

获取自己的用户信息,返回自己的属性字典
itchat.search_friends()
获取特定UserName的用户信息
itchat.search_friends(userName='@abcdefg1234567')
获取任何一项等于name键值的用户
itchat.search_friends(name='wxceshi')
获取分别对应相应键值的用户
itchat.search_friends(wechatAccount='wceshi')
三、四项功能可以一同使用:itchat.search_friends(name='wxceshi', wechatAccount='wcceshi')

获取特定UserName的群聊,返回值为一个字典
itchat.search_chatrooms(userName='@abcdefg1234567')
获取名字中含有特定字符的群聊,返回值为一个字典的列表
itchat.search_chatrooms(name='LittleCoder')
以下方法相当于仅特定了UserName
itchat.search_chatrooms(userName='@abcdefg1234567', name='LittleCoder')

获取特定UserName的公众号,返回值为一个字典
itchat.search_mps(userName='@abcdefg1234567')
获取名字中含有特定字符的公众号,返回值为一个字典的列表
itchat.search_mps(name='gzh')
以下方法相当于仅特定了UserName
itchat.search_mps(userName='@abcdefg1234567', name='gzh')

群聊用户列表的获取方法为update_chatroom。
群聊在首次获取中不会获取群聊的用户列表,所以需要调用该命令才能获取群聊的成员
该方法需要传入群聊的UserName,返回特定群聊的用户列表
memberList = itchat.update_chatroom('bcdefg67')

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • python实现微信接口(itchat) 安装 sudo pip install itchat 登录 itchat...
    爱撒谎的男孩阅读 14,406评论 2 64
  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 14,738评论 0 15
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,359评论 19 139
  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 12,378评论 2 17
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,054评论 6 342