我们可以使用 python 脚本在 slack 中向用户发送消息吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34852104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we send message to user in slack using python script?
提问by Vikas Saini
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?
在 Slack 团队中,我们可以使用 python 向用户发送消息吗?我见过各种 API,它们向通道提供消息,但不向特定用户提供消息。我们可以这样做吗?
回答by Deepali Mittal
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
.
是的,这是可以做到的。在 API 中使用“@user”代替“#channel_name”。当我们使用 API 时,用户将收到来自 slackbot 的消息,而不是来自任何其他用户的直接消息。如果您想以经过身份验证的用户身份发布给该用户,请使用as_user= true
.
slack.chat.post_message('@to_user',msg,username='@from_user')
More details are at https://api.slack.com/methods/chat.postMessage
回答by Prashanth
You can get a list of user IM channels and post your message to any IM channel (Direct messaging).
您可以获得用户 IM 频道列表并将您的消息发布到任何 IM 频道(直接消息)。
from slackclient import SlackClient
SLACK_TOKEN = "xoxb-bottoken" # or a TEST token. Get one from https://api.slack.com/docs/oauth-test-tokens
slack_client = SlackClient(SLACK_TOKEN)
api_call = slack_client.api_call("im.list")
# You should either know the user_slack_id to send a direct msg to the user
user_slack_id = "USLACKBOT"
if api_call.get('ok'):
for im in api_call.get("ims"):
if im.get("user") == user_slack_id:
im_channel = im.get("id")
slack_client.api_call("chat.postMessage", channel=im_channel,
text="Hi Buddy", as_user=True)
回答by David Noah
As Deepali said, just pass @user into the channel parameter. You could also use the Slack postMessage endpoint API on RapidAPI here.This page will allow you to generate the code needed to make the API call in python and test out the API call.
正如 Deepali 所说,只需将 @user 传递到 channel 参数中即可。您还可以在此处使用RapidAPI上的 Slack postMessage 端点 API 。此页面将允许您生成在 python 中进行 API 调用所需的代码并测试 API 调用。
The code RapidAPI will generate for you will look like this:
RapidAPI 将为您生成的代码如下所示:
from rapidconnect import RapidConnect
rapid = RapidConnect("SlackIntegration", "3b744317-91c6-48e8-bc90-305a52f36a5f")
result = rapid.call('Slack', 'postMessage', {
'token': '',
'channel': '',
'text': '',
'parse': '',
'linkNames': '',
'attachments': '',
'unfurlLinks': '',
'unfurlMedia': '',
'username': '',
'asUser': '',
'iconUrl': '',
'iconEmoji': ''
})
回答by running-codebase
This was the Python solution I found using the SlackClient package:
这是我使用 SlackClient 包找到的 Python 解决方案:
from slackclient import SlackClient
slack_token = os.environ["SLACK_API_TOKEN"]
sc = SlackClient(slack_token)
sc.api_call(
"chat.postMessage",
channel="@username",
text="Test Message"
)
回答by James Nelson
I based my example off of this tutorial: build-first-slack-bot-python
我的示例基于本教程:build-first-slack-bot-python
With that I was able to resolve the issue simply by calling:
有了这个,我就可以通过调用来解决这个问题:
userChannel = slack_client.api_call(
"im.open",
user=auser
)
then sending the message
然后发送消息
slack_client.api_call(
"chat.postMessage",
channel=userChannel['channel']['id'],
text=response or default_response,
as_user=True
)
After that everything seemed to work as expected. I'd like to create something similar to the "/gifs" interface in the future.
之后,一切似乎都按预期进行。我想在未来创建类似于“/ gifs”界面的东西。
回答by Andrey Suglobov
SImple solution is using Slack's Web API
and requests
:
简单的解决方案是使用 Slack 的Web API
和requests
:
import requests
# slack access bot token
slack_token = "xpxb-9534042403-731932630054-KSwhrmhg87TXGuF23Z4ipTRm"
data = {
'token': slack_token,
'channel': 'UJ24R2MPE', # User ID.
'as_user': True,
'text': "GO Home!"
}
requests.post(url='https://slack.com/api/chat.postMessage',
data=data)
userID you can get here:
您可以在此处获取用户 ID: