DEV Community

El Bruno for Microsoft Azure

Posted on • Originally published at elbruno.com on

#PowerAutomate – Flow to use Open Trivia Database questions in a Microsoft Teams bot 🤖 2/N

PowerAutomate – Flow to use Open Trivia Database questions in a Microsoft Teams bot 🤖 Series

Hi !

Let´s analyze the response from the API query for a single question [https://www.otriviata.com/api.php?amount=1].


{
    "results": [
        {
            "category": "Entertainment: Television",
            "type": "multiple",
            "difficulty": "medium",
            "question": "What episode of "Mr. Bean" saw him trying to prevent people from seeing him naked?",
            "id": "1124",
            "correct_answer": "Mr. Bean in Room 426",
            "incorrect_answers": [
                "Mr. Bean Goes to Town",
                "The Trouble with Mr. Bean",
                "Back to School Mr. Bean"
            ]
        }
    ]
}

Enter fullscreen mode Exit fullscreen mode

We have a collection of results (questions) with only one result.

Power Automate Flow Steps

In order to get this, let’s use a HTTP step. Our next is to use a [Parse JSON] step to work with the data from the response.

steps with the http request and a parse json with the response from the http request.

The [Parse JSON] step uses a schema to process the json data, and it’s cool enough to generate the schema from a sample json 😀.

_ Disclaimer 1: I’m using +10 steps to parse the JSON as a string. We can do this in less steps, however, I think it’s useful to explain this in simple steps._

_ Disclaimer 2: I even use an old VB6 notation to add the variable type in the variable name._

Let’s create a couple of variables:

  • strQuestions: we will store here the “results[]” element from the JSON Response.
  • intQuestionLength: length of the strQuestions variable. Using the following formula

sub(length(variables('strQuestion')), 2)

Enter fullscreen mode Exit fullscreen mode

steps to create variables for questions and question length

Now comes a tricky one. I can use an array type of variable to get the 1st element of results (1st question), however, I decided to parse some strings. I’ll use a substring to remove the 1st and last char, so I have a new JSON that represent a single question.


    "results": [
        /// I want this ! 
        {
        }
    ]

Enter fullscreen mode Exit fullscreen mode

Once again, let’s use a [Parse JSON] step with the following formula:


substring(variables('strQuestion'), 
1, 
variables('intQuestionLength'))

Enter fullscreen mode Exit fullscreen mode

parse json step to get a json with a single question

Cool ! We can access directly to the elements of the question:

elements of the question.

In order to use this later on the Teams question, we need to have a single string (with JSON format) with all the question’s answers. We have to contact the correct answer and the incorrect answer.

Let’s create 3 variables:

  • strResponse: Uses the following formula to have the correct answer as valid JSON string.

concat('"', 
body('Parse_JSON_from_strQuestion')?['correct_answer'], 
'"')

Enter fullscreen mode Exit fullscreen mode
  • strIncorrectResponses: Using the following formula removes the 1st and last char from the incorrect answers string.

substring(
string(body('Parse_JSON_from_strQuestion')?['incorrect_answers']), 
1,

sub(length(string(body('Parse_JSON_from_strQuestion')?['incorrect_answers'])), 2))

Enter fullscreen mode Exit fullscreen mode
  • strAllResponses: concatenate the correct and incorrect answers in a single string. Using a valid JSON format.

concat(variables('strCorrectResponse'), 
',', 
variables('strIncorrectResponses'))

Enter fullscreen mode Exit fullscreen mode

stes for the previous 3 variables

And finally, let’s create an array variable to store all the responses, using the split() function.


split(variables('strAllResponses'), ',')

Enter fullscreen mode Exit fullscreen mode

We can use this array to add a new step to post a question into teams from the flow Bot.

create and step and post a question into teams from the flow Bot step

Test the Flow

Before moving on, let’s test the Flow. Once the test create an array, we can the following values for the question’ answers:

And the Flow Bot in teams with ask the question with this format

Super cool ! In my next post I’ll share how to process the response to validate if the question is correct or incorrect !

And important: Join me in this 2-week FREE program @msftreactor

and we will learn together about Microsoft Power Pages.

We will also cover fundamentals of Power Pages architecture, components, features, and tooling.

https://aka.ms/DecPowerPagesBootcamp

Starting on Dec 05

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.


Top comments (0)