Sprint start your wechat bot with wechaty padplus
Tags: chat, bot, wechat, weixin
Published: 2020-03-26 04:03:42

---
title: Sprint start your wechat bot with wechaty padplus
published: true
description: programming wechat bot with wechaty padplus
cover_image: https://i.stack.imgur.com/k9UIS.png
tags: chat,bot,wechat,weixin
---

Almost all online chatting apps provide APIs to access their functionalities, but [wechat](https://en.wikipedia.org/wiki/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](https://github.com/wechaty/wechaty-puppet-padplus) under the [wechaty](https://github.com/wechaty/wechaty-getting-started) 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:

- [Getting started with wechaty](https://github.com/wechaty/wechaty-getting-started)
- [The readme/easy-start doc for padplus](https://github.com/wechaty/wechaty-puppet-padplus)

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:

{% gist https://gist.github.com/suntong/c9031f98898d981d7f711dd5c7066c2d file=starter-padplus-bot.ts %}

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

![screenshot](https://img.vim-cn.com/ed/89b005e1cb960d6f145b75eca8958a7ae81bfd.png "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](https://github.com/wechaty/wechaty-getting-started/blob/master/examples/basic/ding-dong-bot.js)):

{% gist https://gist.github.com/suntong/c9031f98898d981d7f711dd5c7066c2d file=starter-padplus-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](https://img.vim-cn.com/3e/b1dc17d0c10f5e3ff320676d2f2c1572b1b6fa.png "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](https://github.com/wechaty/wechaty-puppet-padplus#notice) as long as you continue to contribute using many different ways including just blogging it.