I was a little late for a movie tonight. Last weekend wrote an article about building your own smart chatbot. It was reprinted by a technology big V on Weibo, and then the number of visits to the blog increased sharply. Then it was reprinted by a machine learning public account on wechat, and everyone seemed to be very interested in it. In order to live up to the increased traffic, they wrote something while it was hot
How to connect the chatbot to wechat and chat with friends instead of us
Source code I have hosted on Github: wechat_bot
Robot API
First, we decided to encapsulate the chatbot as an API service, so that it could be used in an unlimited manner and could serve any HTTP client
Another reason for this is that our wechat access script is python2 and the Chinese chatbot script is based on Python3. I don’t want to deal with the coding of Pyhton 2/3, so I packaged the bot as a service
Before you read this, assume you read my previous article about building your own smart chatbot
We went straight to building the chat service.
Create bot_api. Py:
#! /usr/bin/env python # coding: utf-8 from chatterbot import ChatBot from chatterbot.trainers import ChatterBotCorpusTrainer import hug deepThought = ChatBot("deepThought") deepthought.set_trainer (ChatterBotCorpusTrainer) # Train it using a Chinese corpus DeepThought. "train" (chatterbot. Corpus. "Chinese") @ # corpus hug. The get () def get_response (user_input) : response = deepThought.get_response(user_input).text return {"response":response}Copy the code
The source code is very simple and does not require much explanation
I chose Hug as my API framework here because it is very efficient (much more than Flask).
Review images
Install dependencies: Pip3 Install hug and Chatterbot
Run up service: hug -f bot_api.py
Review images
Testing services
Test in a browser
Review images
After detail letter
Thanks to @Liuwons for their wxBot project, which allows us to interact with wechat in code, thus making the input/output of the chat process programmable
So we can let chatbots take over our conversations.
The operation process
Wget download wxBot scripts to local: https://raw.githubusercontent.com/liuwons/wxBot/master/wxbot.py
Create wechat_bot.py:
#! /usr/bin/env python # coding: Utf-8 from wxbot import wxbot import requests bot_api = "http://127.0.0.1:8000/get_response" class MyWXBot (wxbot) : def handle_msg_all(self, msg): if msg['msg_type_id'] == 4 and msg['content']['type'] == 0: user_input = msg["content"]["data"] payload={"user_input":user_input} response = requests.get(bot_api,params=payload).json()["response"] #print(type(response)) # unicode self.send_msg_by_uid(response, msg['user']['id']) def main(): bot = MyWXBot() bot.DEBUG = True bot.conf['qr'] = 'png' bot.run() if __name__ == '__main__': main()Copy the code
PIP Install Requests PyQrCode PYPng Pillow
Running (using python2) : python wechat_bot.py
Then scan the code to log in
Began to chat
This is the picture of the message the program received
This is the chat interface taken over by a robot
Review images
Thank you for your little friend who came out in the middle of the night to assist you
have a good time
Ah, it’s midnight. Go to sleep