ปลั๊กอิน

เกี่ยวกับฟีเจอร์ปลั๊กอินที่มาพร้อมกับ OneComme

OneComme (เวอร์ชัน 5.2 ขึ้นไป) มีกลไกปลั๊กอินที่ทำงานด้วย JavaScript (Node.js)

การพัฒนา --

การใช้ console จะทำให้ข้อมูลถูกส่งออกไปยัง plugin.log ในไฟล์ 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จำเป็นระบุประเภทข้อมูลที่ปลั๊กอินใช้งานในรูปแบบ array
ข้อมูลที่ไม่ได้ระบุไว้ที่นี่จะไม่สามารถรับได้
defaultStateกำหนดค่าเริ่มต้นของข้อมูลที่ปลั๊กอินจะเก็บไว้
state จะถูกบันทึกเป็นไฟล์ json ผ่าน store และคงอยู่ถาวร
init({ dir, store },initialData):voidทำงานเมื่อปลั๊กอินถูกเปิดใช้งาน หรือเมื่อ OneComme เริ่มทำงานขณะปลั๊กอินอยู่ในสถานะเปิดใช้งาน
dir: เส้นทางไดเรกทอรีของปลั๊กอิน
store: instance ของ 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 ให้กับปลั๊กอิน
สามารถส่งคำขอแบบ GET/POST/PUT/DELETE ไปยัง http://localhost:11180/api/plugins/$\{PLUGIN_UID\} ได้

คำขอจะถูกส่งไปยังฟังก์ชัน request ฝั่งปลั๊กอิน

ตัวอย่างฝั่งที่ส่งคำขอ: https://github.com/OneComme/OneCommeOrderSpeechPlugin/blob/main/static/script.js

ตัวอย่างฝั่งที่รับคำขอ: https://github.com/OneComme/OneCommeOrderSpeechPlugin/blob/main/src/index.ts