Zachary Laborde
Zachary Laborde

Reputation: 73

How to select a button from a interactive message with a bot?

I'm trying to get a bot to "click" a button on an interactive message in Slack (preferably as a bot, but using a user token works too).

I've found that the link to send the action information to be

https://blue-hybrid.slack.com/api/chat.attachmentAction

My problem is I can't find any documentation for "chat.attachmentAction." Looking at the request sent when using my browser, it has one http argument: "_x_id" and the payload is a WebKitForm, containing 4 items: payload, client_id, payload_id, and token.

I'm sure if I'm just not sending the appropriate data or authentication or what. All of my POSTs return "invalid_payload" or "invalid_arg_name."

Any help is greatly appreciated.

Upvotes: 3

Views: 1003

Answers (1)

Zachary Laborde
Zachary Laborde

Reputation: 73

Looks like I figured it out, finally!
I had to work it out the old fashioned way. Slack Customer Support would only help with the official public API. I'll leave the solution here in Javascript.

To do this, you need 3 things:

  1. choice_num
    • the number of the choice within the list of options.
    • e.g. If a message has the buttons (from left to right): yes, no, and maybe, then yes=0, no=1 and maybe=2.
  2. message
    • the json of the interactive message
  3. SLACK_TOKEN
    • your slack token (not sure if bot tokens work, user tokens do however)

The method chat.attachmentAction itself requires 3 arguments:

  • payload
  • service_id AND/OR bot_user_id
  • token

args = encodeURI(
'payload={'
+ '"actions":[' + JSON.stringify(message.attachments[0]["actions"][choice_num]) + '],'
+ '"attachment_id":"' + message.attachments[0]["id"] + '",'
+ '"callback_id":"' + message.attachments[0]["callback_id"] + '",'
+ '"channel_id":"' + message.channel + '",'
+ '"message_ts":"' + message.ts + '"}'
+ '&service_id=' + message.bot_id
+ '&bot_user_id=' + message.user
+ '&token=' + SLACK_TOKEN
)

request_url = 'https://YOURSLACKTEAM.slack.com/api/chat.attachmentAction?' + args

then just send an async POST to the request_url and you should get back something like this:

{"replaced":true,"ok":true}

Upvotes: 2

Related Questions