Quick implementation of Python-based WeChat chat robot sample code

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

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.