Episode #129 – Reading Teams messages via Microsoft Graph
Here you can find the transcript of Episode #129 of PiaSys TechBites.
Welcome back to Piasys Tech Bites. Today, I want to talk with you about how you can use Microsoft Graph to manage messages in Microsoft Teams channels and chats.
In fact, through Microsoft Graph, we can easily read the messages in a channel or in a chat. We can see the delta changes, meaning we can get the new messages since a specific point in time based on a token that we can store in a safe place. We can get the replies of a message. We can get a specific reply of a message, and we can subscribe to a notification service like a webhook which will notify our application whenever there is a new message in a chat or in a channel.
In order to do that from a security point of view, we can use a delegated access token. We can use the resource specific concept that we saw just last week, or we can use an application only token. And if it will be an application only token, we will have to register our application with Microsoft in order to get the specific permission to access and to read all of the messages in all of the channels and chats in a target tenant. So, let me move to the demo environment and let me show you how to do that in practice.
So, this is the entry point in the Microsoft Graph documentation about how to list and read the messages in a channel. And as you can see, we have operations to list the channel messages, to get the delta of channel messages, to get a specific message, or to get the reply of a specific message. And for all of these operations, we can see what the permissions are. So the permissions for a delegated access token or for an application only token, and eventually with the resource specific consent model that was just introduced by Microsoft couple of weeks ago.
So, here we also have a team where I have a general channel, and this channel, I have a thread, there is a message that I started, and then there are a couple of replies. So, let’s say that we want to use Microsoft Graph to access the messages in this channel. So first of all, using the Graph Explorer, I can make a request for all of the groups in my target tenant. And through the list of groups, I can retrieve the ID of the specific group back in the team that I’m looking for. And specifically, if I look for the collection of teams, I can make a request for that specific team.
Well, in that team, we have a collection of channels, as we know, so I can get all of the channels and specifically the general channel, which is the one that I want to target. By doing that, I get a reference to the channel and I can get all of the messages in that channel simply by using the messages endpoint. And here we are, we get the whole list of messages.
As you can see, they’re ordered from the newest to the oldest one. And here we can see the last tread that I showed you in the UI of Microsoft Teams. So let me get the ID of this message, just to show you that we can explicitly retrieve that specific message providing the ID right after the messages endpoint. And here is the message.
We can see that the message has been written by a specific user, which is me actually. We can see the HTML content of the body, and here we can see that we have a link and everything else. And we can also access all of the replies for this message simply by saying that we want to access the replies endpoint of this specific channel message. By doing that, I can see, again, the array of messages, the reply messages for these original thread. And here I can see, for every single message again, who is the author and the content, as in HTML content, and stuff like that.
I can also see the mentions. So for example, in this last message, I said, “Thank you Guido Zambarda.” Which is one of my colleagues. And here we can see that we have the HTML with the link and the @ mention for the item zero, where the item zero in the array of mentions is this one. So we have explicit information about what is the mention and who is the target of the mention.
Moreover, if we go to see all of the reply messages, we can also see that we have a reply message written by my colleague, which includes a Giphy. And here we can see how the Giphy is made inside the HTML content of the body, as well as I reacted to that message with a like. And here we can see that in the reactions array, we see that my user reacted with a like, and the supported values are like, heart, laugh, surprised, sad, and angry.
So, we have all of the options that we can see right here in the UI. So for example, let’s say that I’m surprised. Okay, by changing my status on my emotions, I can get the specific ID of this reply. And by putting this ID at the end of the URL of the replies endpoint, I can get that specific reply and I can see in the reactions that now my reaction is surprised. So, really simple and easy to do. By using these APIs, you can create solutions that can automatically read messages in channels or chats, and can improve the quality of your solutions built on top of Microsoft Teams.
Like always thank you for watching this video. I hope you found it interesting and I’m really looking forward to seeing you next week. And remember subscribe to this channel. Thank you.