Skip to main content

Starter Bot

Powered by Wechaty TypeScript

Starter bot is the most basic bot that you can build using Wechaty. You can use this bot as a template for creating an advanced version of the bot by doing further customizations.

In this tutorial, you will get step-by-step instructions for creating the Starter bot.

Requirements

  1. Node.js v16+
  2. Wechaty v0.40+

Getting started

You should have Node.js installed on your system. If you do not have Node.js installed (or have a version below 12), then you need to install the latest version of Node.js by following the links below:

Node.js installation docs

Installation guide for Node.js on other platforms can be found here.

Building the bot

Let's get started with building the Starter bot using Wechaty.

1. Initialize project

Create a new folder called starter-bot and move into the directory:

mkdir starter-bot
cd starter-bot

Use the following command to initialize a NPM project:

npm init -y

This will generate a package.json file containing these:

{
"name": "starter-bot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

2. Install dependencies

You will need the wechaty NPM package for building this bot & ts-node for running the TypeScript file of the bot. Install them using the following commands:

npm install wechaty
npm install ts-node

You will also need to add dependencies for using any Wechaty Puppet which helps to integrate Wechaty with various instant messaging (IM) systems (such as WeChat, WhatsApp, and WeCom):

  1. If you want to use WhatsApp, install wechaty-puppet-whatsapp:

    npm install wechaty-puppet-whatsapp
  2. If you want to use WeChat, you can try the following puppets:

    • Web Protocol: Install wechaty-puppet-wechat:

      npm install wechaty-puppet-wechat
    • iPad Protocol:

      • padlocal: Install wechaty-puppet-padlocal:

        npm install wechaty-puppet-padlocal

        Then get a token like puppet_padlocal_XXX, know more about puppet service padlocal here.

      • paimon: Install wechaty-puppet-service:

        npm install wechaty-puppet-service

        Then get a token like puppet_paimon_XXX, know more about puppet service paimon here.

  3. If you want to use WeCom, install wechaty-puppet-service:

    npm install wechaty-puppet-service

    Then get a token like puppet_wxwork_XXXXX, more about puppet service wxwork here.

You can find more information about the puppets here.

3. Write code for bot

Create a new file called starter-bot.ts in the root project directory. Add the following to the file:

// Importing the Wechaty npm package
import { Wechaty } from 'wechaty'

// Initializing the bot
const bot = new Wechaty({
name: 'starter-bot',
})

// Starting the bot
bot
.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://wechaty.js.org/qrcode/${encodeURIComponent(qrcode)}`))
.on('login', user => console.log(`User ${user} logged in`))
.on('message', message => console.log(`Message: ${message}`))
.start()

Running the bot

You have to export/set an environment variable with the type of puppet to use:

export WECHATY_LOG=verbose
export WECHATY_PUPPET=wechaty-puppet-wechat

# For using WhatsApp:
# export WECHATY_PUPPET=wechaty-puppet-whatsapp

# For using WeCom:
# export WECHATY_PUPPET=wechaty-puppet-service
# export WECHATY_PUPPET_SERVICE_TOKEN="puppet_wxwork_XXXXX"

If you are using WeCom, you can get token from puppet service wxwork.

Run the bot using the following command:

ts-node src/starter-bot.ts

References