Reputation: 1715
Attachments aren't working in the following code and neither is the response_type
showing as it should be. I've also tried using Python's Slack Client, but the exact same thing is happening.
def send_message(channel_id, text):
params = {
"token" : token,
"username" : "NEW BOT",
"channel" : channel_id,
"text" : text,
"response_type": "ephemeral",
"attachments": [{ "text":"This is some text" }]
}
headers = {'content-type': 'application/json'}
slack_api = 'https://slack.com/api/chat.postMessage'
requests.get(slack_api, json=params, headers=headers)
return
@app.route('/', methods=['GET', 'POST'])
def main():
if sc.rtm_connect():
sc.rtm_read()
text = request.args.get("text")
channel_id = request.args.get("channel_id")
send_message(channel_id, text)
return Response(), 200
Upvotes: 1
Views: 4461
Reputation: 2296
The response_type
field can only be set when producing a message in response to a slash command or message button action invocation. It can't be set directly with chat.postMessage
, as there's no context on the target user for whom to display that ephemeral message.
Another quirk about chat.postMessage
is that it doesn't currently accept JSON like incoming webhooks does. Instead, you need to send POST parameters of the application/x-www-form-urlencoded
variety. Even quirkier, that attachments
field actually doesn't get sent as a string of JSON, but URL-encoded into a parameter.
One more tip, with chat.postMessage
and other write methods, you should use an HTTP POST instead of a GET.
Upvotes: 10
Reputation: 144
attachments='[{"title": "Try these - ","text": " Text ", "mrkdwn_in":["text"]}]'
Add the title to the attachment. It worked in my case.
Upvotes: 2