Slack API で User Group に mention する JavaScript サンプルコード

Slack API 経由で User Group に mention する JavaScript サンプルコードをご紹介します。

slack

Slack 公式ドキュメントより User Group への mention 方法の抜粋

For paid account there is an additional command for User Groups that follows the format <!subteam^ID|handle>. (subteam is literal text. ID and handle are replaced with the details of the group.) These indicate a User Group message, and should cause a notification to be displayed by the client. User Group IDs can be determined from the usergroups.list API endpoint. For example, if you have a User Group named happy-peeps with ID of S012345, then you would use the command <!subteam^S012345|happy-peeps> to mention that user group in a message.

ということで、 @happy-peeps という User Group に mention したい場合は、<!subteam^S012345|happy-peeps> という風に記述すると正しく動きます。

@user-group mention JavaScript サンプルコード

以下が Slack API 経由で User Group に mention する JavaScript サンプルコードです。

var userGroup = '<!subteam^S012345|happy-peeps> ';
var slackMessage = userGroup + 'こんにちはこんにちは';
 
UrlFetchApp.fetch(
  'https://slack.com/api/chat.postMessage',
  {
    method: 'post',
    payload : {
      token: PropertiesService.getScriptProperties().getProperties().SLACK_TOKEN,
      channel: '#dev',
      username: 'bot',
      icon_emoji: ':robot_face:',
      text: slackMessage
    }
  }
);

Google Apps Script 向けに書いたコードを一部改変してご紹介しています。

以上、Slack API でも User Group に mention したい現場からお送りしました。