Slack: Slack メッセージ JSON パース

Slack: Slack メッセージ JSON パース

Slack: Slack Message JSON Parse

Slack より受信した Message JSON データからテキストを抜き出す。添付ファイルがある場合はファイルをダウンロードする。ただし添付ファイルは1つのみ対応。

Auto Step icon
Configs for this Auto Step
conf_Json
C1: Slack から受信した Message JSON が格納されている文字型データ項目 *
conf_Text
C2: Slack メッセージのテキストを保存する文字型データ項目 (更新) *
conf_Url
C3: Slack メッセージの画面 URL を保存する文字型データ項目 (更新)
conf_ChannelId
C4: Slack メッセージの投稿チャンネル ID を保存する文字型データ項目 (更新)
conf_Ts
C5: Slack メッセージのスレッド ts 値を保存する文字型データ項目 (更新)
conf_OAuth2
C6: OAuth2 設定(添付ファイルダウンロード向け)
conf_Attachment
C7: Slack メッセージに添付されたファイルを保存するファイル型データ項目 (更新)
Script (click to open)

main();
function main() {

  //// == Config Retrieving / 工程コンフィグの参照 ==
  const json = engine.findDataByNumber( configs.get("conf_Json") ) + "";

  const dataId_text = configs.get( "conf_Text" );
  const dataId_url  = configs.get( "conf_Url" );
  const dataId_channelId  = configs.get( "conf_ChannelId" );
  const dataId_ts  = configs.get( "conf_Ts" );

  const oauth2 = configs.get("conf_OAuth2");
  const dataId_attachment = configs.get( "conf_Attachment" );
  let processFiles;
  if ( dataId_attachment !== "" ) {
    processFiles = engine.findDataByNumber( dataId_attachment );
    // java.util.ArrayList <com.questetra.bpms.core.event.scripttask.QfileView>
  }
  if (processFiles === null) {
    processFiles = new java.util.ArrayList();
  }

  //// == Calculating / 演算 ==
  const jsonObj = JSON.parse( json );
  //JSONがない・JSONにeventがない場合はエラー
  if ( !( jsonObj ) ) {
    throw `JSON data none`;
  }
  if ( !( 'event' in jsonObj ) ) {
    throw `json.event none`;
  }

  const text    = jsonObj.event.text + "";

  const teamId    = jsonObj.team_id + "";
  const channelId = jsonObj.event.channel + "";
  const ts        = jsonObj.event.event_ts + "";
  const messageUrl = "https://app.slack.com/client/" + teamId + "/" + channelId + "/" + ts + "/"


  //// == Data Retrieving / ワークフローデータへの代入 == 
  engine.setDataByNumber( dataId_text, text );
  if ( dataId_url !== "" ) {
    engine.setDataByNumber( dataId_url, messageUrl );
  }
  if ( dataId_channelId !== "" ) {
    engine.setDataByNumber( dataId_channelId, channelId );
  }
  if ( dataId_ts !== "" ) {
    engine.setDataByNumber( dataId_ts, ts );
  }

  if ( dataId_attachment !== "" ) {
    if ( jsonObj.event.files ) {
      const fileUrl = jsonObj.event.files[0].url_private_download + "";
      engine.log("fileUrl:" + fileUrl);
      const fileName = jsonObj.event.files[0].name + "";
      engine.log("fileName:" + fileName);

      const response = accessToTheUrl( oauth2, fileUrl );
      const qfile = saveFile( fileName, response );
      updateData( processFiles, dataId_attachment, qfile );
    }
  }
}

/**
  * ダウンロードファイルの URL に GET リクエストを送信し、ファイルを取得する
  * @param {String} oauth2
  * @param {String} fileUrl  ダウンロードファイルの URL
  * @return {HttpResponseWrapper} response  レスポンス
  */
function accessToTheUrl( oauth2, fileUrl ) {
  let response;
  
  try {
    let httpRequest = httpClient.begin()
    if ( oauth2 !== "" && oauth2 !== null ) {
      httpRequest = httpRequest.authSetting(oauth2);
    }
    response = httpRequest.get( fileUrl );
  } catch (e) {
    throw `Unable to access ${fileUrl}.`;
  }

  const httpStatus  = response.getStatusCode();
  engine.log( `STATUS: ${httpStatus}` );
  if (httpStatus >= 300) {
    engine.log( response.getResponseAsString() );
    throw `Failed to download. STATUS: ${httpStatus}`;
  }

  return response;
}


/**
  * ダウンロードしたファイルを名前を付けて保存する
  * @param {String} name  保存する際のファイル名
  * @param {HttpResponseWrapper} response  レスポンス
  * @return {Qfile} qfile  ファイル
  */
function saveFile( name, response ) {
  const qfile = new com.questetra.bpms.core.event.scripttask.NewQfile(
    name, response.getContentType(), response.getResponse()
  );
  return qfile;
}

/**
  * ダウンロードしたファイルをデータ項目に出力する
  * @param {Array<Qfile>} processFiles  ファイルの配列
  * @param {String} dataId  ダウンロードファイルを追加保存するデータ項目のデータ定義番号
  * @param {Qfile} qfile  ファイル
  */
function updateData( processFiles, dataId, qfile ) {
  processFiles.add( qfile );
  engine.setDataByNumber( dataId, processFiles );
}

Download

warning 自由改変可能な JavaScript (ECMAScript) コードです。いかなる保証もありません。
(アドオン自動工程のインストールは Professional editionでのみ可能です)

Notes

  • Questetra 側の設定
    • 「メッセージ開始イベント(Webhook)」の「HTTPレスポンス内容」にて、受け取ったJSONを折り返せるよう以下のコードを設定し、アプリをリリースしておく
{
  "challenge": #{#q_Slack_JSON},
  "processInstanceId" : #{processInstanceId}
}
  • Slack側の設定
    • 設定例
      • From scratch を選択
        • App Name: Questetra App(任意の名称でOK)
        • Pick a workspace to develop your app in: (連携したいワークスペースを選択)
      • Event Subscriptions
        • Enable Events を On に
        • Request URL: Questetra 側 Webhook の URLをいれる(前述の Questetra 側の HTTP レスポンス設定ができてないと Verify が通らないので注意)
        • Subscribe to bot events: 「message.channels」を Add
      • OAuth & Permissions
      • Basic Information
        • Building Apps for Slack の Install your app で Install to Workspace
    • Slack の対象チャネルの詳細の「インテグレーション」から作成したアプリを追加

Capture

See Also

問合管理プロセス, Slack連携

Questetra Supportをもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

上部へスクロール