-
1. 后端处理服务器API
- 1.1 数据库结构
- 1.2 后端处理服务器HTTP 接口
- 1.3. 数据包
-
2. 后端处理服务器应用集成API
- 2.1 设置应用集成
- 2.2 API 格式和签名机制
- 2.3 API 定义
- 3. 客户端扩展机制
-
4. 会话机器人开发指南
- 4.1 机器人应用开发
开发插件扩展
- 2020-08-11 11:14:48
- 孙浩
- 6936
- 最后编辑:孙浩 于 2020-08-11 11:22:15
- 分享链接
插件扩展通常不包含具体的界面,但可以在界面初始化及关键事件触发时得到通知并执行代码。例如可以通过监听用户发送消息,并在消息发送之前修改消息的内容。
每一个插件扩展需要提供一个入口模块文件,在package.json文件中通过main属性指定。如果不指定此文件则默认使用扩展包目录的index.js文件作为主入口模块文件。扩展主入口模块文件为一个 JavaScript 模块,当喧喧加载完毕时会逐个加载各个扩展的主入口模块。在扩展主入口模块中可以访问全局扩展对象global.xext。
下面为一个简单插件扩展包目录结构:
helloworld-plugin-example/ ├─ lib/ │ └─ index.js # 扩展主入口模块 └─ package.json # 扩展包描述文件
扩展包内源码文件内容如下:
package.json
{
"name": "helloworld-plugin-example",
"displayName": "插件示例",
"version": "1.0.0",
"description": "此插件用于演示喧喧的插件机制。",
"type": "plugin",
"icon": "mdi-cards-playing-outline",
"accentColor": "#afb42b",
"hot": true,
"main": "lib/index.js",
"author": {
"name": "${author.name}"
},
"publisher": "${company}",
"usesPermissions": [
"commander.registerCommand",
"contextmenu.addContextMenuCreator",
"components.Modal"
]
}
lib/index.js
const xext = require('xext');
// 用于存储计时器标志
let timerTask = null;
module.exports = {
onAttach: (ext) => {
console.log('>> 扩展【helloworld-plugin-example】:扩展加载完成,刚刚加载等扩展名称是:', ext.displayName, ext.version);
},
onReady: (ext) => {
console.log('>> 扩展【helloworld-plugin-example】:界面已准备就绪。', ext.displayName, ext.version);
},
onDetach: (ext) => {
// 扩展将被卸载,此时应该清理计时器
clearTimeout(timerTask);
timerTask = null;
console.log('>> 扩展【helloworld-plugin-example】:扩展已卸载。', ext.displayName, ext.version);
},
onUserLogin: (user, error) => {
// 当用户登录时在此处可以进行相关操作,下面以显示当前登录等结果和用户名为例
if (user && !error) { // 表示登录成功
console.log('>> 扩展【helloworld-plugin-example】:用户登录成功了,用户名称是:', user.displayName);
}
},
onUserLogout: (user) => {
if (user) {
console.log('>> 扩展【helloworld-plugin-example】:用户退出登录了,用户名称是:', user.displayName);
}
},
onUserStatusChange: (status, oldStatus, user) => {
console.log('>> 扩展【helloworld-plugin-example】:用户状态发生变化了', {status, oldStatus, user});
},
onSendChatMessages: (messages, chat, user) => {
console.log('>> 扩展【helloworld-plugin-example】:用户发送了消息', {messages, chat, user});
},
onReceiveChatMessages: (messages, user) => {
console.log('>> 扩展【helloworld-plugin-example】:用户收到了消息', {messages, user});
},
commands: {
saveText: (context, ...params) => {
console.log('保存文本成功');
return 'ok';
}
},
contextMenuCreators: [{
match: 'message.text',
items: [{
icon: 'mdi-emoticon-cool',
label: 'say hello',
click: () => {
xext.components.Modal.alert('hello');
}
}, {
icon: 'mdi-earth',
label: '访问禅道',
url: 'http://zentao.net'
}]
}, {
match: 'chat.sendbox.toolbar',
create: context => {
return [{
icon: 'mdi-star',
label: {'zh-cn': '说你好', en: 'say-hello'},
click: () => {
context.sendContent('Hello!');
}
}];
}
}]
};
此扩展实例可以在 helloworld-plugin-example.zip 获取,更多相关例子:
- bing-translate.zip
- [youdao.zip
评论列表
发表评论
联系我们
社群交流
微信公众号
QQ群

实际用不了nodejs的内置模块,或者有无教程?