如何使用Slack API调用/poll命令?

2024-06-07 14:00:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我的slack channel支持来自Simple Poll应用程序的/poll命令。如何使用Slack API调用此命令?

使用python slack(er) API module

from slacker import Slacker

# Using what's now called a "legacy token"
slack = Slacker('my-token')

slack.chat.post_message(
        '#test',
        '/poll "Do you prefer cats or dogs?" "Cats" "Dogs"',
        as_user=False,
        username="Poll Bot",
        icon_emoji=':question:',
        parse='full')

消息只是以纯文本形式显示在测试频道中,而不是转换为投票。

我试着在消息中使用<!poll>而不是/poll,就像message formatting documenation中暗示的那样,但结果是一样的。

注意:这个问题现在有点老了,在重新访问时,我发现我的代码使用的是现在称为legacy token的代码,它不允许指定任何OAuth permission scopes。旧令牌已具有此情况所需的权限。


Tags: 代码命令tokenapi应用程序消息messagechannel
2条回答

您必须使用“未记录的”^{}API函数,而不是chat.postMessage。这个函数对channel参数不太友好——您必须提供通道ID,而不是人类友好的通道名称。

channel_id = slack.channels.get_channel_id('test')
slack.chat.command(
        channel=channel_id,
        command='/poll',
        text='"Do you prefer cats or dogs?" "Cats" "Dogs"'
)

感谢V13Axel in this Wee-Slack bug tracker为包含我的chat.command提供了一些调试信息。

根据@Erik\u Kalkoken的unofficial documentationchat.command

requires the scope post. Since this scope does not seem to be available in the app config window you need to provide a legacy token for this to work.

我遇到了完全相同的问题,所以我做了一些编码,下面是一个工作示例:

https://github.com/dazlious/slack-cmd-trigger

你可以用你的api令牌用命令触发任何通道。

相关问题 更多 >

    热门问题