I'm developing a Slack app that posts alert apps to channels. I want this app to check the history of a channel to find messages it has posted earlier so it can respond accordingly. For example, if there's an alert that has not yet "cleared" it will update said alert instead of posting a new message.
The challenge I'm encountering is that it's not clear how I can identify messages that my app has posted. I see that I can search a channel with conversations.history, and that gives me message events. It looks like some messages have a user
property. There are also bot_message
sub-type messages that have a bot_id
property. However, I don't see any way to identify my app ID.
Should every app have an associated bot_id
? user
ID? If so, where do I get these IDs so I can filter the conversation history?
Update
I tried calling the bots.info
method with no bot ID parameter hoping it would give me my bot ID, but it returned no data other than an "OK" status.
Perhaps because Slack has a long history of different APIs, I was misled. Apparently, it's possible for me to find messages my bot previously posted but not how I thought. Here were my misunderstandings and what I've found out when playing with the Slack API tester.
Using conversations.history
, you can get a list of messages posted in a channel. The docs say that the history returns an array of message events
, and that these have a subtype field. One of the subtypes is bot_message
, so my assumption is that messages posted by my bot would have this sub-type. The docs for bot_message
has a bot_id
, which I don't know for my app, and username
, which I don't know what it will match.
However, it turns out when I posted a test message, that the message did not show up as a bot_message
; rather it appears in the history without a subtype and has properties which don't seem to match any documentation:
{
"bot_id": "B01HSBYRKUZ",
"type": "message",
"text": "Testing the Slack API; please ignore.",
"user": "U01HDNUJ5EE",
"ts": "1609878469.036400",
"team": "<omitted>",
"bot_profile": {
"id": "B01HSBYRKUZ",
"deleted": false,
"name": "my-bot-name",
"updated": 1608584973,
"app_id": "<omitted>",
"icons": {
"image_36": "...",
"image_48": "...",
"image_72": "..."
},
"team_id": "<omitted>"
}
}
So although it's risky to code against an undocumented format (or maybe I just can't find the right docs?), I can filter these messages by looking to see if there's a bot_profile.app_id
that matches my app's ID, which I do know.