Search code examples
slackslack-apislack-commands

Unable to get timestamp of the message posted by Slack App


Whenever my app posts ephemeral message to Slack channel (in response to a query by a user), I am unable to get the timestamp of my Slack app response. As I want to delete it once the user has made a selection using one of the buttons. Although I have subscribed to 'message.channels' event, I don't get a notification to my app whenever my app posts in the channel (in response to the user input), therefore, I am unable to get the timestamp of the message which I'll use to delete it. All I want is the timestamp of the message posted by my app so that I can delete it but I am unable to receive the timestamp. Please help!

For e.g. in Giphy app for Slack. Let's say the user invokes the app by calling '/giphy [dog]' where 'dog' is just an example of a search term. The app responds by sending a gif and user can either send it, shuffle to the next one or cancel it. I want a similar capability of cancelling the app response but I need the timestamp of the message in order to do so therefore I am asking for help.

Thanks.


Solution

  • Your approach can not work, because Slack is handling ephemeral messages differently from "normal" messages. They are only visible by one user and can not be modified by API methods (e.g. deletion).

    But of course its possible to replace ephemeral messages. Here is how:

    Your app can just reply to the interactive message request from Slack with a new message. That new message will by default override the original message including ephemeral messages.

    You can reply in two ways:

    • Directly reply to the request from Slack with a message within 3 seconds
    • Send a message to the response_url from the Slack request within 30 minutes.

    See here for the official documentation on how to respond to interactive messages.

    This approach works both with interactive messages and slash commands.

    See also this answer for a similar situation.