How to mention user in slack.client

梦想的初衷 提交于 2019-11-29 10:53:53

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/40771924/how-to-mention-user-in-slack-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!