happiness (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) happiness

改めてfirebase initしてslackのeventをsubscribeできるようにする

概要

前回、失敗したのでリトライする

firebaseほぼしらずにCLI上でfirebase init~SlackBot連携するときに考えたこと履歴 - ゆもにっき-技術と生活改善と哲学-

documentを参考に、step by stepで素直にやっていく

はじめに: 最初の関数の記述、テスト、デプロイ  |  Cloud Functions for Firebase

firebaseの初期化

すでに firebaseにはログイン済みでprojectも作成、追加済み。

mkdir aaa-playground
cd aaa-playground
firebase init

new projectではなく、既存のprojectを選択する。

欲張らず、functionsのみを選択

cd funcsions
npm run serve

ここまで、OK

次にデプロイしていく

firebase deploy

console確認してみて、問題なさそう。

slackのevent subscriptionsのchallengeを受け取る

src/index.tsを下記のコードに変更する

import { onRequest } from "firebase-functions/v2/https";

export const helloWorld = onRequest((request, response) => {
  if (request) {
    response.status(200).send(request.body);
  } else {
    console.log("Request Error...");
    throw response.status(500);
  }
});

再度firebase deploy --only functionすると、疎通確認できた☺️

slack の subscriptionができるようにしていく

yarn使うと変になりそうなので、npm でinstallしていく。

cd functions/

npm install @slack/bolt

document出す

ソケットではなく、httpのほうが良さそうなので、そっちのdocumentを出す

https://slack.dev/bolt-js/ja-jp/tutorial/getting-started-http

上記のdocumentにしたがって、OAuth & Permissionsから、Bot User OAuth Tokenを取得する。

環境変数の設定

firebaseの場合は、単純にexportするだけではだめだと思うので、firebaseのdocumentを確認しに行く

https://firebase.google.com/docs/functions/config-env?hl=ja&gen=2nd#deploying_multiple_sets_of_environment_variables

boltのdocumentには、以下の2つの情報が必要なので、定義していく

export SLACK_SIGNING_SECRET=<your-signing-secret>
export SLACK_BOT_TOKEN=xoxb-<your-bot-token>

実行したところエラーが出た

firebase functions:secrets:set SLACK_BOT_TOKEN

Error: HTTP Error: 403, Secret Manager API has not been used in project 1086950087481 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/secretmanager.googleapis.com/overview?project=<my project id> then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

https://console.cloud.google.com/apis/api/secretmanager.googleapis.com/metrics?project=

からSecret Manager APIを有効にする必要がある。

有効にしたあとの作業の前に

そもそも、cloud functionsとbolt併用して使えるのか問題が有るので、考えていく。

この辺の議論 https://github.com/slackapi/bolt-js/issues/213

これ https://github.com/seratch/bolt-on-cloud-functions-for-firebase

https://qiita.com/seratch/items/3e44f58a7c8ab41f21f0