1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Google Apps Script(GAS) で作った Slack Slash Command の3秒レスポンス問題に対抗する

Last updated at Posted at 2023-09-08

TL;DR

Google Forms を Pub/Sub っぽくつかおう。

3秒レスポンス問題について

お手軽に Slack の Slash Command を作る場合、以下の記事で紹介されているような GAS の doPost を使用する方法は便利です。

しかし、Slash Command では、レスポンスを 3 秒以内に返却しなければいけない制約が存在し、
3 秒を超えると operation_timeout としてエラーのレスポンスが表示されてしまいます。

This confirmation must be received by Slack within 3000 milliseconds of the original request being sent, otherwise an operation_timeout error will be displayed to the user.

https://api.slack.com/interactivity/slash-commands#responding_basic_receipt

Google Apps Script は実行に時間がかかりやすく、特に Google Docs や Google Sheets といった Google Apps を操作する場合、簡単に 3 秒を超えてしまいます。

これを回避するためには、以下の記事でも触れられているように時間のかかる処理は非同期的に処理し、Slash Command はリクエストを受け取ってすぐ(3 秒以内)にレスポンスを返すしくみを作る必要があります。

Google Cloud を使用できる場合は Cloud Functions や Pub/Sub を使うことで非同期的な処理を実現できますが、お手軽ではなくなってしまうため今回は Google Apps の範囲内で回避するための対抗策を紹介します。

対抗策

Google Forms を用い、擬似的な Pub/Sub メッセージングのようなものとします。
具体的な構造としては以下の図の通りです。

GAS.png

これによって、Slash Command を処理するスクリプト(図の左側のスクリプト)は Form を送るだけとなります。
Slash Command で行いたかった本来の時間のかかる処理はフォーム送信時のイベントトリガで実行されるスクリプト(図の右側)で行われることで、非同期的な処理の実行が実現されます。

注意点

Slash Command ではあくまで Form を送信するのみなので、そのレスポンスは request received のような簡素なものになってしまいます。
本来送りたかったレスポンスは別個に Slack API や Incomming Webhooks などを用いて、後続の onFormSubmit スクリプト側でケアしてあげる必要があります。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?