Dynamics 365 Business Central and Microsoft Teams integration part 2

About one week ago I’ve written a post on how to integrate Dynamics 365 Business Central with Microsoft Teams and after that post I’ve received a lot of questions (so surprised that the topic was so hot).

My goal was to write a message to a Teams channel from Dynamics 365 Business Central because I think that (at least for my customer’s base) this is the most interesting and useful scenario: when something important happens on Dynamics 365 Business Central, I want a notification on a particular Teams channel for react to that event.

Many of you have requested me also the opposite way: how can I read a message posted on a Teams channel (or maybe a reply to a message that I’ve previously posted on the Teams channel from Dynamics 365 Business Central) and then react to that message from Dynamics 365 Business Central?

D365BCToTeams

Here the things are a bit more complicated  I think that if you want to react to what happens (or what someone writes) on a Teams channel from Dynamics 365 Business Central you have essentially two ways:

  1. Write a bot for Microsoft Teams (there’s a great .NET SDK for that) that reacts to messages on a channel and then calls a business logic exposed from Dynamics 365 Business Central as web services or APIs.
  2. Use Microsoft Graph APIs from Dynamics 365 Business Central.

In this post I want to talk about the second option (first option maybe in the future  ) because it’s the direct way that you can use also from AL code.

Microsoft Graph is a RESTful web API that allows you to access Microsoft Cloud and Office 365 resources. Using Microsoft Graph APIs essentially requires the following steps:

  1. You need to register your app
  2. You need to get authentication tokens for a user or service
  3. You need to send HTTP requests to Microsoft Graph endpoints from AL by using HttpClient and by passing the authentication token in the request header:
    HttpClient.DefaultRequestHeaders.Add('Authorization', 'Bearer ' + token);

Said that, you can test Microsoft Graph APIs by using the Graph Explorer tool. This tool is in my opinion a must to use before using Graph APIs in your application because it permits you to discover the permissions you need to set for your objects (Microsoft Graph is quite tricky about that).

Microsoft Graph APIs are in version 1.0 (the default to use) but some features are actually available only on the BETA version (so you need to set the version to use in your API endpoint).

What can we do now with Graph APIs and Teams?

You can see what Teams you’ve joined by sending a GET http call to the following URL:

https://graph.microsoft.com/v1.0/me/joinedTeams

GraphAPITeams_01.jpg

As you can see, the response is a JSON document with the ID of an entity (that you need to use for retrieving details for that entity in next calls) and with details for that entity. In the JSON above, you can see that I’m subscribed to two Teams.

To retrieve a list of channels where the user is subscribed for a specific Team, you need to send a GET http call to the following endpoint (where teamid is the ID of the team retrieved on the previous call):

https://graph.microsoft.com/v1.0/teams/<teamid>/channels

GraphAPITeams_02

To retrieve the details of a particular channel, you’ve to send a GET http request to the following endpoint:

https://graph.microsoft.com/v1.0/teams/<challedid>/channels/<channelid>

where channelid id the ID of the channel that you want to retrieve (as returned by the previous call):

GraphAPITeams_03.jpg

Now we want to work with messages on a channel. The 1.0 Graph API doesn not actually permit to work with messages, so for that scope we need to use the BETA api version.

To retrieve a list of messages posted on a channel, you need to perform a GET call to the following endpoint:

https://graph.microsoft.com/beta/teams/<teamid>/channels/<channelid>/messages

GraphAPITeams_04.jpg

To retrieve a particular message, you need to perform a GET call to the following endpoint:

https://graph.microsoft.com/beta/teams/<teamid>/channels/<channelid>/messages/<messageid>

where messageid is the ID of the message to retrieve:

GraphAPITeams_05.jpg

This is the JSON content of the message I’ve sent to Teams from my Dynamics 365 Business Central extension I’ve sent in the previous post.

If you want to check the reply messages to this post, you’ve to send a GET http request to the following url:

https://graph.microsoft.com/beta/teams/<teamid>/channels/<channelid>/messages/<messageid>/replies

GraphAPITeams_06.jpg

As you can see, the message previously sent from Dynamics 365 Business Central to my Teams channel has a reply from a user (Stefano Demiliani) with the content in the red box.

To retrieve the details of a specific reply message, you’ve to send a GET http request to the following url:

https://graph.microsoft.com/beta/teams/<teamid>/channels/<channelid>/messages/<messageid>/replies/<replyid>

where replyid is the ID of the reply message to retrieve.

The JSON of the reply content is like the following:

{
"@odata.context": "https://graph.microsoft.com/beta/$metadata#teams('bc273420-e9ce-47fb-821f-6d439f9ec3b5')/channels('19%3A399db078120b47feab6cb2c072a94985%40thread.skype')/messages('1550679628007')/replies/$entity",
"id": "1551178342565",
"replyToId": "1550679628007",
"etag": "1551178342565",
"messageType": "message",
"createdDateTime": "2019-02-26T10:52:22.565Z",
"lastModifiedDateTime": null,
"deleted": false,
"subject": null,
"summary": null,
"importance": "normal",
"locale": "en-us",
"policyViolation": null,
"from": {
"application": null,
"device": null,
"conversation": null,
"user": {
"id": "2605a70a-a1ad-4a5b-9cec-620c56d50e51",
"displayName": "Stefano Demiliani",
"userIdentityType": "aadUser"
}
},
"body": {
"contentType": "text",
"content": "Thanks D365BC! Message arrived."
},
"attachments": [],
"mentions": [],
"reactions": []
}

By parsing this JSON message, you’ve all what you need (author, content, attachments, mentions etc.) for handling a reply to your message sent to Teams from Dynamics 365 Business Central.

Possible scenarios? Many

You can search for messages and then react to replies (a reply can start a Dynamics 365 Business Cenral process, for example order authorizations and so on).

Many doors are open here, how to use these features is just your fantasy

P.S. At the time of writing this post, unfortunately OData Query parameters are not currently supported, so you can’t directly retrieve a particular reply message by applying a filter via query string. You need to send the query as show above and parse the JSON response.

 

Comment List
Related
Recommended