【原创】python打造自动聊天回复机器人

最近用 图灵 + itchat组件库写了一个微信自动回复的机器人,发现很多可以拓展的,仅供参考!

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/4/4 14:45
# @Author  : An ning
# @email   : [email protected]
# @File    : robot.py
# @Software: PyCharm

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

# 图灵机器人
# http://www.tuling123.com/member/robot/1380138/center/frame.jhtml?page=0&child=0获取apikey
KEY = 'your key'


def get_response(msg):
    apiUrl = 'http://www.tuling123.com/openapi/api'
    data = {
    
    
        'key': KEY,
        # 将接收到的数据发给图灵
        'info': msg,
        'userid': 'wechat-robot',
    }
    try:
    	# 拿到回复的消息
        r = requests.post(apiUrl, data=data).json()
        return r.get('text')
    except:
        return


@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
    defaultReply = 'I received: ' + msg['Text']
    # 随机在发送的后面添加这三个当中的一个
    robots = ['是我啊', '嘻嘻', '爱你']
    reply = get_response(msg['Text']) + random.choice(robots)
    return reply or defaultReply

# 生成图片或者打印在输出栏
itchat.auto_login(enableCmdQR=False)
itchat.run()

猜你喜欢

转载自blog.csdn.net/Python_anning/article/details/89030141