Now, robots are cool, but building a real robot requires lots of hardware knowledge.
And one common topic you'll notice in today's talk is that I am quite lazy.
So when I say "robot" I'm talking about a robot you can build with software: a chatterbot. Send a message to a WeChat account, and get a message back.
Why WeChat? WeChat is ubiquitous in China, and so it's a great way of getting your service in front of as many people as possible, without them having to download another app.
Now you or I have personal WeChat accounts, but if we want to build a bot we need an official account. There are two types of these, Subscription Accounts and Service Accounts.
Subscription Accounts are best for marketing purposes. You have the ability to send up to 5 "broadcasts" each month to all your followers - perhaps if you are a restaurant, you want to announce some new specials. The downside of subscription accounts is they are hidden away in the WeChat interface inside the Subscription Accounts section.
Service Accounts are listed in the main WeChat friends list. You're only allowed to send one broadcast a month. But, any time someone sends you a message, you can reply to them immediately, and also send them messages for up to 48 hours. So, if you're trying to do customer service via WeChat, or build a bot, a Service Account is a great choice.
Here's the signup form for a WeChat official account (currently Chinese only).
You'll need to provide some basic information like name, email address, and which type of account you want. The two pieces of information that may require some effort are the 营业执照 (Company License) and 组织机构代码证 (Organization number). Right now you'll need to have a Chinese company license to apply for a WeChat official account.
After submitting all your data, be prepared to wait. It will take about 3 business days for your app to get approved.
One you're finally approved, you have access to the WeChat API. This comes in two flavours: basic API and advanced API. The basic API provides all that we need to build a basic chatterbot!
In the Developers section, you now need to provide a URL and Token. At the URL provided, you now need to provide a simple web server. This will listen for requests from the WeChat server. So, for example every time someone sends you a message, you receive an HTTP request with the details of their message.
When you first register an application with WeChat they will send you an authorization request to your URL. This includes the token you provided earlier, plus various other OAuth credentials, and an parameter called "echo_str", which you should return if everything checks out OK.
As mentioned previously, I'm quite lazy, so to avoid having to figure out the authentication issues, I simply return the echo_str for ALL requests. This should not be recommended in a production application.
Now, each time you receive a message to your account, you'll get an HTTP request. You may be hoping for a nice JSON payload.
Unfortunately, Tencent decided to use XML, and not very well structured XML at that.
Here's what a typical incoming message looks like.
You get a FromUserName and ToUserName. These are actually encrypted so you don't have access to the user's real WeChat ID. There's also a timestamp, the type of message (in this case text, but it could also be image, video or voice) and the text content.
To parse this in Node.js, we install an npm package called express-xml-bodyparser and configure Express to use it.
Next, we implement a method to handle the request. We parse the values out of the XML...
Then we construct some XML to send back as a response. Notice that we've switched the to username and from username, to ensure the message gets sent back to the recipient. The text of our reply is "Thanks for sending me a message saying (original message)".
How does this work? Like this!
Now this is great, but of course version 1 of our robot is pretty stupid. How can we imbue our bot with some more intelligence?
To solve this I turned to the work of Joseph Weizenbaum.
He was a brilliant German-American computer scientist based at MIT. In 1966 he wrote a program called ELIZA. ELIZA was one of the first programs which tried to interpret and respond to natural language inputs from users.
It did this via some basic pattern matching. ELIZA could be fed with different scripts of patterns, and the most famous of these, called "DOCTOR", imitated a psychiatrist.
Here's ELIZA in action. You can see that ELIZA picks out certain words in a statement, and is able to formulate a reply using the words.
Now you're just talking nonsense!
> What makes you believe now I am just talking nonsense?
Weizenburg was surprised to discover that despite the primitiveness of the logic, users became quickly emotionally involved with interacting with ELIZA.
ELIZA is a fascinating episode in computing history, and I was delighted to see that the ELIZA logic is available as a npm module. That means integrating it into my bot was very simple.
First, install the module
Then, replace the reply logic with a new function.
Here's the code for replying. We keep a dictionary of ElizaBot objects. That way we can give each unique user who messages us their own instance. That means the message "memory" can be kept distinct.
If this is a new user, we set up a new ElizaBot for them, and request an initial phrase like "Please tell me your problem".
If this is an existing user, we simply call eliza.transform to get a suitable response, based on the last line of input (and previous inputs).
Let's see how this works:
There seems to be great potential for using ELIZA for people who are interested in app development services in Shanghai 😉
Of course, while this is a fun example, there's lots of cool stuff you could make with WeChat robots. A robot could return weather or air quality data on demand. If a user sends you a picture, you could do image processing on that picture and send them back something really creative. The only limit is your imagination!
To download the source code, check out the project on GitHub
Interested in finding out more about what's possible with WeChat, chatbots or mobile apps? Contact us!