跳到主要内容

QR Code Terminal plugin

Powered by Wechaty Wechaty Plugin Contrib TypeScript

QR code terminal plugin shows QR Code for scanning in terminal. In this tutorial you will learn how to integrate this plugin with 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 QR code terminal plugin

For adding this plugin, refer to Starter Bot for building a basic bot and then to add the QR code terminal 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. Add dependency

For using any plugin, you have to add wechaty-plugin-contrib NPM package to the dependencies using the following command:

npm i wechaty-plugin-contrib

3. Integrate the plugin

Inside bot.ts file, import the plugin:

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

Define a config variable which can be used while starting the bot to make the QR code printed on terminal small.

const config = {
small: true, // default: false - the size of the printed QR Code in terminal
}

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

bot.use(QRCodeTerminal(config))
bot.start()

4. Run the bot

Now, you are done with the integration of QR code terminal plugin with your bot. For running the bot you have to export/set an environment variable with the type of puppet to use and then run the bot.

export WECHATY_LOG=verbose
export WECHATY_PUPPET=wechaty-puppet-wechat
# If you want to use WhatsApp
# export WECHATY_PUPPET=wechaty-puppet-whatsapp
npm start

Congratulations! you have successfully added QR code terminal plugin to your bot. On running the bot it will show a QR code on terminal.

QR code terminal plugin output

Conclusion

You can use this QR code terminal plugin for building the bots where you have to connect to a device. You can refer to Ding Dong bot. In ding dong bot you have to scan the generated QR code to connect to Whatsapp/Wechat.

References