7

I'm trying to add an attachment to a slack message via their API. I'm using the python wrapper they recommend. I can send and receive basic messages but when I try to add an attachment in the form of 2 buttons it fails. I have made a slack app and linked the bot as they state in their API. I've carefully reviewed the API and cannot figure out what is going on.

def process_message(message, channel):
    intro_msg = json.loads('{
                      "text": "What would you like to do?",
                      "attachments": [
                        {
                          "text": "Choose an action",
                          "fallback": "You are unable to choose an option",
                          "callback_id": "lunch_intro",
                          "color": "#3AA3E3",
                          "attachment_type": "default",
                          "actions": [
                            {
                              "name": "enroll",
                              "text": "Enroll",
                              "type": "button",
                              "value": "enroll"
                            },
                            {
                              "name": "leave",
                              "text": "Leave",
                              "type": "button",
                              "value": "leave"
                            }
                          ]
                        }
                      ]
                    }')
    r = sc.api_call("chat.postMessage", channel=channel, attachments=intro_msg)

The response is only {u'ok': False, u'error': u'no_text'}

Nish
  • 385
  • 1
  • 5
  • 19
  • Instead of creating `intro_msg` as `dict` object with from `string`, can you tell me what do you see when you pass `dict` object directly? –  Nov 03 '16 at 17:58
  • have you finished it ? how to get response payload ? – Spartan Apr 22 '18 at 14:10
  • I figured it out below. My payload had the `text` field in it. This needs to be separated out from the payload. – Nish May 11 '18 at 19:46

2 Answers2

15

I figured it out.

The python wrapper separates out the payload.

intro_msg  = json.dumps([{"text":"Choose an action","fallback":"You are unable to choose an option","callback_id":"lunch_intro","color":"#3AA3E3","attachment_type":"default","actions":[{"name":"enroll","text":"Enroll","type":"button","value":"enroll"},{"name":"leave","text":"Leave","type":"button","value":"leave"}]}])

sc.api_call("chat.postMessage", channel=channel, text="What would you like to do?", attachments=intro_msg, as_user=True)

My payload was all in attachments since that is how they format it in their API docs. The attachments needs to just be the array after the attachments key.

Nish
  • 385
  • 1
  • 5
  • 19
1

I guess the basic simple example works.

Example:

from slackclient import SlackClient

slack_token = os.environ["SLACK_API_TOKEN"]
sc = SlackClient(slack_token)

sc.api_call(
  "chat.postMessage",
  channel="#python",
  text="Hello from Python! :tada:"
)

According to https://api.slack.com/methods/chat.postMessage and https://api.slack.com/docs/message-buttons#readying_your_application_for_message_buttons the attachments has to be an array. How about sending it as array:

json.loads('[{"text":"What would you like to do?","attachments":[{"text":"Choose an action","fallback":"You are unable to choose an option","callback_id":"lunch_intro","color":"#3AA3E3","attachment_type":"default","actions":[{"name":"enroll","text":"Enroll","type":"button","value":"enroll"},{"name":"leave","text":"Leave","type":"button","value":"leave"}]}]}]')

As there is no further magic involved but the requests module https://github.com/slackapi/python-slackclient/blob/ddf9d8f5803040f0397d68439d3217d1e1340d0a/slackclient/_slackrequest.py I'd give it a try with the sending as array.