プラグイン

わんコメ搭載のプラグイン機能について

わんコメ(5.2以上)にはJavaScript(Node.js)で動作するプラグイン機構を搭載しています

開発 --

consoleを用いることで、ログファイルのplugin.logとして出力されます。

console.info("Hello OneComme!");

サンプル

わんコメ サンプルプラグイン 兼 テンプレート

https://github.com/OneComme/OneCommeOrderSpeechPlugin

わんコメ コメントフィルタサンプルプラグイン

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: 'わんコメ', // @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設定するとわんコメのプラグインページからこのリンクを開くボタンが表示されます(設定やマニュアルページへリンクしてください)
permissions必須プラグインで使用するデータタイプを配列で記載します <br />ここに記載のないデータは取得することができません
defaultStateプラグインとして保持するデータの初期値を定義します <br />stateはstoreを介してjsonファイルとして保存、永続化されます
init({ dir, store },initialData):voidプラグインが有効化されたとき、有効化状態でわんコメが起動したときに実行されます <br />dir: プラグインディレクトリパス <br />store: ElectronStoreインスタンス
destroy():voidプラグインが無効化されたとき、有効化状態でわんコメが終了するときに実行されます
subscribe(type: SendType, …args: any[])permissionsで指定したデータを受信したときに実行されます <br />※ 第2引数以降はデータタイプによって変わります
filterComment(comment: Comment, service: Service, userData: UserData): Promise<Comment | boolean>コメント受信時に実行され、コメントデータを返却することで加工されたコメントなどをわんコメに返すことができます <br />falseを返却した場合にはコメントが間引かれます <br />※ permissionsで ‘filter.comment’ の指定が必要です
filterSpeech(text: string, userData: UserNameData, config: SpeechConfig, comment?: Comment ): Promise<string | boolean>読み上げ前に実行され、読み上げ文字列を返却することで加工された内容で読み上げることができます <br />falseを返却した場合には読み上げされなくなります <br />※ permissionsで ‘filter.speech’ の指定が必要です <br />※ 読み上げ用のAPIで直接送られた場合などcommentが含まれない場合がありますのでご注意ください
request(req: PluginRequest): Promise< PluginResponse >各プラグインに用意されるRestAPIへのリクエストを受信したときに実行されます <br />設定画面などからプラグインにデータを保存したり、プラグインに保存されているデータを受信したりする用途で使用します <br />※ 後述のプラグインRestAPIを参照ください

プラグインRestAPI

プラグインが有効化状態の場合、わんコメからプラグインへ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