使用python slack sdk获取Slack频道ID

问题描述 投票:0回答:2

我正在编写一个 Slack 机器人,需要更新已发布的消息,因此我使用 Slack python SDK 实现了此代码:

def update_message(self, message_text, ts):
    response = self.client.chat_update(channel=self.channel, ts=ts, text=message_text)
    return response

问题是我只有频道名称,而方法

chat_update()
需要频道ID。

知道频道名称怎么知道频道ID?

python slack slack-api
2个回答
0
投票

我相信这是 Slack API 的一个常见问题。您可能需要依赖 conversations.list


0
投票

在你的类中添加这样的方法应该会有所帮助:

def get_channel_id(self, channel_name):
    for result in self.client.conversations_list():
        for channel in result['channels']:
            if channel['name'] == channel_name:
                return channel['id']
    return None

其中

self.client
slack_sdk.WebClient
实例。请注意,它可能会非常慢,因此如果您要多次使用它,则值得缓存答案。

© www.soinside.com 2019 - 2024. All rights reserved.