DartでCloud Functions for Firebaseを動かす
最近FlutterでDartを触っているので、Dartでなにかできればなと思いました。
ちょっとした処理をしたくなって、Cloud Functions for Firebaseが使えればなと思い調べたところ、DartをJavascriptにトランスパイルしてCloud Functionsで利用できるようにするPluginがあったのでその紹介をします。
firebase_functions_interop
READMEが充実しているのでその通りにやればだいたいできます。
build_runnerでビルドして、生成された.jsファイルをpackage.jsonが見るようにします。
デバッグ
serve
コマンドでローカルに立ち上げることができます。
$ firebase serve --only functions
ハマったところ
通信はnode_httpで行う
http.getを行おうとすると実行時に次のようなエラーが発生します。
ReferenceError: XMLHttpRequest is not defined
firebase_functions_interopがnode_httpで通信を行なっているので、それに合わせます。
Cloud Functions for Firebaseの無料版ではOutbound networkに制限がある
Cloud Functions for Firebase内でSlackなどとOutbound networkを行うと次のようなエラーログが吐かれます
Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
無料版だとOutbound networkingがGoogle services onlyという制限があります。
解決するにはFlameプランかBlazeプランにする必要があります。
作ったもの
SlackのOutgoing WebHooksを使って、トリガーとなる言葉とユーザグループを指定するとランダムにユーザを選択するものを作りました。
まとめ
DartでもCloud Functions for Firebaseを動かすことはできますが、適宜制約があるのでハマらないように気をつけましょう。
参考
firebase_functions_interop / Dart Package
node-interop/build_node_compilers at master · pulyaevskiy/node-interop / GitHub
Cloud Functions for Firebase - Billing account not configured / Stack Overflow