How to make WeChat chatbot your personal servant?

David Yu
codeburst
Published in
4 min readApr 14, 2019

--

Photo by Alex Knight on Unsplash

In China, WeChat is the main communication app that everyone uses. For small companies, it’s often their main business communication tool as well.

Unfortunately, for software developers, you might be one message away from bug fixing. It’s manageable when you only have a few projects and a few groups.

As time goes by, the projects that need maintenance by you grow. Then you realize that you spend every morning reading through messages and organizing them to actionable items.

Therefore, I turned my WeChat into a chatbot that collects bug related message into Trello. Here’s how I did it.

How to turn your WeChat into a chatbot?

WeChaty takes advantage of the WeChat Web API to bridge your personal WeChat account. *Need account created before June of 2017.

Setup

In your node.js code,

const { Wechaty } = require('wechaty')

Wechaty.instance() // Global Instance
.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qrcode)}`))
.on('login', user => console.log(`User ${user} logined`))
.on('message', message => console.log(`Message: ${message}`))
.start()

You could also use qrcode-terminal to print the QR code directly in your terminal. It’s useful if you host on heroku and do heroku logs -t

Scan with your WeChat

On the terminal, it will display your WeChat user name. If no errors show up, your server is ready to receive messages.

How to identify a bug related message?

There are a few ways to go about it

  1. Communicate with the group member and use #bug as a keyword
  2. Target bug reporting group or person
  3. Whenever someone mentions you with @
  4. Use machine learning to predict message intent

The easiest and quickest way to go about it will be a combination of 1 and 2.

First, you check which group is the message coming from. WeChaty wraps all message as a Message class. You can get the group’s information by

message.room() // If it's a direct message, it's null

If the message is coming from one of the bug reporting group, pass down the message and analyze the text.

You can get the text of the message by

message.text()

Use Javascript’s string method .search for a quick check of the hashtag #bug

const text = message.text();
if(text.search('#bug') >= 0) {
// Do Magic
}

By the way, to check if someone mentioned you in the group you can do.

message.mentionSelf() // returns a boolean

The last method is something I am still learning how to do with Javascript. There are a lot of Tensorflow tutorials in Python like this one.

How to organize your bugs in Trello?

In short, these are the steps to have bugs’ card show up in the right place.

  1. Grant NodeJS server access to your Trello account
  2. Map board ID to WeChat group
  3. Get the right list of the board
  4. Create a card using the received message
  5. Test it out

Grant NodeJS server access to your Trello account

  • Get your API key
  • Use your API key to get an OAuth token, paste this in your browser
https://trello.com/1/authorize?expiration=never&name=MyPersonalToken&scope=read&response_type=token&key={YourAPIKey}&scope=write,read

Note:

  • If you only want to do this once, set the expiration to never
  • Keep your API key and Token safe using environment variables

Map board ID to WeChat group

You could create a board through API, but it’s relatively simple to click and create a new board through the web interface.

You will find the board ID in the URL

Get the group name by

const groupName = await message.room().topic();

Then you would have an object that reassembles

{
[groupName]: boardId
}

Get the right list of the board

Make a get request to Trello, don’t forget to pass it your API key and OAuth Token.

https://trello.com/1/boards/${boardID}/lists

Let’s say you have a list in the board named Bugs

const bugList = lists.filter((list) => list.name === 'Bugs');

Create a card using the received message

Make a post request to

https://trello.com/1/cards

The only required param is idList, you can check out the full list here. I usually use the sender’s WeChat user name as the name param to know who it’s from.

{
name: message.from().name(),
desc: message.text(),
idList: bugList[0].id // The ID we got above,
pos: 'top' // So it pushes down the older bugs
}

Test it out

Before deploying it, it’s best to do one or two message to test the result. Recall the message after you send it.

Other Ideas

  • In a team of developers, map WeChat username to Trello member ID to utilize Trello’s assign feature
  • Handle different media types such as screenshots and screen records
  • Save messages in a database and analyze sequence and intent
  • Have a way to mark priority and label
  • Auto reply if a bug is already fixed

Conclusion

This is only one example of how you can solve a problem by combining different tools. Let me know in the comment section what other ideas you have.

If you’re looking to learn more about WeChat, I have a free glossary just for you.

--

--

Full-stack developer based in Shanghai. I help people turning their ideas into reality.