# Send Messages to Microsoft Teams

Send device-triggered notifications to Microsoft Teams.

## Introduction

[Soracom Flux](https://docs.soracom.io/en/services/flux) is a low-code IoT application builder designed for advanced automation.

In this guide, we send messages to a Microsoft Teams channel using a [Soracom Flux Webhook Action](https://docs.soracom.io/en/services/flux/webhook). It walks you through setting up a seamless integration between Soracom Flux and Microsoft Teams. By configuring a Microsoft Power Automate workflow to receive and process webhook requests and by creating a Flux App complete with channels and webhook actions, you can automate the delivery of notifications to your Teams channel.

> [!WARNING]
>
> Refer to the [Pricing & Fee Schedule](https://docs.soracom.io/en/pricing#soracom-flux) for detailed information on Soracom Flux pricing. Soracom Flux includes a free tier providing limited usage for certain features at no cost.

## Requirements

For this project, you will need the following:

- A Soracom Account
  - If you don't already have a Soracom account, follow the steps in the [Quick Start guide](https://docs.soracom.io/en/guides/quick-start).

- An Account and a License for Microsoft Power Automate and Microsoft Teams

  - For more information, refer to the following:

    - [Microsoft Teams admin documentation](https://learn.microsoft.com/en-us/microsoftteams/)
    - [Microsoft Power Automate documentation](https://learn.microsoft.com/en-us/power-automate/)

- A Notification Channel in Microsoft Teams
  - [Create a standard, private, or shared channel in Microsoft Teams](https://support.microsoft.com/en-us/office/create-a-standard-private-or-shared-channel-in-microsoft-teams-fda0b75e-5b90-4fb8-8857-7e102b014525)

## Create a Workflow in Microsoft Power Automate

Build a workflow using Microsoft Power Automate that acts as the intermediary between Soracom Flux and Microsoft Teams. The workflow is designed to receive a webhook request from Flux, process the payload, and then post the message to a designated Teams channel.

1. Sign in to [Microsoft Power Automate](https://make.powerautomate.com/).

2. Click + **Create** in the left navigation panel.

   ![Screenshot of the Microsoft Power Automate console showing the Create option.](https://docs.soracom.io/_astro/power-automate01.C_Ov96df_Z2rldM.webp)

3. Click **Instant cloud flow**.

   ![Screenshot illustrating how to select Instant cloud flow in Microsoft Power Automate.](https://docs.soracom.io/_astro/power-automate02.BLQkIYr-_Bbnwe.webp)

4. Enter a **Flow name**.

   ![Screenshot highlighting the Flow Name input field in Microsoft Power Automate.](https://docs.soracom.io/_astro/power-automate03.ugY8aKni_Z2s0KPw.webp)

5. From **Choose how to trigger this flow**, select **When a Teams webhook request is received**, then click **Create**.

   ![Screenshot showing the trigger selection When a Teams webhook request is received in Microsoft Power Automate.](https://docs.soracom.io/_astro/power-automate04.DC3JX9_W_Z2tHwiB.webp)

6. Click the + button to add an action.

   ![Screenshot demonstrating how to add a new action (via the plus button) in Microsoft Power Automate.](https://docs.soracom.io/_astro/power-automate06.Q5MwEvWb_ZBc83e.webp)

7. In **Add an action**, enter **Post message in a chat or channel** and select **Post message in a chat or channel**.

   ![Screenshot of the action search panel for Post message in a chat or channel.](https://docs.soracom.io/_astro/power-automate07.SQ1r6dsL_Z3wqsC.webp)

   > [!WARNING]
   >
   > Sign in to Microsoft Teams.
   >
   > If you are not already signed in to Microsoft Teams, [sign in](https://teams.microsoft.com/v2/) and create a connection to Microsoft Teams. Click **Sign In** and follow the on-screen instructions.

8. Configure the following fields:

   - **Post as**: Select **Flow bot**.
   - **Post in**: Select **Channel**.
   - **Team**: Select the target team.
   - **Channel**: Select the target channel.

   ![Screenshot showing the configuration fields for posting a message as a Flow bot to a specific Microsoft Teams channel.](https://docs.soracom.io/_astro/power-automate08.BNMuAMlP_Z7HgAe.webp)

   > [!WARNING]
   >
   > For more information about the settings, see [Send a message in Teams using Power Automate](https://learn.microsoft.com/en-us/power-automate/teams/send-a-message-in-teams).

9. Click the **Message** input field, then click the **_fx_** button.

   ![Screenshot highlighting the selection of Message from the dynamic content list.](https://docs.soracom.io/_astro/power-automate09.Bxm4VHES_KSEEu.webp)

10. Enter `triggerBody()?['text']`, then click **Add**.

    ![Power Automate configuration showing triggerBody() function input](https://docs.soracom.io/_astro/power-automate10.CSMhWnA9_2kkYMD.webp)

    > [!WARNING]
    >
    > The `triggerBody()` function retrieves the HTTP body from the trigger such as an incoming webhook that initiates the workflow. For example, if you use `@{triggerBody()?['text']}`, it will return a result like `{"text": "Hello from Flux!"}` when that data is received in the webhook's HTTP body.
    >
    > For more details on functions, see [Reference guide to workflow expression functions in Azure Logic Apps and Power Automate](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference).

11. Click the **When a Teams webhook request is received** component and in the left navigation panel, select **Anyone** under **Who can trigger the flow?**.

    ![Screenshot of the Who can trigger the flow setting set to Anyone.](https://docs.soracom.io/_astro/power-automate05.CZsTLO4a_Z1lkJLV.webp)

12. Click 💾 **Save** to save your flow.

13. Click the **When a Teams webhook request is received** trigger to see the **HTTP URL** provided in the flow and click the button to copy the value.

    ![Screenshot emphasizing the HTTP URL field in the When an HTTP request is received trigger.](https://docs.soracom.io/_astro/power-automate11.C06PgWK__yeP2K.webp)

    The **HTTP URL** will be used in the next step when configuring the [Webhook Action](https://docs.soracom.io/en/services/flux/webhook) in Soracom Flux.

    > [!WARNING]
    >
    > **To see the HTTP URL again**:
    >
    > 1. Open the left navigation panel.
    > 2. Select **My flows**, then the Cloud flow you created.
    > 3. Click ✏ **Edit**.
    > 4. Click the **When a Teams webhook request is received** trigger.

## Create a Flux App

A [Flux app](https://docs.soracom.io/en/services/flux) can integrate various forms of inputs and apply sophisticated business logic to achieve desired outcomes.

1. Sign in to the **[User Console](https://console.soracom.io/?coverage_type=g)**. From the **☰ Menu**, expand **Soracom Flux** and select **Flux Apps**.

2. Click **+ Create a new Flux app**.

3. Name the Flux app and provide an optional description, then click **Create**.

   The Flux app will be created and the **Studio** tab in **Soracom Flux Studio** will automatically be displayed.

### Create a Channel

A channel is a Flux [component](https://docs.soracom.io/en/services/flux#components) that connects an event source to actions in your Flux app.

1. Open the **Studio** tab in **Soracom Flux Studio**:

   - If you have just created a Flux app, you will already be in the **Studio** tab.
   - If you are returning, follow Step 1 under [Create a Flux App](https://docs.soracom.io/en/services/flux/ms-teams-messages/#create-a-flux-app) to access your **Flux Apps**, then select your app to open the **Studio** tab.

2. Click **+ Create a channel**.

3. Choose **API/Manual Execution** as the [event source](https://docs.soracom.io/en/services/flux#event-source) and click **Next**.

4. Configure the following:

   - **Name**: Name your channel.
   - **Description**: Optionally provide a summary of the channel.

5. Click **Create a new channel**.

6. After confirming that your configuration is correct, click the ✕ to return to the **Studio** tab.

### Create a Webhook Action

An [action](https://docs.soracom.io/en/services/flux/actions) is a component that can be added to a channel that can conditionally process information from an event source to execute a desired effect.

This step configures a Webhook action within your Flux app. When triggered, the action sends a message to your selected Microsoft Teams channel.

1. Click on the channel created in the [previous section](https://docs.soracom.io/en/services/flux/ms-teams-messages/#create-a-channel) to open the configuration dialog, then select the **Actions** tab and click **+ Add Action**.

2. In the **Create a new action** dialog, choose **Webhook** as your action type, then click **OK**.

3. Configure the following:

   ![Screenshot displaying the action configuration fields (Name, Description, etc.).](https://docs.soracom.io/_astro/webhook-configuration-1.DnyfhcCn_Z1QBqvg.webp)

   - **Name**: Enter an action name.

   - **Description**: Optionally provide a summary.

   - **Enabled**: Set the action to enabled.

   - **Condition**:

     - **Action Condition**: Define when the action should trigger.

       In this case, leave this field blank so that the action executes each time it is called.

4. Configure the following in the **Config** section of the dialog:

   - **HTTP Method**: Select **POST**.

   - **URL**: Enter the URL obtained from [Create a Workflow in Microsoft Power Automate](https://docs.soracom.io/en/services/flux/ms-teams-messages/#create-a-workflow-in-microsoft-power-automate).

   - **Auth Type**: Select **None**.

   - **HTTP Header**: Set the HTTP header to `Content-Type: application/json` for the Webhook HTTP request.

     Selecting **application/json** for **HTTP Body** automatically sets this value to `Content-Type: application/json`.

   - **HTTP Body**: Select **application/json** to set the HTTP body of the Webhook HTTP request and enter the following in JSON:

     ```json
     {"text":"${payload.message}"}
     ```

5. Configure the following in the **Output** section of the dialog:

   - **Republish this action output to another channel**: Set republishing to **Disabled**.

6. Click **Create**.

   ![Screenshot of completed Config and Output sections.](https://docs.soracom.io/_astro/webhook-config.BQgpaFLP_Z1SQa7N.webp)

7. After confirming that your configuration is correct, click the ✕ to return to the **Studio** tab and see the new channel.

## Testing

### Send Messages from the API/Manual Execution Channel

1. From the **☰ Menu**, expand **Soracom Flux** and select **Flux Apps**.

2. Select your Flux app for this project.

3. Click on the channel created in the [Create a Channel](https://docs.soracom.io/en/services/flux/ms-teams-messages/#create-a-channel) section to open the configuration dialog and select the **Test** tab.

4. In the **Body** field, enter JSON syntax containing the message you want to send to the Microsoft Teams channel, then click **Execute**.

   For example, entering `{"message":"Hello from Flux!"}` will send "Hello from Flux!" to a Microsoft Teams channel.

   ![Screenshot of the channel configuration dialog with the Test tab selected](https://docs.soracom.io/_astro/test-tab.D0B4rmlC_Z1N12hW.webp)

### Verify That Notifications Are Being Received in Microsoft Teams

1. Open [Microsoft Teams](https://teams.microsoft.com/v2/).

2. Click **Teams** and select your channel.

   A message should be displayed such as "Hello from Flux!".

### Check the Execution History of the Flux App

1. Sign in to the **[User Console](https://console.soracom.io/?coverage_type=g)**. From the **☰ Menu**, expand **Soracom Flux** and select **Flux Apps**.

2. Click on the Flux app you created.

3. Select the **History** tab.

   For each channel, the Message, Context, Input, and Output of the action executed from that channel are displayed.

> [!WARNING]
>
> To learn about detailed logging, see the [View Logs](https://docs.soracom.io/en/services/flux/logs) documentation.
