擴充功能
關於 OneComme 內建的擴充功能
OneComme(5.2 以上)內建了以 JavaScript(Node.js)運作的擴充功能機制
開發 --
使用 console 時,會輸出到記錄檔的 plugin.log 中
console.info("Hello OneComme!");
範例
OneComme 範例擴充功能(兼模板)
https://github.com/OneComme/OneCommeOrderSpeechPlugin
OneComme 留言過濾範例擴充功能
https://github.com/OneComme/OneCommeFilterSamplePlugin
※ 範例擴充功能可自由修改、再發布
程式碼總覽
const plugin = {
name: '範例擴充功能', // @required plugin name
uid: 'com.onecoome.sampleplugin', // @required unique plugin id
version: '0.0.1', // @required semver version
author: 'OneComme', // @required author name
url: 'https://onecomme.com', // @optional link (ex. documentation link)
permissions: ['comments'], // @required https://onecomme.com/docs/developer/websocket-api/#%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E3%81%AE%E7%A8%AE%E9%A1%9E%E3%81%A8%E3%83%87%E3%83%BC%E3%82%BF
defaultState: { // @optional key-value custom state
count: 0
},
/**
*
* @param { dir: string, filepath: string, store: ElectronStore} param
* dir: plugin directory path
* filepath: this script's path
* store: ElectronStore Instance https://github.com/sindresorhus/electron-store?tab=readme-ov-file#instance
*/
init({ dir, store }, initialData) {},
/**
* called on exit or when activated
* @optional
*/
destroy() {},
/**
* called when the event specified in permissions occurs ( exclude connected event )
* @optional
* https://onecomme.com/docs/developer/websocket-api
*/
subscribe(type, ...args) {
switch (type) {
case 'comments': {
}
}
},
/**
* filter comment
* @param {Comment} Comment
* @param {Service} Service
* @param {UserNameData | null} UserData
* @returns Promise
*/
filterComment(comment, service, userData) {
if (comment.service === 'sample') return false
return comment
},
/**
* filter speech
* @param {string} text
* @param {UserNameData | null} userData
* @param {SpeechConfig} config
* @param optional {Comment} comment
* @returns Promise
*/
filterSpeech(text, userData, config, comment) {
if (!userData) return false
return text
},
/**
* called when a request is made to the plugin-specific RestAPI
* @param {
* url: string // request url
* method: 'GET' | 'POST' | 'PUT' | 'DELETE'
* params: {[key: string]: string} // querystrings
* body?: any // request body
* } req
* @returns {
* code: number // status code
* response: Object or Array // response data
* }
*/
async request(req: PluginRequest) {
// [GET, POST, PUT, DELETE]
// endpoint: localhost:11180/api/plugins/com.onecomme.plugin-sample
switch (req.method) {
case 'GET': {}
case 'POST': {}
case 'PUT': {}
case 'DELETE': {}
}
return {
code: 404,
response: {}
}
}
}
module.exports = plugin
| name | 必填 | 擴充功能的名稱 |
| uid | 必填 | 擴充功能專屬的 ID,必須是不與其他擴充功能重複的唯一 ID |
| version | 必填 | 擴充功能本身的版本號碼 |
| author | 必填 | 擴充功能開發者名稱 |
| url | 設定後,會在 OneComme 的擴充功能頁面顯示開啟此連結的按鈕(請連結到設定或使用說明頁面) | |
| permissions | 必填 | 以陣列列出擴充功能所使用的資料類型 未列在此處的資料將無法取得 可指定的值與可接收資料的清單請參考模板用 SDK 的 Permission(comments、reactions、events 等) 除此之外,還可以指定擴充功能專用的 'filter.comment'、'filter.speech' |
| defaultState | 定義擴充功能所保存資料的初始值 state 會透過 store 以 json 檔案的形式儲存並持久化 | |
| init({ dir, store },initialData):void | 擴充功能被啟用時,或在啟用狀態下啟動 OneComme 時執行 dir: 擴充功能目錄路徑 store: ElectronStore 實例 | |
| destroy():void | 擴充功能被停用時,或在啟用狀態下結束 OneComme 時執行 | |
| subscribe(type: SendType, …args: any[]) | 接收到 permissions 中指定的資料時執行 ※ 第二個參數之後會依資料類型而異 | |
| filterComment(comment: Comment, service: Service, userData: UserData): Promise<Comment | boolean> | 接收到留言時執行,透過回傳留言資料,可將加工後的留言等內容回傳給 OneComme 若回傳 false,該則留言會被過濾掉 ※ 需在 permissions 中指定 'filter.comment' | |
| filterSpeech(text: string, userData: UserNameData, config: SpeechConfig, comment?: Comment ): Promise<string | boolean> | 在朗讀前執行,透過回傳朗讀文字字串,可用加工後的內容進行朗讀 若回傳 false,則不會進行朗讀 ※ 需在 permissions 中指定 'filter.speech' ※ 若是透過朗讀用的 API 直接送出等情況,可能不包含 comment,請注意 | |
| request(req: PluginRequest): Promise< PluginResponse > | 接收到各擴充功能所提供的 RestAPI 請求時執行 用於從設定畫面等將資料儲存至擴充功能,或接收擴充功能中儲存的資料等用途 ※ 請參考後述的擴充功能 RestAPI |
擴充功能 RestAPI
擴充功能為啟用狀態時,OneComme 會提供 RestAPI 供擴充功能使用
可對 http://localhost:11180/api/plugins/$\{PLUGIN_UID\} 進行 GET/POST/PUT/DELETE 請求
請求會被傳送至擴充功能端的 request 函式
請求端範例: https://github.com/OneComme/OneCommeOrderSpeechPlugin/blob/main/static/script.js
接收端範例: https://github.com/OneComme/OneCommeOrderSpeechPlugin/blob/main/src/index.ts