Recently heard of a very fun Turing machine Human API, just can use it to do a chat robot, the following is the implementation
# test.pyimport requestsimport itchat #这是一个用于回复的库KEY = ' 8edce3ce905a4c1dbb965e6b35c3834d ' #这个key可以直接拿来用 # Send request def to API Get_response (msg): apiurl = ' Http://www.tuling123.com/openapi/api ' data = { ' key ' : Key, ' info ' : Msg, ' userid ': ' Pth-robot ', } try: r = Requests.post (Apiurl, Data=data). JSON () return R.get (' text ') except: return# registration method \ @itchat. Msg_register (Itchat.content.TEXT) def tuling_reply (msg): # In order to ensure that there is a problem in Turing key can still reply, here set a default reply defaultreply = ' I Received: ' + msg[' Text '] # If there is a problem with Turing key, Then reply will be none reply = Get_response (msg[' Text ") # A or b means, if a has content, then return a, otherwise return B returns reply or defaultreply# use hot start Itchat.auto_login (hotreload=true) Itchat.run () in order to modify the program without multiple sweep codes
If you want this robot to run forever, you need to upload to the server, with the screen command to open a new window, run Python3 test.py, this time will be in the same directory to generate a qr.jpg file, but because generally we use SSH connection server, no image, So you need to use the SCP command, download to the local, use the phone to scan the code, so that the work is done
The effect is as follows:
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.