Wechaty Puppet Providers
The Wechaty community builds separate RPA modules for different instant messaging (IM) systems (such as WeChat, Whatsapp, and TikTok). These modules are called Wechaty Puppet.
The Wechaty Puppet Provider which is supporting WeChat is named wechaty-puppet-wechat. The puppet provider supporting Whatsapp is named wechaty-puppet-whatsapp, and supporting Lark is named wechaty-puppet-lark.
Wechaty Puppet Provider is an NPM module for a specific IM protocol Wechaty Puppet. Run specific Wechaty puppet using the following command :
npm install wechaty-puppet-NAME
export WECHATY_PUPPET=wechaty-puppet-NAME
npm start
You can switch between different Wechaty Puppet Provider by changing your
WECHATY_PUPPET
environment variable. All your source code should work between different tokens without any changes.
Wechaty Puppet Providers
Presently, we have the following Wechaty Puppet Providers:
Name | Platform | Protocol | Stable |
---|---|---|---|
Web | Beta | ||
Web | Alpha | ||
Official Account | WeChat Official Account | API | Alpha |
Gitter | Gitter | API | Alpha |
Lark | Lark | API | Alpha |
PadLocal | Pad | Beta | |
WeChat4U | Web | Alpha | |
XP | Windows | Alpha | |
Service | Universal | gRPC | Beta |
Mock | Universal | Mock | Beta |
Using Wechaty Puppet Providers
Let’s see a code example (in TypeScript) of how to change Wechaty Puppet Providers.
We have the following six lines code, which is the world’s shortest chatbot:
// bot.ts
import { Wechaty } from 'wechaty'
Wechaty.instance() // Global Instance
.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()
The above code will use the default Wechaty Puppet Provider (which is wechaty-puppet-wechat) because we have not specified manually.
If we want this bot to serve on Whatsapp, we need to specify a Wechaty Puppet Provider for Whatsapp.
We have two methods to specify Wechaty Puppet Providers for our program:
- Using the WECHATY_PUPPET environment variable.
- Import the Wechaty Puppet manually in code.
Using the WECHATY_PUPPET environment variable
The WECHATY_PUPPET environment will be used as the Wechaty Puppet Provider NPM name, and then you are all set.
export WECHATY_PUPPET=wechaty-puppet-whatsapp
ts-node bot.ts
The above shell commands set the wechaty-puppet-whatsapp
to the environment variable WECHATY_PUPPET
and our Wechaty system will use it.
Importing the Wechaty Puppet manually in code
On the other hand, you can import the Wechaty Puppet Provider NPM module directly in your code and then add an option when instantiating the Wechaty with the object name puppet:
// bot.ts
import { Wechaty } from 'wechaty'
+ import { PuppetWhatsapp } from 'wechaty-puppet-whatsapp'
- Wechaty.instance() // Global Instance
+ Wechaty.instance({ puppet: new PuppetWhatsapp() })
.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()
In the above code, Wechaty will use PuppetWhatsapp from the wechaty-puppet-whatsapp module.
History
- Wechaty Puppet Providers Trends, Huan, Mar 4, 2021
- Introducing Wechaty Puppet Service (Providers), @huan, Jan 14, 2021
See Also
If you want to learn more about the concepts behind Wechaty Puppet Provider, you can read:
For a deeper understanding of the Puppet in Wechaty, you can read its source code from https://github.com/wechaty/wechaty-puppet/blob/main/src/puppet.ts