ปลั๊กอิน
เกี่ยวกับฟีเจอร์ปลั๊กอินที่ติดตั้งมากับ 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 | จำเป็น | ระบุประเภทข้อมูลที่ปลั๊กอินใช้งานในรูปแบบอาเรย์ ข้อมูลที่ไม่ได้ระบุไว้ที่นี่จะไม่สามารถรับได้ สำหรับรายการค่าที่สามารถระบุได้และข้อมูลที่สามารถรับได้ ให้ดูที่ Permission ของ SDK สำหรับเทมเพลต (เช่น comments, reactions, events) นอกจากนี้ยังสามารถระบุ 'filter.comment' และ 'filter.speech' ซึ่งเป็นค่าเฉพาะสำหรับปลั๊กอินได้ด้วย |
| defaultState | กำหนดค่าเริ่มต้นของข้อมูลที่ปลั๊กอินเก็บไว้ state จะถูกบันทึกและคงอยู่ถาวรในรูปแบบไฟล์ json ผ่าน store | |
| 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 คอมเมนต์นั้นจะถูกคัดกรองออก ※ ต้องระบุ 'filter.comment' ใน permissions | |
| filterSpeech(text: string, userData: UserNameData, config: SpeechConfig, comment?: Comment ): Promise<string | boolean> | ทำงานก่อนการอ่านออกเสียง สามารถให้อ่านออกเสียงเนื้อหาที่ผ่านการแก้ไขได้โดยการคืนค่าข้อความสำหรับอ่านออกเสียง หากคืนค่า false จะไม่มีการอ่านออกเสียง ※ ต้องระบุ 'filter.speech' ใน permissions ※ โปรดทราบว่าในบางกรณี เช่น เมื่อส่งข้อมูลโดยตรงผ่าน 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