Using TBP API to send a message via a bot using Power Automate
Knowledge Level: Advanced
In this tutorial, we will demonstrate how you can use the TBP API to send a simple text message to a bot user via Power Automate.
Prerequisites:
- A bot
- A power automate account (Paid)
- Bearer token for authentication. Click here for instructions to set it up
- Your favorite beverage
Step 1: Extract Bearer token
If you have followed the guide to setup a bearer token for authentication (linked in the Prerequisites), you will notice that the bearer token appears in the output body of the 'HTTP' flow. We will need to separate the bearer token from the rest of the output.
We need to add a couple of modules to be able to separate the bearer token.
First, let's add an 'Initialize variable module' where we will store the bearer token when the flow is executed.
In the next step, add another module and look for 'Compose'. In this flow, we will use the output from the HTTP output body to extract the values under the variable 'access_token'.
In the 'Input' section of this module, enter the following code:
@{outputs('HTTP')?['BODY']?['access_token']}
After this, we will add one more module to store the extracted token value in the 'bearer token' variable we created above. To do this, look for the 'Set Variable' module and use the output from the 'Compose' module.
Step 2: Send message to the bot user using HTTP module
In the last step, we will now add an 'HTTP' module to post our message to the bot user.
In the HTTP module settings, set the following:
Method: Post
URI: https://api.thebotplatform.com/v1.0/activity/external
Headers-Accept: application/json
Headers-Authorization: Bearer (bearer token value)
In the 'body' part, use the following code:
{
"data": {
"attributes": {
"messages": [
{
"text": "Hello"
},
{
"text": "World"
}
],
"recipient": "[email protected]"
},
"type": "external-activity"
}
}
Note
In the recipient section of the code, use the email address of the recipient who is supposed to receive this message.
The final configuration should look like this:
Once the above is completed, make sure to save your flow and turn it on.
If everything works as expected, you should receive a message from the bot once you run the trigger.
Updated over 2 years ago