Wechaty Plugin Contrib Package for the Community
Image Credit: What is Plugin
When you find yourself writing repetitive code, it's time to extract it into a plugin.
Wechaty has a great support for using Plugins by calling Wechaty.use(WechatyPlugin())
. A Wechaty Plugin is a JavaScript function that returns a function which accepts a Wechaty instance.
The first Wechaty Plugin system was design by our core team developer @gcaufy from issue #1939(Wechaty Plugin Support with Kick out Example) to PR #1946(feat: added wechaty plugin).
This package is for publishing the Wechaty Plugins that are very common used by the core developer team.
You are welcome to send your plugin to our contrib by creating a Pull Request!
# | Plugin | Author | Feature |
---|---|---|---|
1 | DingDong | @huan | Reply dong if bot receives a ding message. |
2 | EventLogger | @huan | Log Wechaty Events for 'scan' \| 'login' \| 'message' ... etc. |
3 | QRCodeTerminal | @huan | Show QR Code for Scan in Terminal |
4 | Heartbeat | @huan | Send emoji periodically |
5 | ChatOps | @huan | Forward DM & Mention messages to a room |
6 | RoomConnector | @huan | Connect rooms together with 1:N , M:1 , and M:N modes |
7 | FriendshipAccepter | @huan | Accept friendship automatically, and say/do something for greeting. |
8 | RoomInviter | @huan | Invite user to rooms by keyword |
9 | EventHotHandler | @huan | Hot reloading event handler module files |
10 | RoomInvitationAccepter | @huan | Automatically accepting any room invitations |
11 | MessageAwaiter | @ssine | Wait for a particular message using await syntax #13 |
dong
if bot receives a ding
message.import { DingDong } from 'wechaty-plugin-contrib'
const config = {
mention : true, // default: true - Response to Mention Self (@/at) Message in Room
contact : true, // default: true - Response to Direct Message
room : true, // default: true - Response to Rooms Message
self : true, // default: true - Response to Message that send from the bot itself
}
wechaty.use(DingDong(config))
config
as a Functionconfig
can also be a function which receives a message: Message
and returns a boolean
result to decide whether response a ding
message.
Config: (message: Message) => boolean | Promise<boolean>
"dong" | "message" | "error" | "friendship" | "heartbeat" | "login" | "logout" | "ready" | "reset" | "room-invite" | "room-join" | "room-leave" | "room-topic" | "scan"
import { EventLogger } from 'wechaty-plugin-contrib'
const config = ['login', 'ready', 'message']
// Do not provide an config will log all events.
wechaty.use(EventLogger(config))
import { QRCodeTerminal } from 'wechaty-plugin-contrib'
const config = {
small: false, // default: false - the size of the printed QR Code in terminal
}
wechaty.use(QRCodeTerminal(config))
import { Heartbeat } from 'wechaty-plugin-contrib'
const config = {
contact: 'filehelper', // default: filehelper - Contact id who will receive the emoji
emoji: {
heartbeat: '[爱心]', // default: [爱心] - Heartbeat emoji
},
intervalSeconds: 60 * 60, // Default: 1 hour - Send emoji for every 1 hour
}
wechaty.use(Heartbeat(config))
import { ChatOps } from 'wechaty-plugin-contrib'
const config = {
room : 'xxx@chatroom', // required: room id for ChatOps
mention? : true, // default: true - Response to Mention Self (@/at) Message in Room
contact? : true, // default: true - Response to Direct Message
whitelist?: ChatOpsFilter, // whitelist for messages that allow to send to ChatOps Room
blacklist?: ChatOpsFilter, // blacklist for messages that forbidden to send to ChatOps Room
}
wechaty.use(ChatOps(config))
RoomConnector
(s)Connect rooms together, it supports three modes:
1:N
- OneToManyRoomConnector
can broadcast the messages in one room to others.M:1
- ManyToOneRoomConnector
can summary messages from rooms into one room.M:M
- ManyToManyRoomConnector
will broadcast every message between rooms.M:N
- SourceToTargetRoomConnector
will broadcast every message from source room(s) to target room(s).The difference between SourceToTargetRoomConnector
and ManyToManyRoomConnector
is that:
SourceToTargetRoomConnector
have two options to specify:
source
: use RoomMatcherOptions
to specify the source roomstarget
: use RoomFinderOptions
to specify the target roomsManyToManyRoomConnector
have one option to specify:
many
: use string[]
as list of room ids to broadcast toOneToManyRoomConnector()
import { OneToManyRoomConnector, OneToManyRoomConnectorConfig } from 'wechaty-plugin-contrib'
const config: OneToManyRoomConnectorConfig = {
blacklist: [ async () => true ],
many: [
'20049383519@chatroom', // 小句子测试
'5611663299@chatroom', // 'ChatOps - Mike BO'
],
map: async message => message.talker().name() + '(one to many): ' + message.text(),
one: '17237607145@chatroom', // PreAngel 动态
whitelist: [ async message => message.type() === Message.Type.Text ],
}
wechaty.use(OneToManyRoomConnector(config))
ManyToOneRoomConnector()
import { ManyToOneRoomConnector, ManyToOneRoomConnectorConfig } from 'wechaty-plugin-contrib'
const config: ManyToOneRoomConnectorConfig = {
blacklist: [ async () => true ],
many: [
'20049383519@chatroom', // 小句子测试
'5611663299@chatroom', // 'ChatOps - Mike BO'
],
map: async message => message.talker().name() + '(many to one): ' + message.text(),
one: '17237607145@chatroom', // PreAngel 动态
whitelist: [ async message => message.type() === Message.Type.Text ],
}
wechaty.use(ManyToOneRoomConnector(config))
ManyToManyRoomConnector()
import { ManyToManyRoomConnector, ManyToManyRoomConnectorConfig } from 'wechaty-plugin-contrib'
const config: ManyToManyRoomConnectorConfig = {
blacklist: [ async () => true ],
many: [
'20049383519@chatroom', // 小句子测试
'5611663299@chatroom', // 'ChatOps - Mike BO'
],
map: async message => message.talker().name() + '(many to many): ' + message.text(),
whitelist: [ async message => message.type() === Message.Type.Text ],
}
wechaty.use(ManyToManyRoomConnector(config))
SourceToTargetRoomConnector()
import { SourceToTargetRoomConnector, SourceToTargetRoomConnectorConfig } from 'wechaty-plugin-contrib'
const config: SourceToTargetRoomConnectorConfig = {
blacklist: [ async () => true ],
source: [
'5611663299@chatroom',
/source room topic/i,
],
target: [
'20049383519@chatroom',
/target room topic/i,
],
map: async message => message.talker().name() + '(source to target): ' + message.text(),
whitelist: [ async message => message.type() === Message.Type.Text ],
}
wechaty.use(SourceToTargetRoomConnector(config))
Accept friendship automatically, and say/do something for greeting.
import { FriendshipAccepter, FriendshipAccepterConfig } from 'wechaty-plugin-contrib'
const config: FriendshipAccepterConfig = {
greeting: 'we are friends now!',
keyword: '42',
}
wechaty.use(FriendshipAccepter(config))
greeting
will be sent after the friendship has been accepted.keyword
if set, the friendship must match the keyword
text.Invite a contact to the room with password
, welcome
(public message), and rule
(private message) options supported.
import { RoomInviter, RoomInviterConfig } from 'wechaty-plugin-contrib'
const config: RoomInviterConfig = {
password : 'wechaty',
room : '18171595067@chatroom',
welcome : 'Welcome to join the room!',
rule : 'Please be a good people',
repeat : 'You have already in our room',
}
wechaty.use(RoomInviter(config))
Hot reloading event handler module files.
import { EventHotHandler, EventHotHandlerConfig } from 'wechaty-plugin-contrib'
const config: EventHotHandlerConfig = {
login: './handlers/on-login',
logout: './handlers/on0-logout',
}
wechaty.use(EventHotHandler(config))
Automatically accepting any room invitations.
import { RoomInvitationAccepter } from 'wechaty-plugin-contrib'
wechaty.use(RoomInvitationAccepter())
await
syntax (await messagePrompter(message)(...)
).import { messageAwaiter } from 'wechaty-plugin-contrib'
wechaty.on('message' async (message) => {
const prompter = messagePrompter(message)
const reply = prompter('Hello')
if (reply) {
await message.say('hint message')
// do anything you want...
}
})
Other arguments include regex
which is tested on the message andtimeoutSecond
which automatically rejects the dialog after specified seconds.
Learn more from:
The Wechaty Plugin Contrib will only accept simple plugins which does not dependence very heavy NPM modules, and the SLOC (Source Line Of Code) is no more than 100.
There are many great Wechaty Plugins can not be included in the contrib because they are too powerful. They will be published as a NPM by itself.
We are listing those powerful Wechaty Plugins outside the contrib as in the following list, and you are welcome to add your plugin below if you have published any!
SourceToTargetRoomConnector
to connect a source room to a target room by forward messages to target room.EventHotHandler
due to ESMMessageAwaiter
plugin to messagePrompter
helper function. (#60)types.SayableMessage
and types.toSayableMessage
dm
renamed to contact
at
renamed to mention
talkers.*
, finders.*
, and matchers.*
mappers.messageMapper()
matcher.languageMatcher()
Add more helper utility functions.
RoomMatcher
, ContactMatcher
, MessageMatcher
RoomTalker
, ContactTalker
, MessageTalker
RoomFinder
, ContactFinder
OneToManyRoomConnector
, ManyToOneRoomConnector
, and ManyToManyRoomConnector
.FriendshipAccepter
for setting to accept friendship automatically.RoomInviter
for invite user to rooms with password
, rule
, and welcome
options support.EventHotHandler
for hot reloading event handler module files.ChatOps
: forward all DM & Mention messages to a Room for logging.Added the following Wechaty Plugins:
The wechaty-plugin-contrib
project was kicked off by the issue Wechaty Plugin Support with Kickout Example #1939 and the PR feat: added wechaty plugin #1946.