Quick implementation of Python-based WeChat chatbot Sample code

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.