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

firebaseほぼしらずにCLI上でfirebase init~SlackBot連携するときに考えたこと履歴

概要

とりあえず、serverlessなfirebaseを使おうということで、ほぼドキュメントノールックで始める。

初期化

https://firebase.google.com/docs/cli?hl=ja#install_the_firebase_cli

npm -g install firebase-tools
firebase login

projectを作成する

mkder test-project

initする

cd test-project
firebase init

なんか色々選択肢が出来きてわからん

やりたいこと

  • slackでreaction情報、投稿情報を都度取得する
  • DBに保存する
  • eventに応じて、slack userに反応を返す
    • この時、DBを参照する

選択肢

  • Firestore:
  • Functions:
  • Hosting:
  • Hosting:
  • Storage:
  • Emulators:
  • Remote:
  • Extensions:
  • Realtime Database:

firebase 機能で検索し、一番うえに出てきてくれたページを参考にFunctionsとFirestoreにした。

SlackのApp manufest

を定義する or Slack

document確認

何をどうしてくれる。

https://firebase.google.com/docs/functions/typescript?hl=ja

slack event APIを最小構成でsubscribe確認するために事例を見る 

firebase slack bolt 勤怠 でgoogle検索 検索

firebase.jsonファイルについて

firebase initすると生成されるファイルの一つ

predeployは、functions/配下のpackage.jsonのscriptsのコマンドが実行去れるように書かれている。

predeployで実行されるのは、下記の2つのコマンドとなる。 ``

yarn run lint

eslint --ext .js, .ts src/.

yarn run build

tsc

``

lintがなかなか通らないと、deployまでこぎつけられないので、面倒な場合は、この記述を外してしまってもよさそうである。

firebaseのデプロイ

ここで詰まり、面倒になり、2週間くらい放置した。

再度確認すると、まず

funcsions/というディレクトリに、package.jsonが存在するので、yarnで実行する場合は、workspaceを指定する必要が出ると思いきや、ディレクトリ直下のpackage.jsonはないので、それはできない。

記述を追加しても良いが、なるべくデフォルトの状態から改変したくないので、ここは一旦、firebase deploy --only functions コマンドを実行する。

実行するとlint系が勝手に動作してしまい、めんどいことに成ったので、エラーを一つ一つ解消していく。

tscのエラー

sh: tsc: command not found

と言われたので、

npm i typescript -g

したら

Volta error: Could not locate executable `tsc` in your project.

と言われたので、

volta install typescript

したら、また同じこと言われたので、

cd functions; npm install typescript 

したら、

Internal Error: functions@workspace:.: This package doesn't seem to be present in your lockfile; run "yarn install" to update the lockfile

ってことで、

cd functions; yarn install
src/index.ts:6:3 - error TS2345: Argument of type '(req: Request, res: Response) => void' is not assignable to parameter of type '(req: Request, resp: Response<any>) => void | Promise<void>'.
  Types of parameters 'req' and 'req' are incompatible.
    Type 'Request' is missing the following properties from type 'Request': cache, credentials, destination, integrity, and 13 more.

6   (req: Request, res: Response) => {
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in src/index.ts:6

とりあえずfirebase.jsonのpredeployのlint削除しretry

✔  functions: functions folder uploaded successfully
i  functions: creating Node.js 16 (1st Gen) function challenge(us-central1)...
Build failed: yarn run v1.22.19
error Command "plugin" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.; Error ID: e83c667d

Functions deploy had errors with the following functions:
    challenge(us-central1)
i  functions: cleaning up build files...

Error: There was an error deploying functions

面倒そうなので、一旦検索し似た事例を発見

zenn.dev

yarn workspaceの追加方法

https://classic.yarnpkg.com/lang/en/docs/workspaces/#toc-how-to-use-it

参考に追加して見て再度実行したら、うまく行ったっぽい。

Project Console: の確認

https://console.firebase.google.com/project/slack-event-subscriber/overview に移動し、

左側のfuncsionsタブから、エンドポイントを確認する

index.ts に定義したchallenge/関数が存在していることを確認できたが、デプロイできませんでしたと書いてある。

とりあえず、削除して再デプロイしたところ、失敗した。

今まで、firebase deployしていたので、yarn workspace functions run deployを実行したところ、Command \"plugin\" not foundと言われ、同じように失敗した。

Slack側の設定

https://api.slack.com/apps/<AppID>/event-subscriptionsに移動して、先程のエンドポイントを設定する

slack-botを実行する

DMを送ったり、リプライをしたりしたい場合

コラム: workspaceについてのメモ

  • 組織でやってたら、workspaceにまたがって、slack appをインストールできるらしい。やりかたは知らん

次回考えること

  • 何人をさばけるのか
    • 従量課金制というが、どれくらいまで無料?