1、前期准备:
需要的库:requests和itchat
机器人申请:博主在丽丽机器人上申请的http://www.itpk.cn/
2、代码
1 # -*- coding: utf-8 -*- 2 import itchat 3 import requests 4 5 my_api_key='' 6 my_api_secret='' 7 def get_response(msg): 8 apiurl = 'http://i.itpk.cn/api.php' #moli机器人的网址 9 data={ 10 "question": msg, #获取到聊天的文本信息 11 "api_key": "my_api_key", 12 "api_secret": "my_api_secret" 13 } 14 15 r=requests.post(apiurl,data=data) #构造网络请求 16 return r.text 17 @itchat.msg_register(itchat.content.TEXT) #好友消息的处理 18 def print_content(msg): 19 return get_response(msg['Text']) 20 @itchat.msg_register([itchat.content.TEXT], isGroupChat=True) #群消息的处理 21 def print_content(msg): 22 return get_response(msg['Text']) 23 itchat.auto_login(True) #自动登录 24 itchat.run() #启动聊天机器人