Adding an Application to Slackbot

I am trying to add an attachment to a slip message through my API. I use the python shell they recommend. I can send and receive basic messages, but when I try to add an attachment in the form of two buttons, it does not work. I made a small application and linked the bot, as they claim in their API. I 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)

Answer only {u'ok': False, u'error': u'no_text'}

+4
source share
2 answers

I get it.

The python shell separates 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 in the application, since it was they who formatted it in their API documents. Attachments should be just an array after the attachment key.

+5

, .

:

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

https://api.slack.com/methods/chat.postMessage https://api.slack.com/docs/message-buttons#readying_your_application_for_message_buttons . :

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

, https://github.com/slackapi/python-slackclient/blob/ddf9d8f5803040f0397d68439d3217d1e1340d0a/slackclient/_slackrequest.py .

+1

Source: https://habr.com/ru/post/1659752/


All Articles