1. 程式人生 > >Python實現微信聊天機器人

Python實現微信聊天機器人

# -- coding=utf-8 --
import requests
import itchat
import random
import json

KEY = ‘18c2919631d84026912c25970f23286f’

def get_response(info):
apiUrl = ‘http://www.tuling123.com/openapi/api’
data = {‘key’: KEY, ‘info’: info}
try:
req = requests.post(apiUrl, data=data).text
replys = json.loads(req)[‘text’]
return replys
except:
return replys
@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
defaultReply = ‘I received: ’ + msg[‘Text’]
robots = [’——By Robot’, ‘——By Robot’, ‘——By Robot’]
# message = ‘{}’.format(msg.text)
reply = get_response(info=msg.text) + random.choice(robots)
return reply or defaultReply
itchat.auto_login(hotReload=True)
itchat.run()
--------------------- 
作者:Hill 肖偉峰 
來源:CSDN 
原文:https://blog.csdn.net/weixin_42323337/article/details/83929600 
版權宣告:本文為博主原創文章,轉載請附上博文連結!