27

在一个松弛的团队中,我们可以使用 python 向用户发送消息吗?我见过各种 API,它们向频道提供消息,但不向特定用户提供消息。我们可以这样做吗?

4

7 回答 7

26

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

于 2016-01-18T10:25:25.710 回答
11

简单的解决方案是使用 Slack 的Web APIrequests

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)

您可以在这里获取用户 ID:

在此处输入图像描述

于 2019-08-17T18:27:18.833 回答
9

这是我使用 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"
)

https://pypi.python.org/pypi/slackclient

于 2017-07-19T00:45:12.510 回答
2

您可以获取用户 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)
于 2016-12-27T08:48:30.197 回答
1

我的示例基于本教程:build-first-slack-bot-python

有了这个,我可以简单地通过调用来解决这个问题:

userChannel = slack_client.api_call(
    "im.open",
    user=auser
)

然后发送消息

slack_client.api_call(
        "chat.postMessage",
        channel=userChannel['channel']['id'],
        text=response or default_response, 
        as_user=True
    )

之后,一切似乎都按预期进行。我想在未来创建类似于“/gifs”界面的东西。

于 2018-02-27T17:54:56.840 回答
1

正如 Deepali 所说,只需将 @user 传递给 channel 参数。您还可以在此处使用 RapidAPI上的 Slack postMessage 端点 API 。此页面将允许您生成在 python 中进行 API 调用所需的代码并测试 API 调用。

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': ''

    })
于 2017-02-02T19:26:15.610 回答
0

上面的解决方案已经解决了问题!对于那些想要学习设置整个工作流程(Slack App + Python)以通过 Slack 聊天单独发送消息的人,我在这里写了一些分步说明 - https://github.com/solomonvimal/GlobalShapersLosAngeles/blob /main/Slack_Bday_Nudge.py

于 2021-09-05T22:34:10.767 回答