Look a message's text from events api response - slack-api

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

Related

Passing additional data to the action listeners along with the block_actions payload in Slack Bolt python

I am new to Slack APP development, I am developing the app using Slack bolt python. In my app I have configured the slack interaction using the button click, on clicking the button I am calling the corresponding listener(identified by action_id mentioned in the button). In the listener I am getting the block_actions payload which contains all the state values of the event, but in addition to that I want to some arguments to the listener function. Is it possible in any way that we can send additional arguments to the listeners
#app.action("change_time_period")
def handle_change_time_period(ack, context, body, client, logger, message, say):
# want the arguments here such as passing email_id from post_data
def post_data(user_id, email_id):
client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
result = client.chat_postMessage(
channel=user_id,
blocks=[
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"emoji": True,
"text": "Change time period"
},
"style": "primary",
"value": "time_period",
"action_id": "change_time_period"
}
]
}
]
)
In the code above, I will be calling the post_data which will post the message containing the block kit, once the user clicks the button the handle_change_time_period will be called
Slack block kit buttons do not have a field where we can pass metadata to the interactivity handler. One workaround to achieve this would be to make use of the value field of the button element dict to dump a JSON with the required data. From the Slack API doc, the value field can hold a maximum of 2000 characters. Your value field can be something like
"value": json.dumps({"actual_value" : "time_period", "email" : "johndoe#abc.com"})
This can be optimized further by reducing the characters in keys. If the extra data you need to pass goes beyond the char limit, you might have to consider caching the data on the server and passing a cache key in the block which can then be used in handler to retrieve the actual data from the cache.

Twilio Function: access variable from studio flow "set variable"

I'm sure this is a silly question, but I don't claim to be a developer, just someone with a wide ranging job description 😊
I am following this guide: https://www.twilio.com/blog/forward-voicemail-recordings-to-email
The transcription callback is firing correctly and sending me the email. That's great.
I'm looking to expand this to collect additional information fields through Gather widgets. I have the following being sent to my function:
{
"flow": {
"flow_sid": "FW07e11311d367...f8a0501c05e5108",
"variables": {
"CallerName": "Joe Bloggs"
},
"channel": {
"address": "+441...147"
},
"sid": "FN866c64beb9...f5bf349fa19ad3"
},
"widgets": {
"SetCallerNameVar": {
"CallerName": "Joe Bloggs"
},
"GatherVoicemail": {
"Called": "+4414....7",
"Digits": "#",
"RecordingUrl": "https://api.twilio.com/2010-04-01/Accounts/AC5fa2...12c7/Recordings/RE3a1d420de6db...2abb554c04f6",
"CallerCountry": "GB",
"Direction": "inbound",
[...]
I access the other (working) information through the ${event.variable} syntax. However, simply doing ${event.CallerName} results in "undefined". Can anyone advise how to access the CallerName variable that I have set in my flow?
If I was calling the function rather than using a transcription callback, it would be easy to pass the parameter, but doing so would result in duplicate emails per call.
I hope this makes sense and appreciate any advice.
Thank you
Twilio developer evangelist here.
The issue here is that your transcription callback does not have the same context as the Studio Flow, so does not come with all the other data. You can, however, add that context to the request by setting query parameters on the transcription callback URL.
Try setting your transcription callback URL to:
https://your-function-service.twil.io/?CallerName={{flow.variables.CallerName}}
You will then receive the CallerName in the event object.
Just to note, the Transcription Callback URL field does not highlight the liquid variable, but it does get interpolated. There is now an open issue to add the highlighting to this field.

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

microsoft-graph message ID

I call my mailbox using REST calls to MSGraph:
GET https://graph.microsoft.com/v1.0/me/mailfolders/inbox/messages
As expected I receive a Json with my mails.
To play with a specific mail (move, delete,forward, ...) I saw that the API should be called like
POST me/messages/{id}/forward
I can't target the message if I provide the value of the id field that I get from the JSon. Where to find the correct {id} that I should use?
my result is like the example at https://developer.microsoft.com/en-us/graph/graph-explorer see id: field when calling GET my mails.
Thanks
The id from JSON is OK. what you missing is you have not set the post conent for move/forward.
If you use the forward API, you should set the post content like below:
{
"Comment": "FYI",
"ToRecipients": [
{
"EmailAddress": {
"Address": "XXX#XXX.onmicrosoft.com"
}
}
]
}
This is the worked forward url for me(you can implement the move/delete like this too):
https://graph.microsoft.com/v1.0/me/messages/AAMkAGNjZTcyZDJmLTkzMjYtNGQwNi05Y2UxLTc5NDk0NjEwNjJjMABGAAAAAAAUYpVLVYiKTINMO3MZR0H5BwB3NF3PTyl5QIQd59edwRO9AAAAAAEMAAB3NF3PTyl5QIQd59edwRO9AAApBI9fAAA=/forward
For information please see:
https://learn.microsoft.com/en-us/previous-versions/office/office-365-api/api/version-2.0/mail-rest-operations#ForwardDirectly

Impossible to get messages details in a list request with Gmail API

I am using a ruby on rails app which connects to the Gmail API. When I make a listrequest to get all the messages of one mailbox, I only get back an idand a threadId property for each message.
I tried to follow Gmail API Doc using the fields parameters to get other properties (title, date...). It doesn't work, whether I use the google-api-client gem in my app, or by doing a direct GET request.
Adding any other parameters to the request ends with a failure. Here is the url that works :
https://www.googleapis.com/gmail/v1/users/me/messages?fields=messages(id,threadId)
Am I forced to make one call per message or using batch requests to get relevant datas ? It seems a little heavy...
You first need to list messages like you've done, and then get each message in a separate request.
Request 1
GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=1&access_token={ACCESS_TOKEN}
Response 1
{
"messages": [
{
"id": "15fd9f0fe242f975",
"threadId": "15fd9f0fe242f975"
}
],
"nextPageToken": "11889180580605610074",
"resultSizeEstimate": 2
}
Request 2
GET https://www.googleapis.com/gmail/v1/users/me/messages/15fd9f0fe242f975?access_token={ACCESS_TOKEN}
Response 2
{
"id": "15fd9f0fe242f975",
"threadId": "15fd9f0fe242f975",
"labelIds": [
"IMPORTANT",
"CATEGORY_UPDATES",
"INBOX"
],
"snippet": "Tasks tracked last week...",
"historyId": "966691",
...
}
It's also possible to get the total amount of request down from 1 + n of messages to 2 by using batch requests.

Resources