Apply auto-reply in your private WeChat account
It is not too hard to apply an auto-reply program in your WeChat Official account
There have been many interface that can apply this function in an WeChat Official Account. In fact, WeChat itself have applied many function to fulfill the Official account if you want
Today our focus is on private WeChat account. How can we add functions on our own weChat account so we can have fun for ourselves?
Here we use python to help us with auto-reply function.
1. Import Python itchat package
sudo pip install itchat
2. Sign up Turing API
go to this website: http://www.tuling123.com/ and sign up turing API
Then paste your own Key in dashboard.
3. Create your own program
create a main.py in your local environment and paste the code below
# coding=utf8
import itchat
import sys
import requests
import json
import TuringAPIDataProcess
reload(sys)
sys.setdefaultencoding('utf-8')
turingApiUrl = 'http://www.tuling123.com/openapi/api'
itchat.wechatTable = {}
itchat.openTurning = u'芝麻开门'
itchat.closeTurning = u'芝麻关门'
def getMessageFromTuring(msg):
inputData = {
'key': '', # Paste your own Turing API key here
'loc': '北京市中关村',
'userid': '116985',
'info': msg
}
rawData = json.dumps(inputData)
r = requests.post(turingApiUrl,rawData)
return r.json()
def processTuringJsonData(data):
return TuringAPIDataProcess.codeMap[data['code']](data)
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
text = msg['Text']
FromUserName = msg['FromUserName']
if itchat.wechatTable.has_key(FromUserName):
if text == itchat.closeTurning:
itchat.wechatTable[FromUserName]['isTurningOn'] = False
return
if text == itchat.openTurning:
itchat.wechatTable[FromUserName]['isTurningOn'] = True
return "欢迎光临~接下来的时间由我来回答你的问题哦~~~ 如果不想和我聊天就输入‘芝麻关门’~~~~我就会走哦~~"
if itchat.wechatTable[FromUserName]['isTurningOn'] == True:
resultJson = getMessageFromTuring(msg['Text'])
text = processTuringJsonData(data=resultJson)
return text
else:
itchat.wechatTable[FromUserName] = { 'isTurningOn': False }
return text_reply(msg)
def main():
itchat.auto_login()
itchat.run()
if __name__ == '__main__':
main()
create a TuringAPIDataProcess.py
# -*- coding: UTF-8 -*-
def getNormalText(data):
return data['text']
def getListText(data):
text = data['text'] + "\n"
for article in data['list']:
one = u'=> 标题:' + article["article"]+'\n' + u'来源:' + article["source"]+ '\n'+ u'详情:' + article["detailurl"]+ '\n'
text = text + one.encode('utf-8')
return text
def getUrlText(data):
text = data['text'] +'\n' + data['url']
return text
def getFoodRecipe(data):
text = data['text'] + "\n"
for article in data['list']:
one = u'=> 图片:' + article["icon"]+'\n' + u'=> 名称:' + article["name"]+'\n' + u'信息:' + article["info"]+ '\n'+ u'详情:' + article["detailurl"]+ '\n'
text = text + one.encode('utf-8')
return text
codeMap = {
100000 : getNormalText,
200000 : getUrlText,
302000 : getListText,
308000 : getFoodRecipe
}
4. Run your app with single commend
python main.py
After this you will see a QR Code and you can scan the code and then you will enjoy the talk with a auto-reply robot by turing api.
Input ‘芝麻开门’ so that the robot start talking with you.
Important: The robot will set a separate state for each conversation
For source code see here:
https://github.com/WJerry0227/WeChatReply