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

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

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/4/4 14:45
# @Author  : An ning
# @email   : 18279460212@163.com
# @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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值