#PowerAutomate – Flow to use Open Trivia Database questions in a Microsoft Teams bot 🤖 2/N
Tags: englishpost, microsoftteams, opentriviadatabase
Published: 2022-11-24 16:26:54

---
title: #PowerAutomate – Flow to use Open Trivia Database questions in a Microsoft Teams bot 🤖 2/N
published: true
date: 2022-11-24 15:02:38 UTC
tags: EnglishPost,EnglishPost,MicrosoftTeams,OpenTriviaDatabase
canonical_url: 
---

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

- [Intro to Open Trivia Database API](https://dev.to/azure/powerautomate-flow-to-use-open-trivia-database-questions-in-a-microsoft-teams-bot-1n-1h1e)
- [How to process the API Response and send a question to Teams](https://elbruno.com/2022/11/24/%c2%b7powerautomate-flow-to-use-open-trivia-database-questions-in-a-microsoft-teams-bot-%f0%9f%a4%96-2-n/)

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"
            ]
        }
    ]
}

```

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.](https://brunocapuano.files.wordpress.com/2022/11/image-4.png?w=632)

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)

```

![steps to create variables for questions and question length](https://brunocapuano.files.wordpress.com/2022/11/image-5.png?w=627)

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 ! 
        {
        }
    ]

```

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

```

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

```

![parse json step to get a json with a single question](https://brunocapuano.files.wordpress.com/2022/11/image-6.png?w=647)

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

![elements of the question.](https://brunocapuano.files.wordpress.com/2022/11/image-7.png?w=414)

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'], 
'"')

```

- **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))

```

- **strAllResponses:** concatenate the correct and incorrect answers in a single string. Using a valid JSON format.

```

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

```

![stes for the previous 3 variables](https://brunocapuano.files.wordpress.com/2022/11/image-8.png?w=667)

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

```

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

```

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](https://brunocapuano.files.wordpress.com/2022/11/image-9.png?w=689)

## 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:

![](https://brunocapuano.files.wordpress.com/2022/11/image-10.png?w=634)

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

![](https://brunocapuano.files.wordpress.com/2022/11/image-11.png?w=1024)

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](https://aka.ms/DecPowerPagesBootcamp)

Starting on Dec 05

Happy coding!

Greetings

El Bruno

More posts in my blog [ElBruno.com](https://www.elbruno.com).

* * *