This article describes how to quickly implement the Python-based chatbot sample code. developed based on itchat, you can use it as a chatbot. if you are interested, you can take a look. Recently I heard that a very interesting Turing Robot api can be used as a chatbot. The following is an implementation
# Test. pyimport requestsimport itchat # This is a library KEY for replying to = '8edce3ce905a4c1dbb965e6b35c3834d' # this key can be used directly # send a request to the api def 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') failed t: return # registration method \ @ itchat. msg_register (itchat. content. TEXT) def tuling_reply (msg): # to ensure that you can still reply when the Turing Key is faulty, set a default defaultReply = 'I have ed: '+ msg ['text'] # if the Turing Key is faulty, reply will be None reply = get_response (msg ['text']) # a or B means, if a has content, a is returned; otherwise, B return reply or defaultReply # to enable the modification program to scan the code multiple times, enable itchat with heat. auto_login (hotReload = True) itchat. run ()
If you want the robot to run forever, you need to upload it to the server and use the screen command to open a new window and run python3 test.py. at this time, a qr.jpg file will be generated under the same directory, but generally we connect to the server using ssh without images, so we need to use the scp command. after downloading the code to the local device, we can scan the code on the mobile phone to complete the work.
The effect is as follows:
For more details about how to quickly implement Python-based chatbot sample code, please follow the PHP Chinese network!