Slackでbotからのメッセージに対してマイキーワードでの通知が発火しないケースと解決策

概要

Slackにおいて、マイキーワードを設定すると通知がされることを期待するはず。ここでの通知とは、バッジでの表示とシステム統合されたサウンド付きの通知のことを期待している。

しかし、Slack botからのメッセージにおいて、マイキーワードを設定しているにもかかわらず、通知される場合とただハイライトされるだけの場合がある。ヘルプにも以下の記載があるが、詳細については書かれていない。

https://slack.com/intl/en-gb/help/articles/201355156-Configure-your-Slack-notifications#keyword-notifications

Keywords in messages from some apps won’t trigger notifications, but will appear highlighted in Slack

挙動を見たり、サポートに問い合わせたりした結果、以下の条件で通知が発火することが確認できた。サポート曰く、バグらしいのでいつかは修正されるかもしれない。

通知が発火する条件

  • Slack Appであること
    • レガシーのincoming webhookは通知されない
  • API経由で送信すること
    • Slack Appのwebhookでも通知されない
  • Block kitでmrkdwnで送信すること
    • text属性として送信しても通知されない
    • plain_textは通知されない
  • username, icon_url, icon_emojiを変更しない
    • バグらしい。サポートから教えてもらった

mrkdwnでの送信の例。

    [
      {
        type: "section",
        text: { type: "mrkdwn", text: "Hello world" }
      }
    ]

さらなるコツとして、 chat.postMessage を実行するときに、blocks だけでなく text も渡すというのがあります。

client.chat_postMessage(channel: channel, blocks: blocks, text: "Hello world")

そうすると通知の中に内容を表示することができます。

経緯

レガシーのwebhookで送信しているメッセージに対してマイキーワードがただのハイライトされるだけということが発覚。

経緯2

以前に別のサービスで type: plain_text なメッセージに対して、同様にマイキーワードがただのハイライトになる事象があり、そのときは type: mrkdwn にすることで、通知にすることができた。

Slackサポートからの回答

Declan here at Slack, sorry for the trouble here.

Unfortunately, keyword notifications aren't always consistent when the message is posted by a bot, rather than a regular user.
With that said, setting a username shouldn't really break this functionality. I'm going to flag this as a bug on my end, and we'll notify you when it's resolved. It may take some time due to some other pressing bugs the team is working on, so for now, I would advise you to send messages without changing the username. Let me know if you have any other questions on this.

Quickly following up with you.
After checking internally, it looks like it's expected for keyword notifications to not work in all scenarios. This is mentioned on the docs here: https://slack.com/intl/en-gb/help/articles/201355156-Configure-your-Slack-notifications#keyword-notifications

Keywords in messages from some apps won’t trigger notifications, but will appear highlighted in Slack

Changing the usersname, icon_url or icon_emoji will all cause keyword notifications to not work. We have filed feedback to improve this approach more generally, but we don't have any immediate news to share on that just yet. Sorry again for the trouble, but let me know if you have any questions on this.