首页 > 编程 > Python > 正文

基于python的itchat库实现微信聊天机器人(推荐)

2019-11-25 11:31:16
字体:
来源:转载
供稿:网友

一、开始之前必须安装itchat库

pip install itchat(使用pip必须在电脑的环境变量中添加Python的路径)

conda install request

二、开始编程前,我们需要在图灵机器人官网注册自己的图灵机器人,来实现我们程序的智能聊天功能

1、图灵机器人官网(http://www.turingapi.com/)

2、注册登录后点击创建机器人

3、创建成功后,可以获得机器人提供的API接口(apikey)

三、代码实现

import itchatimport requests#上传获得消息内容到图灵机器人def getMessage(msg):  apiURL='http://www.tuling123.com/openapi/api'  data={'key':'你的apikey',     'info':msg,     'userID':'你的userID(可以随便写)'     }  r=requests.post(apiURL, data=data).json()  print('答:'+r.get('text'))  return r.get('text')#监听个人微信聊天@itchat.msg_register(itchat.content.TEXT)def return_message(msg):  try:    print('问:'+msg['Text'])  except Exception as e:    print(e)  return getMessage(msg['Text'])'''#监听微信群聊天@itchat.msg_register([itchat.content.TEXT],isGroupChat=True)def return_message(msg):  print('问:'+msg['Text'])  return getMessage(msg['Text'])'''if __name__=='__main__':  itchat.auto_login(hotReload=True)  itchat.run()

总结

以上所述是小编给大家介绍的基于python的itchat库实现微信聊天机器人,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表