In the previous post I described how easy it is to use Microsoft Flow to interact with Dynamics 365 Customer Engagement, by letting Azure Functions handle the core logic. In this post, I will show how to integrate Slack to Dynamics 365 Customer Engagement using Flow and Functions.
This is the objective: In my Slack channel, I quickly want to query the record count using a slash command, without have to jump into XrmToolBox/Dynamics 365 Customer Engagement application itself. I took the record count as a simple use case. You can create multiple slash commands, with each one doing a different targeted action in Dynamics 365.
The first step is to create the new app in Slack. Navigate to https://api.slack.com/apps/new
Since this is an internal app that I won’t be distributing, I am choosing a simple name. If you plan to distribute this app, choose a more appropriate name.
Now you will be taken to the app’s initial config screen.
We will be creating a new slash command that will return the record count of the entity from Dynamics 365 Customer Engagement. Click on “Create a new command”
Choose the name for the slash command. I am just going with “/count”.
The critical part here is the Request URL. This the URL that Slack will POST to with some information. What is the information and how does this look like? I used RequestBin* (see footnote) to find out this information.
Note the two relevant parameters:
- command – This the actual slash command the user executed
- text: This is the text that comes after the slash command
For e.g., if I typed “/count account” into the Slack chat window, the command parameter’s value will be “/count” and the text parameter’s value will be “account“. During the development phase, I put in the RequestBin’s URL in the Request URL. We will come back later, once the Flow is complete and replace this placeholder URL, with the actual Flow URL.
Now you can see the list of slash commands in this app.
Now click the “Basic Information” screen on the left, and then on “Install your app to the workspace”. This should expand the section, and you can now actually install the app into your workspace by clicking on “Install App to Workspace”.
Grant the required permissions for the app.
Now it is time to develop the Flow, which looks very similar to my previous post about Flow and Functions. The difference here is, that the Flow is triggered by HTTP POST, and not manually using a Flow button. Flow will receive the slash command from Slack. Here is what the Flow looks like.
Here is what the Flow does:
- When HTTP POST request is received from Slack, it posts a message back to Slack asking the user to wait while the record count is retrieved.
- Checks if the slash command is “count”
- If the slash command is “count”, call the Azure Function using the custom connection (refer previous post, on how to do create a custom connection to the Azure Function that you can use in Flow)
- Parse the response received from Azure Function, which queries Dynamics 365 Customer Engagement for the entity’s record count
- Send a mobile notification that shows up if the user has Flow app installed
- Send a message back to the channel that the slash command was executed on, with the record count
There are three important bits in the Flow:
The first is getting the slash command from the POST message.
The second is posting into the right Slack channel i.e. the channel that was the source of the slash command. You can get the channel from the “channel_name” parameter.
The third is parsing the JSON returned by the Azure Function. This is schema of the JSON returned.
{ "type": "object", "properties": { "entityName": { "type": "string" }, "count": { "type": "number" } } }
You can get the Flow URL by clicking on the HTTP step that is the first step of the Flow.
Grab the whole HTTP URL and plug it in on the slash command’s request URL.
Now, you can use the slash command on your workspace to get the record count.
Note: When I worked on this post last month, RequestBin had the capability to create private bins. But, when I looked into this again this week it looks like they have taken away this capability, due to abuse -> https://github.com/Runscope/requestbin.
You would have to self-host to inspect the POST message from Slack. The other option is to create the Flow with just the HTTP request step and look into the execution log, to see what was posted like below.