Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

SlackのSlash Commands+Google Apps Scriptで無料ボット生活

f:id:tbpg:20161125234551p:plain

Slackのチャットボットを作成する場合、
HubotやRuboty+Herokuという組み合わせを利用している方が多いと思います。
ただ、Herokuの無料枠には限りがあります。
そこで、簡単な機能であれば Google Apps ScriptSlash Commands Integrations
を組み合わせることで実現できます。

Slash Commands Integrations とは?

Slackには組み込みで使える / ではじまる Slash コマンドがあります。
この機能を外部サービスと連携してカスタマイズできるのが
Slash Commands Integrations です。

Slash Commands - Slack公式ドキュメント

作成手順

今回は疎通確認だけします。
ping に対して pong を返却します。

Google Apps Scripts: Spreadsheetの作成

新規の Google Spreadsheet を作成し、スクリプトエディタを開きます

f:id:tbpg:20161125234256p:plain

Google Apps Scripts: 関数の作成

doGet関数を作成します。
返却フォーマットに関する詳細は公式ドキュメントをご確認ください。

Slash Commands - Slack公式ドキュメント

function doGet(e) {
  var res = {"text": "pong"};
  return ContentService.createTextOutput(JSON.stringify(res)).setMimeType(ContentService.MimeType.JSON);
}

f:id:tbpg:20161125234303p:plain

Google Apps Scripts: ウェブアプリケーションとして公開

ウェブアプリケーションとして公開します

  • アップロードアイコンを選択します

f:id:tbpg:20161125234309p:plain

  • プロジェクト名を設定します

f:id:tbpg:20161125234313p:plain

  • アクセス対象を全員にして導入をします

f:id:tbpg:20161125234317p:plain

  • URLをコピーしておきます

f:id:tbpg:20161125234321p:plain

Slack: Slash Integration を開く

  • App Directory で Slash を入力して検索します

f:id:tbpg:20161125234325p:plain

  • Slash Commands を選択します

Slack: 設定を追加する

  • Add Configuration を選択します

f:id:tbpg:20161125234329p:plain

  • コマンド名を入力し, Add Slash Command Integration を選択します

f:id:tbpg:20161125234334p:plain

  • 各種設定を行います

f:id:tbpg:20161125234343p:plain f:id:tbpg:20161125234348p:plain

  • Show this command in the auto complete list の部分はこんな感じで表示されます

f:id:tbpg:20161125234354p:plain

以上で設定完了です。

動作確認

f:id:tbpg:20161125234404p:plain