Make a fortune bot with Microsoft Power Virtual Agents | IIJ Engineers Blog
Contents
[IIJ 2021 Tech Advent Calendar 12/12 (Sun) article]
hello.R-ITO is in charge of Power Platform in general.My favorite sushi is an egg.Recently, the supermarkets in the neighborhood are delicious because the rolled eggs are delicious.
A little more until 2022.Therefore, I decided to prepare a fortune bot that divules the fortune of the New Year for the company.I will implement it using Microsoft Power Virtual Agents recently.This is an Office365 product that can easily make chatbots based on GUI.
It would be nice if you had a lot of fortune results because it was a fortune bot.So I want to be able to edit the result of the fortune.This is an app that creates a list of Microsoft Lists so that you can freely edit the results of fortune.
Combine these, get the answer, and implement a bot that randomly reply.Power Virtual Agents, it's actually difficult to reply randomly, so I'm glad if it gets some hints.
The simple whole picture is as follows.
The result of the fortune is edited and saved with Microsoft Lists.We will publish these to the user and have them messed up.The ID is automatically earned in Lists in order of registered.If you use this and specify your ID randomly, it will be a fortune.
Power Automate is in charge of the algorithm part.Acquire the number of registered Lists with the Length function, generate numbers between 1 to the number of registrations in the random function, access the list as IDs, and get the fortune results.
After that, if you make a mechanism to reply to the user in Virutalagents, it will be completed.
Prepare a list of fortune results
Introducing how to make it.
First, add Microsoft Lists on Teams to create a fortune list.(Since the contents of Microsoft Lists are actually just SharePoint lists, we use the SharePoint connector when processing in Power Automate later).
It is useful for debugging by displaying IDs with options that are displayed/hidden by columns.It will be easier to see if the random value is properly in the ID range and what number it is.
As a result, prepare a fortune in the title, and a thankful word in the next column.Return the fortune bot in the set of these two.
I hope that the list of fortune results will be released and it will be able to be freely edited in the company, so that Virtual Agents can be easily touched.It would be nice if BOT returned the result of the fortune you made without writing the code.
Edit trigger in Virtual Agents
Now, after preparing Lists, we will move on to create Virtual Agents.What will Virtual Agents respond if he talks from the user?It consists of (trigger) and what kind of reaction (canvas) returns.
If the user talks, such as Omikuji, Omikuji, luck, etc. as a trigger, we will implement this answer pattern.You may set multiple triggers, so if you set more keywords, you will be friendly.
Let's first prepare a trigger to get the fortune.Predict the user input so that you can withstand some fuzzy input.
Once you have created a keyword, you will enter the response content and the canvas edit.
Get random fortune results with Power Automate
The part that randomly discharge the fortune is left to the Power Automate action.To create it, call her action, so choose the creation of a flow.Then, select the Power Virtual Agents Floten Plate.
Create the following flows from the template.Here we will explain in order.
First, I will explain the random part of the fortune.Acquire multiple items in the SharePoint connector to get the result of the registered fortune using Lists.(Japanese translation is a terrible trap that is not intuitive at all)
Then store the results and prepare a variable to calculate the random value.Select Initializing the connector variables and write the formula.
The actual formula looks like this.Rand (1,10) can generate numbers from 1 to 10.You can specify a random ID if you set the 10 part here to the total number of Lists registration.So you can get the total number of registrations with LENGTH (Value in the List), and replace the RAND part to complete.
It is complicated to look at the completed formula, but when written in Japanese, it has the following structure.Random number acquisition (minimum value = 1, maximum value = List of fortune results)
The random number is now created.After that, you will get the result of the fortune based on the result of the random number.
Includes the SharePoint connector item to the random number variable calculated earlier in the ID.Enter the obtained results in text format as return values to Virtual Agents.
This completes the Power Automate part.Save and return to Virtual Agents.
Final response setting
After returning to Virtual Agents, you can reply to the results of Power Automate.Also, if you put the name of the person who drawn a fortune, the naturalness will increase, so BOT.UserdisplayName is used in the standard variables to reply to the user's display name.
This is the completion of the fortune bot!Thank you for your hard work.By saving, testing, and publishing the bot, you can expand into the company.
I hope you will have a lot of fortune and be a good year next year.
Applied edition
As you may have noticed so far, if your ID is lost, this method will break down.This is because there is a possibility of acquiring an ID that does not exist.
As a countermeasure, it can be solved by accessing the result as an array.However, it is a simple face, and there is nothing difficult to do with a good theory.
This is enough for beginners to try it out, and I wonder if it is easy to debug.The formula of Power Automate tends to be scared, so I think it's important to have it touched as a story.
It is an implementation that saves all the results as an array and randomly, but you can access the array by adding a variable to the variable.Use this.
It is OK if you insert the Rand (0, the length of the array), so the formula is as follows.Variables ('fortune list corrections')?
Please improve and make a better bot.
IIJ Engineers blog読者プレゼントキャンペーン