Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete interactive message (command response) just with Slack Commands API

So I'm building a slack app in Node that responds to some user commands. It usually responds with an interactive message that has some attachments and buttons.

For certain buttons, I return different types of texts or other attachments, but I also want to have a "Cancel" button that would basically cancel the current command - delete it from the user's chat.

The thing is that I want to do it without having to request the chat:write:user scope or without having to create a bot.

  1. By asking for the chat:write:user scope I can basically remove any message the user created, but it asks for that scope when installing the app and gives my app permission to post on behalf of the user, which most find uncomfortable.

  2. By creating a bot I could achieve this, but again it asks for permission to add a bot to the channel and I don't want this.

What I've tried

  1. Use chat.delete method, but I get { ok: false, error: 'missing_scope', needed: 'chat:write:user', provided: 'identify,commands' } from Slack, even when I try with as_user set to false.

  2. Respond with an empty message to the command, like res.send(), or res.send({ text: null }), or res.send({ attachments: null }), etc. When you reply to a user command on Slack it overrides by default the previous content. I just want to override with nothing, basically removing the original response. When I try this however, it doesn't do anything, unless I provide some content.

I'm pretty sure #2 is the way to go and I feel I'm close and it's something pretty simple but out of my sight.

Working example of what I want

I know it can be done because the GIF Keyboard app for slack does this. When searching for Gifs, they have a "cancel" button at the bottom that just removes the current command/message. Furthermore, they only ask for the Commands scope.

like image 674
Raul Rene Avatar asked Apr 03 '18 09:04

Raul Rene


People also ask

How do I bulk delete Slack messages?

According to Slack's own guidance, it's not possible to bulk delete messages. Owners and admins can delete a channel to remove all its messages.

What is ephemeral message Slack?

While an ephemeral message is similar to other messages in conversations in Slack, only one user within a conversation will see it, and it does not persist across reloads, between desktop and mobile apps, or across sessions.


1 Answers

You are on the right track. Option #2 works, so no need to request scopes to delete a message.

Just send the following response back to Slack and the last (ephemeral) message from your app will be deleted:

{
    "response_type": "ephemeral",
    "replace_original": true,
    "delete_original": true,
    "text": ""
}
like image 123
Erik Kalkoken Avatar answered Sep 22 '22 03:09

Erik Kalkoken