How to split Slack messages from same user? - slack-api

I'm using the Slack API to post automatic messages with various statuses. To mark the status, I use emoji icons. Sending works fine, and the right icon is set (I see it in the response), but subsequent posts are running together in the channel, so even if even if the icon is different, it does not show until there is a message from another user in between:
[red icon] BOT_USER msg #1 some info - status critical
msg #2 some info - status ok (should have green icon!)
msg #3 some info - status critical
[user icon] SOME_USER some message
[green icon] BOT_USER msg #4 some info - status ok
(sorry, not enough rep to post a screenshot)
Is there a way to split the messages, ensuring the icon is always displayed? If not, is there a way to e.g. change a message's background color?

I found the way to mark it using the attachments. My script looks like that (quoting gave me some headache):
ATTACHMENTS="[{\"fallback\":\"$INPUT\",\"text\":\"$INPUT\",\"color\":\"$COLOR\"}]"
curl -sS -X POST \
--data "token=$TOKEN&channel=$CHANNEL_ID&username=$FROM&attachments=$ATTACHMENTS" \
https://slack.com/api/chat.postMessage
Setting the $COLOR variable to danger, good, or a hex color, provides a vertical bar of that color next to the message, which works for me. So, even if the messages get grouped by user, the bars provide distinction.

The grouping of message from the same user is standard behavior from Slack and can not be changed. So it makes sense not to use the icon and name of a user as indicator, but instead look for other approaches.
Colors in attachments is one alternative approach. Another is to use thumbnails in attachment (instead of user icons).

Instead of text, u can utilize blocks & blocks of type divider can be used before & after a message.
"blocks": [
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "This is unquoted text\n>This is quoted text\n>This is still quoted text\nThis is unquoted text again"
}
},
{
"type": "divider"
}
]

Related

API POST Endpoint for Inline Pull Request Comments?

I’m hoping someone can help me be able to post inline comments on PR’s. In the UI version of Bitbucket, you can post a comment on a specific line, for example, line 6 (see screenshot):
However, when I try to send a POST request to the API, it won’t do an inline comment. All I can get it to do so far is post the comment at the end, rather than on a specific line, like line 6, as shown in this screenshot:
The POST endpoint I’m using is (https://api.bitbucket.org/2.0/repositories/{workspace}/{repo}/pullrequests/6/comments) and I’m sending the following information in body:
{
"content": {
"raw": "will this work on one line?"
},
"inline": {
"from": 6,
"to": 7,
"path": "style.css"
}
}
No matter what I put in the “from” and “to” it always puts the comment at the end, like in the 2nd screenshot, rather than inline, like it does in the UI (1st screenshot). Does anyone have any suggestions for getting the POST request to do inline comments like the UI version allows?
The API documentation tells you how to use them. You should use either-or. In your example you are trying to write on old file line 6, which will be the value taken, which is at the end of the snippet, where the comment is displayed like you show.

Unfurl links blocks template

I'm using the link_shared events to unfurl links in my workspace, trying to generate a template that is as close to Slack's unfurling template as possible, but I have several issues -
Blocks have very large spacing between them, causing my 3 blocks to take a lot of space
I'm unable to have an image inlined with the text for the title, unless I'm using context, but this is causing the text to be very small.
Taking Slack's example of how link unfurling should look like and trying to mimic it with blocks should explain the differences. This is the blocks message, and here you can see the result as an image
So my main question is - does Slack use some internal blocks formatting not available in the API, or is it possible to achieve the same result?
Thanks a lot!
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":pager: *Slack*"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*<https://slack.com/features|Features>*"
}
},
{
"type": "image",
"title": {
"type": "plain_text",
"text": "Slack is where work flows. It's where the people you need, the information you share, and the tool you use come together to get things done.",
"emoji": true
},
"image_url": "https://a.slack-edge.com/13f94ee/marketing/img/homepage/self-serve-campaign/unfurl/img-unfurl-ss-campaign.jpg",
"alt_text": "Slack"
}
]
}
That example is not using the Slack block unfurl - it's an example of how a generic link would be displayed using the page's meta tags to display some additional information, using the favicon image.
If you wanted to create something similar you could use use a markdown block and an image block (like this) - but the file size would be displayed on a new line rather than after the text.
It took a bit of playing around, but I realized Slack is actually using message attachments (the legacy version of message formatting) in order to generate their link unfurls.
For example, if you want to unfurl a GitHub repository link, this is the payload you should send, and it'll generate an almost identical unfurling to what Slack is generating (a small Added by {app-name} will be added to the footer) -
unfurls["https://github.com/slackapi/bolt-js/"] = {
author_name: "GitHub",
author_icon: "https://a.slack-edge.com/80588/img/unfurl_icons/github.png",
title: "GitHub - slackapi/bolt-js: A framework to build Slack apps using JavaScript",
title_link: "https://github.com/slackapi/bolt-js/",
text: "A framework to build Slack apps using JavaScript. Contribute to slackapi/bolt-js development by creating an account on GitHub.",
image_url: "https://opengraph.githubassets.com/3e06f7eee96f05a53cd4905af3b296dfe333be7a902bb3e6a095770e87fd17fe/slackapi/bolt-js"
}

Identifying messages posted by my app in Slack

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.
you may know id your bot if use context. Example: const {botUserId} = context

Multi line title in push notification for iOS

I'm integrating FCM for an app which has both iOS and Android version.
Currently, for iOS, I want to send title in multiple lines. How do I do this? I'm trying out using Firebase console to test. But nothing seems to work for me.
I tried with these
\n
<br/>
\\n
\r\n
as suggested in this post. But nothing really worked.
I basically want the first 2 lines of the notification to be in bold. Any help much appreciated.
The 1st line in bold you see is a notification Title, the second the Subtitle, and the Content underneath (regular font).
These are properties of a notification, since iOS 10 set on UN(Mutable)NotificationContent (subtitle line is not available before).
They properties can be configured in the payload sent via APNS:
{
"aps": {
"alert": {
"title": "Custom title",
"subtitle": "Custom subtitle",
"body": "Custom message"
}
}
}
Also, you can create a UserNotificationServiceExtension to update the above properties of a notification that has been received.

Look a message's text from events api response

I'm using slacks events API and have setup a subscription to the reactions_added event. Now when a reaction is added to a message, slack will send me a post body with all the details of the dispatched event as described here.
The problem I'm having is that I want to get the details, specifically the text of the message that my users have reacted to so I can parse/store etc that specific message. I assumed the message would return with some type of UUID that I could then respond to the callback and get the text, however I'm find it difficult to get the specific message.
The only endpoint I see available is the channels.history, which doesn't seem to give me the granularity I'm looking for.
So the tl;dr is: How do I look up a via slacks API, a messages text sent from the events API? Give the information I have the event_ts, channel and message ts I thought would be enough. I'm using the ruby slack-api gem FWIW.
You can indeed use the method channels.history (https://api.slack.com/methods/channels.history) to retrieve message from a public channel . The reaction_added dispatched event includes the channel ID and timestamp of the original message (in the item) and the combination of channelId + timestamp should be unique.
Be careful that you use the correct timestamp though. You need to use item.ts not event_ts
Full example dispatched event from the docs:
{
"token": "z26uFbvR1xHJEdHE1OQiO6t8",
"team_id": "T061EG9RZ",
"api_app_id": "A0FFV41KK",
"event": {
"type": "reaction_added",
"user": "U061F1EUR",
"item": {
"type": "message",
"channel": "C061EG9SL",
"ts": "1464196127.000002"
},
"reaction": "slightly_smiling_face"
},
"event_ts": "1465244570.336841",
"type": "event_callback",
"authed_users": [
"U061F7AUR"
]}
So calling channels.history with these values set should work:
latest = item.ts value
oldest = item.ts value
inclusive = 1
channel = item.channel value
If you want to get messages from a private channel you need to use groups.history.
https://api.slack.com/methods/channels.history

Resources