Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

python - How do you invoke the /poll command using the Slack API?

My slack channel supports the /poll command from the Simple Poll app. How do you invoke this command using the Slack API?

Using the 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')

The message just shows up in the #test channel as plain text, not converted to a poll.

I tried using <!poll> instead of /poll in the message, as sort of implied in the message formatting documenation, but same result.

Note: This question is a bit old now, and upon revisiting I have found out that my code is using what's now called a legacy token, which doesn't allow specifying any OAuth permission scopes. The legacy token already has the permissions it needs for this case.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to use the "undocumented" chat.command API function instead of chat.postMessage. This function is a little less friendly with the channel parameter -- you have to provide the channel ID and not the human-friendly channel name.

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"'
)

Thanks to V13Axel in this Wee-Slack bug tracker for providing some debugging info for the chat.command that clued me in.

According to @Erik_Kalkoken's unofficial documentation, chat.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...