Can we send message to user in slack using python script?

Vikas Saini picture Vikas Saini · Jan 18, 2016 · Viewed 46.7k times · Source

In a slack team, can we send a message to a user using python? I have seen various APIs, they offer message to channel, but not to a particular user. Can we do that?

Answer

Deepali Mittal picture Deepali Mittal · Jan 18, 2016

Yes,this can be done. Instead of "#channel_name" use "@user" in the API. The user will receive the message from slackbot as we are using API and not a direct message from any other user. And if you want to post to that user as the authenticated user, use as_user= true.

slack.chat.post_message('@to_user',msg,username='@from_user')

More details are at https://api.slack.com/methods/chat.postMessage