47

This might be a simple question, but I can not get it work.

I am using Slack Python Api to mention a user in a channel, and I am referring to the document here, https://api.slack.com/methods/chat.postMessage, and my code is simple as,

from slackclient import SlackClient
sc = SlackClient(token)
message = sc.api_call(
  'chat.postMessage',
  channel='#channelname',
  text='This is a test.'
  )

This will send a message to the channel, but I can not find any option to mention users. And I tried to put @someone inside the message such as

 text='@someone This is a test.'

The message will be posted but in plain text, but really mentioning someone. BTW, I am using a Test Token.(Or maybe this feature is only available for authorized token? )

Is there any option or method to do this?
Thank you in advance.

Haipeng Su
  • 1,673
  • 2
  • 10
  • 26
  • 1
    Check this out: http://stackoverflow.com/questions/36414427/how-to-add-mention-in-response-to-slash-commands-in-slack/36463098#36463098 – Wilhelm Klopp Nov 23 '16 at 21:13
  • @WilhelmKlopp thank you for the suggest. It helps. However, the `user_name` will be changing the bot showing name, not mentioning someone in the message. I posted my answer below and believe we don't need to get the user ID in that case. – Haipeng Su Nov 23 '16 at 21:32

2 Answers2

48

After a little bit exploration, I got the solution which is quite simple. I don't know how I could miss it.

message = sc.api_call(
  'chat.postMessage',
  link_names=1,
  channel='#channelname',
  text='@someone This is a test.'
  )

use the option link_names=1 to link channels or user names automatically in the text message. This will do the trick.

Thank you everyone.

Haipeng Su
  • 1,673
  • 2
  • 10
  • 26
  • The real question is: why isn't the default for link_names set to 1? Thanks a bunch! – ScottieB Mar 09 '18 at 02:50
  • @ScottieB might be a good idea to mention it to the development team. :) – Haipeng Su Mar 10 '18 at 20:05
  • 5
    `link_names` can also be set to True/False which makes a little more sense then setting it to 0 or 1. – John Snow Sep 24 '18 at 15:56
  • 5
    Use `` instead. This method doesn't work when mentioning more than 1 user. – marengaz Sep 26 '18 at 17:31
  • 2
    @marengaz the feeling I get after reading (pretty much all) slack documentation is that userId is preferred, generally speaking. However I was able to get two mentions in a chat_postMessage method (although I'm using https://github.com/slack-ruby/slack-ruby-client ) text: ": ", And it worked fine – JohnZaj Nov 24 '18 at 01:44
  • sorry @JohnZaj, i was a little ambiguous. when i said "This method ..." i was referring to `text='@someone This is a test.'`, as in the answer. i have the same findings as you, in that, 2 mentions work correctly with the <> brackets – marengaz Nov 26 '18 at 14:15
  • For anyone that wants to notify a user via an incoming webhook, setting `link_names: true` in REST payload also does the trick. Even though this is not documented... – YAC Jan 31 '20 at 04:00
43

Posting an updated answer as this method no longer works since Slack updated their API. Now you have to discover the user's ID using users.list, or just looking it up in the Slack app on their profile.

Then for a given userID, you mention them by setting the text as follows: <@userID>. The link_names argument is now irrelevant. So this would be the code to use now:

message = sc.api_call(
  'chat.postMessage',
  channel='#channelname',
  text='<@userID> This is a test.'
  )

HOWEVER, if you want to mention a usergroup, then the old method still applies - just @mention them and in that case do set link_names to true.

Tom Wagstaff
  • 720
  • 8
  • 12
  • Note that the @userID is not the user's handle or display name. It's an underlying identifier https://api.slack.com/changelog/2017-09-the-one-about-usernames – Noel May 22 '19 at 22:11