DEV Community

Cover image for Sprint start your wechat bot with wechaty padplus
suntong
suntong

Posted on • Updated on

Sprint start your wechat bot with wechaty padplus

Almost all online chatting apps provide APIs to access their functionalities, but wechat is an exception. There are no official functionalities/supports on either APIs or bots. But that doesn't deter people from trying to automate those repetitive tasks in wechat with bots, all thanks to those people who had jumped through the hoops backward to provide programmable access to wechat functionalities. One of the best of such platform is padplus under the wechaty framework (believe me, I know it the hard way).

This article will illustrate how easy to get started with wechat bot programming using wechaty padplus. The existing documentation is already very helpful, please check out the following two if you haven't done so:

Both are very good, so what covered there will not be repeated here. Instead, we'll dive directly into practical aspect to get you hit the road and run.

The padplus readme gives a generic typescript code as a guideline on how to start your script. You cannot directly run it as there are still customization you need to make before stating it( e.g., replacing the padplus token with yours). To make it even more simpler, here is a code that you can grab and run right away:

What it does is to get the padplus token from the environment variable WECHATY_PADPLUS_TOKEN, so all you need to do is to set it before running the script. To set and run in one step, here is how to do it under linux:

WECHATY_PADPLUS_TOKEN=puppet_padplus_xxxxx ts-node starter-padplus-bot.ts
Enter fullscreen mode Exit fullscreen mode

screenshot

Me personally don't like TypeScript very much. I know it is good for corporation that help preventing people from shooting themselves in the foot, but I still prefer the plain old JavaScript, the ES6 actually, as I believe with proper tools (like eslint, 'use strict', etc) ES6 can be a good language under the care of a good programmer. The extra bell-and-whistles, and thus the extra overhead of TypeScript do not appeal to me much, as I write only for myself.

If you prefer JavaScript as well, I've prepared the JavaScript version of the starter-padplus-bot file (based on ding-dong-bot.js):

As you can see it is very close to a full-fledged bot script. It

  • has the shebang so that it can be started as a command under Linux.
  • has all the essential event handler examples
  • has logging code builtin and ready to be used
  • has logged all incoming message on console so that you can start dealing with them right away
  • has interactive behavior builtin so that people can check from the wechat end to verify if the bot is alive or not

Here is how to start it (under xterm, somehow I cannot get the small version of qrcode working, but only the normal sized, as shown in the picture. I think it might be a bug BTW):

screenshot

Once you can automate things for wechat, the application scenarios are endless. So start automate your wechat and make it alive!

And last but not least, the highlight of the whole article, if you are willing to contribute your code back to the wechaty community as open source project, you should be able to get a padplus token for free as long as you continue to contribute using many different ways including just blogging it.

Top comments (0)