Send message to self-Chat in Microsoft Teams using Graph API - microsoft-graph-api

I'm trying to send a message to my self-Chat in Microsoft Teams through a Graph API call but can't find the ID of this specific chat. For the record, this type of chat was introduced to Teams in June, 2022.
By reading the Graph API documentation, it's possible to list all chats available for a specific user using the following API call (in this case, myself):
[GET] https://graph.microsoft.com/beta/me/chats/
Yet, I can't seem to find my self-chat in there. The chat itself is already created since I wrote messages in it but it doesn't appear in the call response.
I've tried to filter the results by most recent results, by filtering on my own name or by filtering by ChatType, but it was still missing.
Is anyone aware of a way to get the ID of a user self-chat in Microsoft Teams?
Thanks!

Self chat is a special kind, You can use this endpoint to communicate with it:
https://graph.microsoft.com/v1.0/me/chats/48:notes/messages
Hope that helps :)

Answering harrywyn's question regarding the pop up notification, you can set it as unread the same as any chat like this:
EndPoint = f'https://graph.microsoft.com/v1.0/chats/48:notes/markChatUnreadForUser'
update_chat = {
"user": {
"id" : uid,
"tenantId": TENANT_ID
}
}
resp = requests.post(EndPoint, headers=headers, verify=False, json=update_chat)

Related

Microsoft teams messaging extension inconsistently sending a valid conversation ID to use in a graph call

I am developing a MSTeams application, and inside I use a messaging extension. Upon opening the extension, a request is fired over to my message handler, which I use an azure function to handle. Alongside the request is a payload, with details about the context (in this case the conversation or chat) of where the messaging extension was opened from.
Now, I've built up a graph URL with the conversation ID from the payload:
const id = context.req.body.conversation.id
const graphEndpoint = encodeURIComponent(`https://graph.microsoft.com/beta/me/chats/${id}/members`)
I authenticate a user by calling microsoftTeams.authentication.authenticate({...}) before I make the call, and use the token in the request.
Sometimes, this call will succeed, and return the information I want. However, the other times it will fail with a 400, telling me I had a bad request, despite it being a GET request with no body.
I notice in bad requests, that the conversation ID doesn't trail with #thread.v2 or #unq.gbl.spaces etc. I have no clue why this is so inconsistent, or if it's my fault. Any help would be appreciated.
EDIT: I have also seen that the issue only occurs when the id starts with a:, and succeeds when it starts with 19:. However, the context in which I open the messaging extension is the same each time: In a 1:1 / User:User chat.
I have previously implemented installing the bot in the conversation to get this information, but this method is very undesirable. Perhaps a side point - it seems that the conversations where I have previously installed the bot seem to return the 19: id, and everything else a:.
Here's an example of the 400 response:
{
"error": {
"code": "BadRequest",
"message": "Bad Request",
"innerError": {
"date": "2021-01-25T09:43:26",
"request-id": "3bb55aa2-e694-4c80-952c-88842f482dc1",
"client-request-id": "3bb55aa2-e694-4c80-952c-88842f482dc1"
}
}
}
The conversation id you received in the turn-context is not the chat-id. The conversation id is different from conversation id. Conversation id the id between bot and the user and chat id the id of the chat. Both are different. You cannot use conversation id to call the graph API. Please use the chat id to call graph API. You can get the chat id using list chats API.

How to download Teams chat message attachments using the Microsoft Graph Beta API?

A #microsoft.graph.downloadUrl is not available for chat message attachments using the Microsoft Graph Beta api.
I recently posted an issue in the msgraph-sdk-javascript repo concerning downloading Microsoft Teams chat message attachments through the beta graph api.
https://github.com/microsoftgraph/msgraph-sdk-javascript/issues/200
The issue is that there is currently no way to obtain a #microsoft.graph.downloadUrl for these chat attachments. I have posted a workaround there that is as follows but that requires an additional api call and is possibly fragile as it involves parsing the chat attachment's contentUrl.
function getDownloadUrl(attachment: ChatMessageAttachment, team?: Team): string {
const url = new URL(attachment.contentUrl as string);
const path = url.pathname.split('/').slice(4).join('/');
if (team) {
return `/groups/${team.id}/drive/root:/${path}`;
} else {
return `/me/drive/root:/${path}`;
}
}
I was asked by a microsoft-graph-javascript maintainer to post my question here.

How to access data field in Dialogflow?

As the Dialogflow documentations states, the data field represents
Additional data required for performing the action on the client side.
The data is sent to the client in the original form and is not
processed by Dialogflow.
How should one access it in the iOS framework?
request?.setMappedCompletionBlockSuccess({ (request, response) in
...
}
I couldn't find it in the response object and can't find any documentation for iOS.
Thanks.
Your question is a bit vague (can you edit and narrow it down?), but i think you got it the other way round, what that snippet of documentation that you pasted means is that you are supposed to send that payload to DialogFlow and it will forward it to a connected Client (e.g Messenger, Slack etc) un-touched. It simply means that DialogFlow assumes that you know what you are doing.
Here is a sample Fulfilment response to DialogFlow in JS
module.exports.sendGenericMessageWithText = function(message) {
return {
data: {
facebook: [
{
text: message
]
}
}
}

Twilio Chat getChannels order by most recent message

In twilio chat, is there a way to specify an order to the getChannels() method? Or is there a property on the Channel object that will tell me when the last message sent on that channel was? The dateUpdated property on Channel seems to be when properties on the channel were updated, not including messages sent/received.
I would like to order my channels list by the most recent messages. And I would like to do this without having to retrieve all the messages first.
You can add the attributes parameter upon updating a channel.
An optional string metadata field you can use to store any data you
wish.
You could track time/date info of messages here.
# Update the channel
service = client.services.get(sid="CHANNEL_SID")
channel = service.channels.create()
response = channel.update(friendly_name="NEW_FRIENDLY_NAME", attributes="ANY_DATA_YOU_WISH")
print(response)
You should then be able to subscribe to a channel event (JavaScript SDK example). As you did not specify what language you're using you will also find more details in the API Docs for iOS and Android SDKs as well.
// A channel's attributes or metadata have changed.
messagingClient.on('channelUpdated', function(channel) {
console.log('Channel updates: ' + channel.sid);
});

How can a Slack bot detect a direct message vs a message in a channel?

TL;DR: Via the Slack APIs, how can I differentiate between a message in a channel vs a direct message?
I have a working Slack bot using the RTM API, let's call it Edi. And it works great as long as all commands start with "#edi"; e.g. "#edi help". It currently responses to any channel it's a member of and direct messages. However, I'd like to update the bot so that when it's a direct message, there won't be a need to start a command with "#edi"; e.g. "#edi help" in a channel, but "help" in a direct message. I don't see anything specific to differentiate between the two, but I did try using the channel.info endpoint and counting the number of people in "members"; however, this method only works on public channel. For private channels and direct messages, the endpoint returns an "channel_not_found" error.
Thanks in advance.
I talked to James at Slack and he gave me a simply way to determine if a message is a DM or not; if a channel ID begins with a:
C, it's a public channel
D, it's a DM with the user
G, it's either a private channel or multi-person DM
However, these values aren't set in stone and could change at some point, or be added to.
So if that syntax goes away, another way to detect a DM to use both channels.info and groups.info. If they both return “false” for the “ok” field, then you know it’s a DM.
Note:
channels.info is for public channels only
groups.info is for private channels and multi-person DMs only
Bonus info:
Once you detect a that a message is a DM, use either the user ID or channel ID and search for it in the results of im.list; if you find it, then you’ll know it’s a DM to the bot.
“id” from im.list is the channel ID
“user” from im.list is the user ID from the person DM’ing with the bot
You don’t pass in the bot’s user ID, because it’s extracted from the token
FYI as of July 2017, for "message.im" events (via your app's Event Subscriptions), the event payload seems to now return additional fields to detect if the message is coming from your own bot (pasted in here from my logs):
INFO[0012] got Slack message: (bot.SlackMessage) {
SlackEvent: (bot.SlackEvent) {
Type: (string) (len=7) "message",
EventTs: (string) (len=17) "1501076832.063834",
User: (string) ""
},
SubType: (string) (len=11) "bot_message",
Channel: (string) (len=9) "D6CJWD132",
Text: (string) (len=20) "this is my bot reply",
Username: (string) (len=15) "Myapp Local",
BotID: (string) (len=9) "B6DAZKTGG",
Ts: (string) (len=17) "1501076832.063834"
}
Slack have added Conversations API some time ago. You should use it to differentiate between PM/channel instead of relying on prefix.
From Conversations API documentation:
Each channel has a unique-to-the-team ID that begins with a single letter prefix, either C, G, or D. When a channel is shared across teams (see Developing for Shared Channels), the prefix of the channel ID may be changed, e.g. a private channel with ID G0987654321 may become ID C0987654321.
This is one reason you should use the conversations methods instead of the previous API methods! You cannot rely on a private shared channel's unique ID remaining constant during its entire lifetime.
Get conversation info using conversations.info method and check is_im flag. is_im == true means that the conversation is a direct message between two distinguished individuals or a user and a bot.
The info function is also available for private channels with the Slack API method groups.info. This works also for direct message channels with multiple participants, since they are a special form of private channels.
You can use groups.list to get the IDs of all private channels incl. direct message channels with multiple participants.
Note that groups.list will only return private channels, that the user or bot that the access token belongs to has been invited to.

Resources