python——聊天机器人

http://www.tuling123.com 进入个人中心,创建一个机器人

python——聊天机器人_第1张图片

用命令下载模块itchat

pip install itchat

python代码: 

import itchat
import requests

def get_response(msg):
    apiUrl = "http://www.tuling123.com/openapi/api"
    data = {'key':'50a58e378d43404d975580fb88c3e5c4', #创建的机器人的apikey
            'info':msg,
            'userid':'lanse'}
    r = requests.post(apiUrl,data = data).json()
    return r.get('text')

#用于接收好友对话消息
@itchat.msg_register(itchat.content.TEXT)
def print_content(msg):
    return get_response(msg['Text'])

#通过微信扫描二维码登录
itchat.auto_login()
itchat.run()

效果:

python——聊天机器人_第2张图片

你可能感兴趣的:(python——聊天机器人)