微信自动回复小程序

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值