플러그인

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필수플러그인에서 사용할 데이터 타입을 배열로 기재합니다
여기에 기재되지 않은 데이터는 가져올 수 없습니다
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