python3 itchat自动回复私聊_python itchat实现微信自动回复

本次使用图灵机器人接口实现个人微信的自动回复。

一:首先申请图灵账号

http://www.tuling123.com/

申请完在后台创建机器人

e9b50994f3e264374e2d3252f0e50c22.png

得到apikey

二:

使用python提供的itchat实现个人微信登录和自动回复

(1)微信登录和消息监听

# -*- coding=utf-8 -*-

import requests

import itchat

import random

from Tuling import Tuling

import sys

reload(sys)

sys.setdefaultencoding('utf8')

tuling = Tuling('http://openapi.tuling123.com/openapi/api/v2') #图灵机器人2版本

@itchat.msg_register(itchat.content.TEXT)

def tuling_reply(msg):

#msg['Text'] = unicode(msg['Text'], 'utf - 8')

defaultReply = 'I received: ' + msg['Text']

print defaultReply

robots=["\n——-(机器人小黄)","\n——-(机器人大奔)"]

reply = tuling.getTextByV2(msg['Text'])+random.choice(robots)

return reply or defaultReply

itchat.auto_login(enableCmdQR=2)

itchat.run()

(2)图灵机器人回复

# -*- coding:utf-8 -*-

import requests

import json

import random

#图灵机器人实现聊天自动回复

class Tuling():

key = ['k1','k2']

url = 'http://openapi.tuling123.com/openapi/api/v2'

def __init__(self,url):

self.url = url

def getKey(self):

return random.randint(0, len(self.key)-1)

def getTextByV2(self,text):

data = {

"reqType": 0,

"perception": {

"inputText": {

"text": text

},

},

"userInfo": {

"apiKey": self.getKey(),

"userId": "111"

}

}

data = json.dumps(data).encode('utf8')

try:

r = requests.post(self.url, data=data, headers={'content-type': 'application/json'}).json()

return r.get('results')[0]['values']['text']

except Exception as e:

return e

三:运行代码  扫码登录微信,开始愉快聊天吧

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值