Skip to main content

Event Logger plugin

Powered by Wechaty Wechaty Plugin Contrib TypeScript

The EventLogger plugin helps in logging Wechaty events with just one line of code. In this tutorial, you will learn how to add the EventLogger plugin to a Wechaty bot.

Requirements

  1. Node.js v16+
  2. Wechaty v0.40+
  3. Wechaty Plugin Contrib

Getting started

You will require Node.js version 16.0 or greater in order to follow this tutorial. You can verify whether Node.js is installed on your system or whether you have the correct version using the command:

node -v

If you do not have Node.js installed or your version is below requirement, get 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.

Adding Event Logger plugin

For the demonstration of adding this plugin, we will use the Starter Bot and show you how to add the EventLogger plugin to it. Follow the steps below:

1. Create a starter bot

Follow the instructions on the Starter Bot page to create the foundation of a Wechaty bot.

2. Install dependency

As the EventLogger plugin is present in the wechaty-plugin-contrib NPM package, you have to first add it to the dependencies. It can be installed using the following command:

npm i wechaty-plugin-contrib

3. Integrate the plugin

Inside the event-logger-bot.ts file, import the plugin:

import { EventLogger } from 'wechaty-plugin-contrib'

Now, just before starting the bot, you can use this plugin:

// Add the following line to use the plugin
bot.use(EventLogger())
bot.start()

4. Run the bot

In order to run the bot, first you have to export/set an environment variable with the type of puppet to use, and then start the bot:

export WECHATY_LOG=verbose
export WECHATY_PUPPET=wechaty-puppet-wechat
make bot
# the above is equals to the below command:
# npm start
# or, npx ts-node examples/ding-dong-bot.ts

EventLogger plugin output

Congratulations! You have successfully integrated the EventLogger plugin to your Wechaty bot. You will see after running the bot, it starts logging the Wechaty events.

Conclusion

You can apply a similar concept to add the EventLogger plugin to any of your Wechaty bots. You can learn more about this plugin here.

Learn how to integrate QR Code Terminal Plugin to your Wechaty bot in the next tutorial.

References