跳到主要内容

Listen to events

提示

The wechaty bot is available in various programming languages!

Before starting this guide, make sure you are already familiar with the ding dong bot. If not, go to examples/basics. In this section, you will learn how to add various events to a bot. Events are functions that define the operations a bot can perform. Some common events include login, logout, onMessage. The guide will demonstrate in JavaScript, but you can choose between various programming languages available.

Getting started

  1. Create an empty project folder
  2. Add a bot.js file
  3. In the terminal, type the following command:
  4. npm init

    A package.json file is created.

  5. In the terminal, install the necessary dependencies
  6. npm i qrcode-terminal wechaty 
     npm i wechaty-puppet-PUPPET-PROVIDER

    The PUPPET-PROVIDER represents the messaging platform you want to integrate your bot with.

    • For WhatsApp:
     npm i wechaty-puppet-whatsapp
    • For WeChat:
     npm i wechaty-puppet-wechat
  7. In the package.json file, add a script:
  8. "start": "node bot.js"

  9. In the terminal, type the following commands:
  10.  export WECHATY_LOG=verbose
    • For WhatsApp:
    • export WECHATY_PUPPET=wechaty-puppet-whatsaap
    • For WeChat:
    • export WECHATY_PUPPET=wechaty-puppet-wechat
  11. Now, we can run the bot using the command:
  12. npm start

    But, before that we need to add events to the bot. See the following examples:

Basic Events

scan Event

The scan event generates a QR code to integrate your bot to a puppet provider.

import { ScanStatus } from 'wechaty'

async function onScan (
qrcode: undefined | string,
status: ScanStatus,
) {
console.info('Scan QR Code to login, status:', status, ScanStatus[status])
console.info('https://wechaty.js.org/qrcode/' + encodeURIComponent(qrcode))
}

bot.on('scan', onScan)
await bot.start()

login Event: bot contact

The login event logs the bot in, with the contact of the user.

import { Contact } from 'wechaty'

function onLogin (bot: Contact) {
console.info('Bot logged in:', bot)
}

bot.on('login', onLogin)
await bot.start()

logout Event

The logout event logs the bot out.

// TODO: Pull Request is welcome!

message Event

The message event notifies you when a new message arrives.

import { Message } from 'wechaty'

function onMessage (message: Message) {
console.info('New message:', message)
}

bot.on('message', onMessage)
await bot.start()

friendship Event: friend requests

The friendship event alerts you when someone sends you a friend request.

// TODO: Pull Request is welcome!

Room Events

room-topic Event: messages

The room-topic event alerts you when someone changes the room topic.

// TODO: Pull Request is welcome!

room-invite Event: messages

The room-invite event alerts you when there is a room invitation.

// TODO: Pull Request is welcome!

room-join Event: messages

The room-join event alerts you when anyone joins the room.

// TODO: Pull Request is welcome!

room-leave Event: messages

The room-leave event alerts you when anyone leaves the room.

// TODO: Pull Request is welcome!

System events

ready Event

The ready event is executed when all data has been loaded successfully.

// TODO: Pull Request is welcome!

heartbeat Event: messages

The Heartbeat event helps to send emojis to a specified contact or group periodically.

// TODO: Pull Request is welcome!

error Event

The error event makes the bot to throw an error whenever an error is encounterd.

// TODO: Pull Request is welcome!