Plugins

About the plugin feature built into OneComme

OneComme (5.2 and later) includes a plugin mechanism that runs on JavaScript (Node.js).

Development --

Using console outputs to plugin.log in the log file.

console.info("Hello OneComme!");

Samples

OneComme sample plugin / template

https://github.com/OneComme/OneCommeOrderSpeechPlugin

OneComme comment filter sample plugin

https://github.com/OneComme/OneCommeFilterSamplePlugin

※ The sample plugins may be freely modified and redistributed

Overall code structure

const plugin = {  
  name: 'Sample Plugin', // @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

Plugin type reference

nameRequiredThe plugin's name
uidRequiredA unique ID for the plugin. It must not overlap with any other plugin's ID
versionRequiredThe plugin's own version number
authorRequiredThe plugin developer's name
urlWhen set, a button to open this link appears on the plugin page in OneComme (link it to your settings or manual page)
permissionsRequiredAn array of the data types used by the plugin
Any data not listed here cannot be retrieved
defaultStateDefines the initial values of the data the plugin keeps
The state is saved and persisted as a JSON file via store
init({ dir, store },initialData):voidRuns when the plugin is enabled, or when OneComme starts while the plugin is enabled
dir: plugin directory path
store: ElectronStore instance
destroy():voidRuns when the plugin is disabled, or when OneComme exits while the plugin is enabled
subscribe(type: SendType, …args: any[])Runs when data of the type specified in permissions is received
※ Arguments after the second one vary by data type
filterComment(comment: Comment, service: Service, userData: UserData): Promise<Comment | boolean>Runs when a comment is received; by returning comment data, you can pass a processed comment back to OneComme
Returning false will drop the comment
※ Requires 'filter.comment' to be specified in permissions
filterSpeech(text: string, userData: UserNameData, config: SpeechConfig, comment?: Comment ): Promise<string | boolean>Runs before speech playback; by returning the speech text, you can have the processed content read aloud
Returning false will stop the speech from being read
※ Requires 'filter.speech' to be specified in permissions
※ Note that comment may be missing in some cases, such as when sent directly via the speech API
request(req: PluginRequest): Promise< PluginResponse >Runs when a request is received on the RestAPI provided for each plugin
Use it to save data to the plugin from a settings screen, or to receive data stored by the plugin
※ See the plugin RestAPI section below

Plugin RestAPI

While a plugin is enabled, OneComme exposes a RestAPI for the plugin.
Requests can be made with GET/POST/PUT/DELETE to http://localhost:11180/api/plugins/$\{PLUGIN_UID\}

Requests are sent to the plugin's request function.

Requester-side sample: https://github.com/OneComme/OneCommeOrderSpeechPlugin/blob/main/static/script.js

Receiver-side sample: https://github.com/OneComme/OneCommeOrderSpeechPlugin/blob/main/src/index.ts