Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove Ephemeral Messages

Tags:

slack-api

I'm trying to figure out the mechanism to post an ephemeral message to a user and then remove it and replace it with a message visible to all. Similar behavior to giphy in which the Slash Command shows an interactive ephemeral message and creates a channel message once the user decides which gif to send. I'm also curious about updating the ephemeral message. I assume this can be done by the response_url if we use an interactive ephemeral message.

I initially figured I'd just create a ephemeral message using chat.postEphemeral and then call chat.delete on it, but it seems chat.delete and chat.update can't be called on a message created using chat.postEphemeral.

The Slack message guidelines seems to suggest that a multi-step interactive flow should always be handled in an ephemeral way so that other channel user don't see all intermediate messages before the result but I'm having bad luck figuring out how to get rid of the ephemeral when done. Probably just being bad at reading but any help appreciated.

Edit with more details:

The documentation around using response_url and postEphemeral states

As you replace messages using chat.update or the replace_original option, you cannot change a message's type from ephemeral to in_channel. Once a message has been issued, it will retain its visibility quality for life.

The message guidelines suggest:

If a user has initiated an action that has multiple steps, those steps should be shown as ephemeral messages visible only to that user until the entire action is complete to avoid cluttering the channel for everyone.

Presumably, I should be able to create an interaction in which I first send an in_channel interactive message.

  • When a user initiates an action, I should be able to send them a series of ephemeral messages using the response_url and passing response_type: 'ephemeral' and replace_original: false?
  • A new ephemeral interactive message created this way will have its own response_url for making edits, right?
  • Once I am done with the interactive flow via ephemeral messages, I can modify the original interactive message using its original response_url?
  • Lastly, how do I get rid of the last ephemeral edit? Or do I just change it to something like "Workflow completed" and hope for the best? I'm asking because Slash commands obviously seem to have a way to essentially replace the ephemeral message for an in_channel message and I'm trying to figure this kind of workflow out.
like image 689
Szymon Rozga Avatar asked Nov 26 '17 13:11

Szymon Rozga


People also ask

Can you delete ephemeral messages?

Deleting messages If using a bot user token, only messages posted by that bot user, or those posted as the app, may be deleted. Ephemeral messages can't be deleted in this way.

Can you edit ephemeral messages discord?

As of today, ephemeral messages can only be edited if they were created as an interaction response. This can be done via Edit Original Interaction Response endpoint.

How long do ephemeral messages last discord?

ephemeral ( bool ) – Indicates if the message should only be visible to the user who started the interaction. If a view is sent with an ephemeral message and it has no timeout set then the timeout is set to 15 minutes.


2 Answers

I searched high and low on how to do this and finally came across the answer.

  1. Your ephemeral message must trigger an action, i.e. button click.
  2. Your response to the action must use the following body

    {
        'response_type': 'ephemeral',
        'text': '',
        'replace_original': true,
        'delete_original': true
    }
    

'delete_original': true is the key here, which as far as I can tell is not mentioned in any of the API guides, however it is present in the API field guide under Top-level message fields

If you wish to change the response_type of your message instead of deleting it, you must do so by first deleting the ephemeral message and then posting the same message with 'response_type': 'in_channel'.

In my use case I wanted to take an ephemeral message and repost it with the exact same message body as an in-channel message. I have not found a way to retrieve the content of your ephemeral message, so the best method I've found is to pass whatever necessary data spawned your ephemeral message in the button's value so that your action handler can read this data and dynamically recreate the message body.

In my case, this was the user input being used to perform a query. On the off chance that data in the database changes between the time the original ephemeral message is posted and the in-channel version is posted they will be different. You may be able to send a JSON string directly through this value field and avoid making additional database calls and running the risk of messages changing when posted to the channel. The character limit of value is 2000 so JSON passing is extremely limited.

Assuming you use the same code to generate this body when initially creating the ephemeral message and also when recreating it in-channel, you should receive the same body and essentially are able to change an ephemeral message to in-channel message.

like image 152
Benjamin Vogler Avatar answered Nov 12 '22 08:11

Benjamin Vogler


Some ephemeral messages can be "soft" deleted/replaced but only when posted as part of a message with interactive features like buttons or menus. When a button is clicked or a menu selection made, you have a chance to instruct Slack to either "delete" the original message, or replace it with a new one. These docs detail using responses and response_url to accomplish that.

A message created with chat.postEphemeral that itself has no interactive features can never be explicitly deleted. Once it's delivered, it's like a ghost and will disappear following a restart or refresh.

Answering your bulleted questions in order:

  • Correct, you essentially start a new chain of interactivity with net new ephemeral message you post to that user
  • Each interactive message interaction will have its own response URL. The new ephemeral message won't have a response_url you can use until the end user presses a button, selects a menu item, etc.
  • response_url will eventually expire ("using the response_url, your app can continue interacting with users up to 5 times within 30 minutes of the action invocation.") If the original message is non-ephemeral, using chat.update is a better strategy for longer timelines. With ephemeral messages, it's more of a "do your best" strategy. They'll eventually get cleaned up for the user after a refresh.
  • I think you have a good handle on what's best. Personally, I think it's easier to kick off a new "in_channel" message by using chat.postMessage instead of as a chain effect directly from a slash command or interaction.
like image 27
Taylor Singletary Avatar answered Nov 12 '22 07:11

Taylor Singletary