Python implements Turing robot to help you reply to WeChat friend messages



'''
Created on April 26, 2018 @author: cvv54 ''' #Introduce the itchat package to get WeChat messages import itchat from itchat.content import * import hashlib import requests #Register an account http://www.tuling123.com/ #Get your own robot and record apikey #Incoming message to bot and get response def get_response(msg,FromUserName): api_url = ' http://www.tuling123.com/openapi/api ' #Fill in your own apikey apikey= ' ************************* ************ ' hash=hashlib.md5() userid=hash.update(FromUserName.encode('utf-8')) data = {'key': apikey, 'info': msg, 'userid': userid } try : #Forward information to the api interface of the robot req = requests.post(api_url, data= data).json() #Return the reply of the robot return req.get( ' text ' ) except : return #Generate the QR code picture for WeChat login, and the user scans the code to log in to WeChat itchat.auto_login() #Only automatically reply to friends who send WeChat messages to themselves, not to group messages @itchat.msg_register([PICTURE,TEXT]) def simple_reply(msg): ''' if msg['Type']==TEXT: ReplyContent='I received message: '+msg['Content'] if msg['Type']==PICTURE: ReplyContent = 'I received picture: '+msg['FileName'] itchat.send_msg('[auto-reply]'+ReplyContent,msg['FromUserName']) ''' #Call the function, send information to the robot and return the reply to responses respones = get_response(msg[ ' Content ' ], msg[ ' FromUserName ' ]) # Forward the robot's reply to the sender of the message and add On "I'm a Turing Robot:" Distinguishing automatic replies from human replies itchat.send( " I'm a Turing Robot: " +respones, msg[ ' FromUserName ' ]) itchat.run()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324950115&siteId=291194637