未验证 提交 2f7b6414 编写于 作者: G Gcaufy 提交者: GitHub

feat: added wechaty plugin (#1946)

* feat: added wechaty plugin

* 0.39.14

* fix: remove plugins & change to functional plugin & added test cases

* 0.39.15

* fix: varible name changes & no this & Test class extends

* fix: varible name changes & no this & Test class extends

* fix typo

* 0.39.19
Co-authored-by: Huan (李卓桓)'s avatarHuan (李卓桓) <zixia@zixia.net>
上级 769921d9
{
"name": "wechaty",
"version": "0.39.18",
"version": "0.39.19",
"description": "Wechaty is a Bot SDK for Individual Account, Powered by TypeScript, Docker, and 💖",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
......
......@@ -149,6 +149,43 @@ test('on(event, Function)', async t => {
})
test('use plugin', async (t) => {
// Do not modify the gloabl Wechaty instance
class MyWechatyTest extends Wechaty {}
let result = ''
const myGlobalPlugin = function () {
return function (bot: Wechaty) {
bot.on('message', () => (result += 'FROM_GLOBAL_PLUGIN:'))
}
}
const myPlugin = function () {
return function (bot: Wechaty) {
bot.on('message', () => (result += 'FROM_MY_PLUGIN:'))
}
}
MyWechatyTest.use(myGlobalPlugin())
const bot = new MyWechatyTest({
puppet: new PuppetMock(),
})
bot.use(myPlugin())
bot.on('message', () => (result += 'FROM_BOT'))
bot.emit('message', {} as any)
await bot.stop()
t.ok(result === 'FROM_GLOBAL_PLUGIN:FROM_MY_PLUGIN:FROM_BOT')
})
test('initPuppetAccessory()', async t => {
const wechatyTest = new WechatyTest()
......
......@@ -114,6 +114,10 @@ export interface WechatyOptions {
ioToken? : string, // Io TOKEN
}
export interface WechatyPlugin {
(bot: Wechaty): void;
}
const PUPPET_MEMORY_NAME = 'puppet'
/**
......@@ -152,6 +156,8 @@ export class Wechaty extends EventEmitter implements Sayable {
*/
private static globalInstance: Wechaty
private static globalPluginList: WechatyPlugin[] = []
private memory?: MemoryCard
private lifeTimer? : NodeJS.Timer
......@@ -202,6 +208,31 @@ export class Wechaty extends EventEmitter implements Sayable {
return this.globalInstance
}
/**
* @param {WechatyPlugin[]} plugins - The plugins you want to use
*
* @return {Wechaty} - this for chaining,
*
* @desc
* For wechaty ecosystem, allow user to define a 3rd party plugin for the all wechaty instances
*
* @example
* // Report all chat message to my server.
*
* function WechatyReportPlugin(options: { url: string }) {
* return function (this: Wechaty) {
* this.on('message', message => http.post(options.url, { data: message }))
* }
* }
*
* bot.use(WechatyReportPlugin({ url: 'http://somewhere.to.report.your.data.com' })
*/
public static use (
...plugins: WechatyPlugin[]
) {
this.globalPluginList = this.globalPluginList.concat(plugins)
}
/**
* The term [Puppet](https://github.com/wechaty/wechaty/wiki/Puppet) in Wechaty is an Abstract Class for implementing protocol plugins.
* The plugins are the component that helps Wechaty to control the Wechat(that's the reason we call it puppet).
......@@ -282,6 +313,8 @@ export class Wechaty extends EventEmitter implements Sayable {
// No need to set puppet/wechaty, so do not clone
this.UrlLink = UrlLink
this.MiniProgram = MiniProgram
this.installGloablPlugin()
}
/**
......@@ -521,6 +554,27 @@ export class Wechaty extends EventEmitter implements Sayable {
return this
}
/**
* @param {WechatyPlugin[]} plugins - The plugins you want to use
*
* @return {Wechaty} - this for chaining,
*
* @desc
* For wechaty ecosystem, allow user to define a 3rd party plugin for the current wechaty instance.
*
* @example
* // The same usage with Wechaty.use().
*
*/
public use (...plugins: WechatyPlugin[]) {
plugins.forEach(plugin => plugin(this))
return this
}
private installGloablPlugin () {
(this.constructor as typeof Wechaty).globalPluginList.forEach(plugin => plugin(this))
}
private addListenerModuleFile (event: WechatyEventName, modulePath: string): void {
const absoluteFilename = callerResolve(modulePath, __filename)
log.verbose('Wechaty', 'addListenerModuleFile() hotImport(%s)', absoluteFilename)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册