cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Josverl
Level: Powered On

Microsoft Teams and Flow: Use information from a new Message trigger as input to other actions

In order to minimize the need to reconfigure different actions as flows are moved between environments and users, I have looked for a way to use the inputs for the trigger, as the inputs for subsequent actions.

As an example; When I create a flow that triggers on a new message in a Teams-Channel, how can I reply back to that message, without the need to re-specify that same Team and Channel each time that I :

  • Import the Flow to a different tenant/environment
  • Change the Team
  • Change the Channel

While there are plans to bring such capability to Flow in the future, I'm not widely known for my patience and time waits for no Feature.
So I found a way to implement this today.

The below example is for the Teams trigger action 'when a new item appears in a channel'.
This same technique can be used, with some exploration and a few adoptions to most, if not all, other Triggers.

The Short and Simple version

  • You can get at the inputs for the trigger via the expression : trigger()
    this provides access to not only the output, but also to the inputs of the trigger.
  • The 'inputs' in turn contains:
    • a 'path' property : trigger()?['inputs']?['path']
    • path": "/trigger/beta/teams/db80860b-4456-4fab-a839-a91a24af8f67/channels/19%3A317b30a442174b1ea82163d2df324e78%40thread.skype/messages"
  • The path is a (partial) url, that contains the urlencoded information; in this case the interesting info is:
    • Teams-ID at level 4 = db80860b-5555-4fab-a839-a91a24af8f67
    • Channel-ID at level 6 = 19%3A317b30a123456b1ea82163d2df324e78%40thread.skype
  • when we split() the path on the / delimiter, the result is an array that makes is quite simple to access these nodes
    • Teams-ID : array[4]
    • Channel-ID: array[6]

Option 1) Variable Assignment

If you put that all together you get expressions such as the two shown below You can use this expression in a variable assignment, or type/paste them directly where you need to access the information.

To get the Teams-ID that triggered the Flow use: @{ split(trigger()?['inputs']?['path'], '/')[4] }

And similar for the Channel-ID at the 6th level of the path: @{ split(trigger()?['inputs']?['path'], '/')[6] }

So-far I found that there is no need to url-decode the IDs, as in most cases the would be re-encoded as they are used again in URLs.

Variable.png

 

Option 2) Add a 'Compose' action

It is also possible to get both the Teams and channel ID's at the same time, by using a Compose , or a JSON action Although the Message-ID is already available in the trigger body, it is sometimes hard to pick out as the name is just 'id', so while we are at it , lets also add that .

Copy/paste the below into a Compose action to get the Triggers information

    // Compose Inputs
    {
        "Team-ID": "@{ split(trigger()?['inputs']?['path'], '/')[4] }"  ,
        "Channel-ID": "@{ split(trigger()?['inputs']?['path'], '/')[6] }" ,
        "Message-ID": "@{ triggerbody().id }"
    }

 

Compose.pngThe advantage of compose is that it will get you access to all three pieces of information at the same time, but you need to remember to specify outputs('Compose')?['Teams-ID'] or outputs('Compose')?['Channel-ID'] each time.

 

 

 

Note:

In my exploration phases, I frequently use the Compose to explore/dump multiple expressions as I try them out. In those cases , leave out the { } at top and bottom to avoid json validation, then compose (usually) will treat whatever you put in there as just a large string.

 

Option 3) Add a 'Parse JSON' action

To make life a bit simpler for myself and other, I like to use of the 'Parse JSON' action. this will show all three attributes in the 'Dynamic Content' pop-up, so you can simply re-use these wherever you need.

  1. create a new json action
  2. Copy the below json expression
  3. paste this into the Content
  4. Use Sample payload to generate schema,
  5. Paste the same into the sample box, and press [OK] to generate the schema
  6. Remove or comment the 'string' type of the message-ID to make it visible in more places where you need it

Parse JSON expression (same as for compose):

    {
        "Team-ID": "@{ split(trigger()?['inputs']?['path'], '/')[4] }"  ,
        "Channel-ID": "@{ split(trigger()?['inputs']?['path'], '/')[6] }" ,
        "Message-ID": "@triggerbody().id"
    }

Parse_Trigger.png

By using the JSON pars method you will get simpler access to the Trigger information as the results are shown in the variable picker. In addition the variables show clearly in the designer, so you will not need to hover over the variable to catch mistakes.

 

Picker_and_reply.png

 

 

Note:
The schema for the 'Parse JSON' actions will get auto generated, so the below is mostly for your reference. If you find that some items do not show in the picker, it may be due to a type conflict. i.e. Flow assumes that the Message-ID is a number, not a string. I find it best to remove the type to make the Message-ID show up where I want to use it.

    {
        "type": "object",
        "properties": {
            "Team-ID": {
                "type": "string"
            },
            "Channel-ID": {
                "type": "string"
            },
            "Message-ID": {
                // remove/comment the type of the message-id 
                // "type": "string"
            },
        }
    }

 

Conclusion

Using the above techniques you should be able to make better re-usable flows, and also avoid the need for redundant information.

Call-Graph.pngOne of the many other uses is to use the Team-IS and Channel-ID to call to Microsoft Graph to:

  • Get information on the Team or Channel
  • Post a message to the Channel as the Flow Bot
  • Add a Tab to the current channel
  • Create a New Channel

So, what will you build next ?

 

A last note on the Copy and Paste of expressions:

The @{ and } in the above examples should allow you to directly paste the information into flow, and flow will recognize these as expressions. I think this is more effective than just screenshots, especially as the letterbox provided to enter expressions is a bit restrictive for my taste. If you run into problems, first paste into an editor ( vscode or notepad , and double check the braces and quotation marks)

and please let me know how this works for you.

 

regards,
Jos Verlinde
Jos.Verlinde@microsoft.com - 8/8/2019

Helpful resources

Announcements
firstImage

Microsoft Business Applications Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 Release Wave 2 Plan

Power Platform 2019 Release Wave 2 Plan

Features releasing from October 2019 through March 2020

thirdimage

Flow Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Flow Community Video Gallery!

Users Online
Currently online: 230 members 4,721 guests
Please welcome our newest community members: