Send a text to Slack Webhook containing selected u

2019-08-10 13:49发布

I am trying to send a message from Google Apps Script to Slack via a Webhook URL that works great, but I can't figure out how to send a highlighted or selected user in that text. For example '@UsernameTest how are you?' is something I want to send to a person or channel, but it doesn't work. I figured out that to highlight the channel i.e. send '@Channel' I just needed to write , but that is not what I want. I tried <@UserID> but it still didn't work. (I Received the UserID by using inspector tool on the web-version of Slack on the user.)

The Slack API docs helped some but still have problems.

Here is the code I'm using, thank you.

var SLACK_URL = 'https://hooks.slack.com/services/...

function sendSlackMessage(text) {
  var slackMessage = {
    text: "@UsernameTest how are you? and all you guys <@Channel>?" 
  };

  var options = {
    method: 'POST',
    contentType: 'application/json',
    payload: JSON.stringify(slackMessage)
  };
  UrlFetchApp.fetch(SLACK_URL, options);
}

2条回答
We Are One
2楼-- · 2019-08-10 14:43

Seems that you have 3 " in that line:

text: "@UsernameTest how are you? and all you guys "<@Channel>?"
查看更多
We Are One
3楼-- · 2019-08-10 14:49

The correct format for sending user mentions is <@[user-id]> And for channel pings the syntax is <!channel> or <!here>.

e.g. to send a mention to a user with user ID U1234567 you need to sent the string:

<@U12345678> how are you? and all you guys <!channel>?"

For more information on how to link users and channel see the documentation.

Note that @username is being phased out and should no longer be used. For more details see "A lingering farewell to the username" from Slack.

查看更多
登录 后发表回答