🍣

Slack Webhookの自動投稿失敗する問題

2021/11/15に公開

前提

SlackのエンドポイントにJSon投げつけて投稿するBotがいた。
たまにそのBotが投稿失敗するので原因調査した

技術的前提

Ruby on Rails + slack-notifier gem を利用

参考(Slack Webhook)

Sending messages using Incoming Webhooks

参考(slack-notifier gem)

https://github.com/slack-notifier/slack-notifier

エラー内容

{
class: Slack::Notifier::APIError, 
message: 
The slack API returned an error: invalid_blocks (HTTP Code 400)
Check the "Handling Errors" section on https://api.slack.com/incoming-webhooks for more information
}

失敗する投稿の特徴

文面が多い。
例えば「今週1,000アクセスされた自社メディアの記事一覧!」みたいな自動投稿で、自社メディアがよく見られていると頻繁に失敗した

調査が難航する点

Sentryなどのエラー検知ツールだと返ってくるエラーログが途中で省略される
JSonが長いこと+投稿内容の文面が多いと失敗しやすいことからここが厳しかった

結論

Slack Webhookは50blocksまでの投稿しか受け付けていない
エラーになる場合は必ず50以上のblock数を持っていた。

根拠

Read our guide to building block layouts to learn where and how to use each of these components. You can include up to 50 blocks in each message, and 100 blocks in modals or home tabs.

Reference: Layout blocks

Discussion