preface
Last article This paper introduces how to configure the robot framework, and realizes some simple functions. (send private chat or group chat information, receive reported events, simple automatic reply, etc.)
This time, in order to make QQ robot more intelligent, some practical interfaces are called. Through the robot built by ourselves, we can regularly wake up our girlfriend and chat with her.
As shown in the figure above, my robot will wake up my girlfriend on time every day; And when I'm busy, but my girlfriend is bored, I can chat with her for a while.
Concrete implementation
The following functions need to be configured by the robot, and some functions in my last article have been written.
Last article address - > > > QQ robot production tutorial, super detailed!
1. Send information regularly
It is not difficult to realize this function. You only need to introduce the variable time. The specific code is as follows (QQ number, internal time and msg can be changed by yourself):
import datetime import time while True: qq=girl friend qq number now = datetime.datetime.now() if(now.hour==0 and now.minute==0): send_msg({'msg_type': 'private', 'number': qq, 'msg': 'good night!'}) send_msg({'msg_type': 'private', 'number': qq, 'msg':'[CQ:poke,qq={}]'.format(qq)}) time.sleep(60) continue if (now.hour == 9 and now.minute == 30): send_msg({'msg_type': 'private', 'number': qq, 'msg': 'Wake up'}) send_msg({'msg_type': 'private', 'number': qq, 'msg': '[CQ:poke,qq={}]'.format(qq)}) time.sleep(60) continue else: continue
The above code is: send your girlfriend good night and poke her at 0:00 every day; I get up at 9:30 every morning and poke her. You can improve these yourself, send expression packages, and even put 'time' in the code Sleep (60) 'delete, then you will send messages to bomb her continuously within one minute. It is strongly recommended to use it!
2. Let the robot chat with his girlfriend
To realize this function, I need to use the API interface. At present, the address of the practical intelligent reply API interface I found is: http://api.qingyunke.com/
This interface can not only chat, but also have some practical functions, as shown in the figure below:
To achieve the above functions, you need to get the information sent by your girlfriend. This is in Last article It has been written. After obtaining it, directly call the interface to return the result, and then send it to her.
The following code is directly:
from receive import rev_msg import requests import json import urllib.request from urllib.parse import quote import string while True: try: rev = rev_msg() print(rev) if rev == None: continue except: continue if rev["post_type"] == "message": if rev["message_type"] == "private": #Private chat message=rev['raw_message'] if 'face' in message: qq = rev['sender']['user_id'] img = rev['raw_message'] send_msg({'msg_type': 'private', 'number': qq, 'msg': img}) elif 'image' in message: qq = rev['sender']['user_id'] img=rev['raw_message'] send_msg({'msg_type': 'private', 'number': qq, 'msg': img}) elif 'Poke' in message: qq = rev['sender']['user_id'] send_msg({'msg_type': 'private', 'number': qq, 'msg': 'Stop poking'}) else: url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg=' + message s = quote(url, safe=string.printable) try: with urllib.request.urlopen(s) as response: html = response.read() # Decode the obtained response content and convert the json string content to python dictionary format # Get the content replied by the robot through subscript qq = rev['sender']['user_id'] # print(eval(html.decode("utf-8"))["content"]) send_msg({'msg_type': 'private', 'number': qq, 'msg': eval(html.decode("utf-8"))["content"].replace('{br}', '\n')}) # print(eval(html.decode("utf-8"))["content"]) except: qq = rev['sender']['user_id'] send_msg({'msg_type': 'private', 'number': qq, 'msg': 'Reconnecting...'}) else: # rev["post_type"]=="meta_event": continue
In the above code, first judge the type of "post_type". If it is message, it indicates that someone has sent a message to the robot; Look at "message_type". If it is private, it indicates that this is a private chat message. Of course, if it is group, it indicates that this is a group chat message; Then we get the message message=rev ['raw_message']. After we get this message, we can set keywords to reply by ourselves.
The 'face' in message and 'image' in message I set in the code are to obtain expressions and pictures. The other party will return whatever it sends, because the intelligent reply interface can't recognize expressions and pictures.
3. Call some interesting interfaces
stay Aggregate data I found some interesting interfaces, many of which are free.
This article takes the constellation fortune as an example to teach you how to realize it. Applying for an api will give you a request for key, which requires real name authentication first.
After applying for an interface, click test and enter two request parameters to return the results.
The python code is as follows (you need to use your own api to request a key):
def horoscope(constellation): # Request address url = "http://web. juhe. cn/constellation/getAll? "+'consname = {} & type = today & key = your own request key '. Format (consumption) # Send get request r = requests.get(url) # Get the returned json data result = r.json()['summary'] print(result) return result horoscope('Capricornus')
Then integrate it into the robot program:
if 'Constellation fortune' in message: try: constellation = message.split(' ')[1] text = horoscope(constellation) qq = rev['sender']['user_id'] send_msg({'msg_type': 'private', 'number': qq, 'msg': text}) except: qq = rev['sender']['user_id'] send_msg({'msg_type': 'private', 'number': qq, 'msg': 'Please put a space before the constellation.'})
Health reporting assistant development...
A few days ago, a counselor friend read my article and asked me if I could make a QQ robot about the health filling assistant. He reminds students to fill in the health report every day, and he needs to log in to the website to obtain the statistical data every time he checks who hasn't filled in, which will inevitably forget or it's too annoying to do these things every day, so he wants me to develop a QQ robot for him.
After a brief exchange, I learned about the functions required by this QQ robot:
1. Send the names of all students who have not submitted health reports regularly every day
2. Counselors poke QQ robot or Aite, and it will also send the names of all students who have not submitted health reports
3. Set a specific time T. If it has not been submitted after t, it will be chatted privately by QQ robot
Write at the end
Previous article: QQ robot production tutorial, super detailed!
It's not easy to make. I look forward to your one key three links!
If you have any questions, please communicate in the comment area.