Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mention user in slack.client

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.

like image 709
Haipeng Su Avatar asked Nov 23 '16 18:11

Haipeng Su


People also ask

How do I mention someone in Slack?

@everyone. @everyone can only be used in the #general channel, which is a channel that all members (except guests) are automatically added to. This mention will notify everyone in the #general channel whether their availability is set to active or away.

How do I mention a user in Slack API?

Mentioning users in messages The user mentioning syntax <@W123|bronte> is now deprecated and will eventually be removed. Slack will use the "display name" when rendering all mentions. Use the simpler user ID-only form <@W123> instead. Using link_names when posting messages is also deprecated.

How do I refer someone to a Slack chat?

Tap and hold the message that you'd like to share. Tap Copy link to message. Paste the link in a channel or direct message and tap the paper plane icon to send. The copied link will expand to display the message that you're quoting.

How do I mention someone in Slack Webhook?

You can mention users through <@memberID> as text in the Slack webhook. This will post a username as a string. For example, if there is a user named John with memberID UC6B12Z4N , then pass <@UC6B12Z4N> in the Slack webhook. This will mention @John with the correct link.


1 Answers

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.

like image 120
Tom Wagstaff Avatar answered Sep 19 '22 15:09

Tom Wagstaff