@chengweihuang
2019-02-19T12:15:03.000000Z
字数 1336
阅读 552
未分类
# encoding: utf-8__author__ = 'ChengweiHuang'__date__ = '2018/11/13 19:48'#pip install itchat 先导库# http://www.tuling123.com/ 进入网站 注册账号import itchatimport requestsimport jsondef get_response(msg):# 下面是图灵机器人配置部分url = "http://openapi.tuling123.com/openapi/api/v2"data = {"reqType": 0,"perception": {"inputText": {"text": msg},},"userInfo": {"apiKey": "a79fcc9fe2fa4b43948db43345d8858b", # 这个地方 写你自己的apiKey"userId": "qweasfsfsdf"}}# 回复消息response = requests.post(url, data=json.dumps(data)).json()return response.get('results')[0].get('values').get('text')@itchat.msg_register(itchat.content.TEXT) # 当聊天类型 是TEXT 的时候def write_back(msg):return get_response(msg["Text"]) # 调用回复函数if __name__ == "__main__":itchat.auto_login(True) # 登录微信 设置 True 可以在短期 不用重复登录itchat.run() # 实时监听聊天
登录篇
import itchat# pip install itchat# 实现给好友发消息itchat.auto_login(True) # 登录 扫描二维码# itchat.auto_login(True) 设置True 短期之内 不用重复登录friend = itchat.search_friends('黄志宇')[0]# 搜索好友 以字典的形式 展示出来# 括号里的 参数写朋友的名字 不写 默认自己print(friend)itchat.send('你好',toUserName=friend["UserName"])itchat.logout()# 装饰器itchat.msg_reister 可以根据接受消息的类型 寻找对应的注册方法 如果一个消息# 没有对应的注册方法 ,该消息被舍弃
自动回复篇
# encoding: utf-8__author__ = 'ChengweiHuang'__date__ = '2018/11/13 19:29'import itchat@itchat.msg_register(itchat.content.TEXT) # 文本消息类型def write_back(msg):#print(msg) # 关于聊天的消息 字典形式itchat.send(msg['Text'],toUserName=msg['FromUserName'])print(msg['Text'])itchat.auto_login(True)itchat.run() # 实时监控聊天信息