7

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.

CivFan
  • 13,560
  • 9
  • 41
  • 58

2 Answers2

16

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.

CivFan
  • 13,560
  • 9
  • 41
  • 58
  • 2
    This appears to be another undocumented API command. Many thanks for sharing. – Erik Kalkoken Dec 03 '16 at 02:36
  • 3
    I am maintaining a github repo to document "undocumented" Slack API methods - [link](https://github.com/ErikKalkoken/slackApiDoc) - and I have taken the liberty to add this method to it. Any comment or further input on the documentation would be highly appreciated. – Erik Kalkoken Dec 03 '16 at 14:18
  • This does not seem to be working anymore. At least not with the python slack-client sc.api_call("chat.command", channel=channel, command=slash_command, text=message) – mschmidt42 Apr 03 '17 at 00:03
  • @mschmidt42 I haven't looked into the slack package you mentioned, but it's still working for me with the `slacker` package. What error are you getting? – CivFan Apr 04 '17 at 15:23
  • @mschmidt42 The channel must be by id, the command must have %2F before the slash_command, and you have to pass an authentication token – Clay May 03 '17 at 17:01
  • @CivFan While trying the same snippet, I am getting a missing scope error. What is the scope required for chat.command to work? I have tried both chat:write:bot & chat:write:user – nithishr Aug 06 '17 at 18:24
  • 1
    @ritz I was using what's now called a [legacy token](https://api.slack.com/custom-integrations/legacy-tokens). You can't specify scopes on this kind of token. – CivFan Sep 11 '17 at 19:10
  • @ritz I think this requires a legacy token unfortunately. I added a note with a quote from Erik's unofficial documentation. – CivFan Sep 11 '17 at 19:24
  • Is it still possible to send the /poll command? Because Im getting an "unkown_command" error – zwep Mar 11 '19 at 14:02
  • @zwep Sorry I don't have my environment or code to test this anymore. It's possible `/poll` was dropped. Or you need to install a plugin. – CivFan Mar 11 '19 at 16:37
  • @CivFan thanks for the info! I'll fix something myself – zwep Mar 12 '19 at 11:35
3

I was stumbling across the exact same problem, so I did a bit of coding and a working example is here:

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

You can trigger with your api-token any channel with a command.

dazlious
  • 546
  • 6
  • 23
  • 1
    This is so amazing!! I am trying to use this with "/giphy" but the user has to click on "Send" for posting the gif. Do you know if we can achieve it using the command line? – A_G Jul 06 '20 at 18:42