Wednesday, August 30, 2017

Using Microsoft Flow to Search Bing News and Post to Microsoft Teams

So continuing the theme of finding ways to use Microsoft Flow with Microsoft Teams for Microsoft Ignite, I've cooked up a flow that uses Azure Cognitive Services to Search Bing News for news related to Microsoft Ignite, Skype for Business or Microsoft Teams and then posts articles that match that criteria to a Microsoft Teams Channel.

One of the problems I had to solve is that by default, Microsoft Flow Bing News trigger for new news, triggers Azure Cognitive Services way too often! The plan I configured for Cognitive Services allowed 1000 hits a month and my original flow burned through that in less than a week. If I had a different plan that allowed overages, I quite possibly would have had quite a big bill. So, I set out to see if I could control how often my flow would look for new news. The secret was in using a somewhat hidden feature of timeout for a trigger.

Anyway... let me show you how to put all this together!

So before we do the flow, we need to create the Azure Cognitive Service for Bing Search.
.
Start by accessing the Azure Portal and click the "+" to create a new service. Then select AI + Cognitive Services and the click "Create" under Bing Search APIs


Give the service a name. I used "BingSearch" and then select the appropriate Subscription that you want to bill the service to. Under the Pricing Tier, I suggest that you select the $1 tier that limits you to 1000 calls per month. The other Tiers allow for overages, which if not planned for correctly could get costly. 

Next, I suggest creating a new Resource Group. As a rule I name my resource groups something similar to the service they are used with, in this case I named it "BingSearch-RG". Make sure the Resource Group is created in the data center you want and then Check the box for the confirmation of the notice. Lastly, Click the "Create" at the bottom.


 After the service is created, the Overview is displayed. From here you will want to click "Show access keys"


Two access keys will be displayed. Copy the first one into the clipboard and put it into notepad or somewhere else where you can reference it later.


So, now we start to build the Flow. Much like my first blog using Microsoft Flow, you'll need to access "My Flows" and then click "Create from blank"


Next click on "Search hundreds of connectors and triggers"


Click on "Schedule"


Then Click "Schedule - Recurrence" under Triggers


Configure the Recurrence trigger for every 4 hours. Then Click "New Step" and choose "Add an action"


Search for "bing" and select "Bing News"


Switch to Triggers and select "Bing Search - On new news article"


If you haven't setup Bing News before, Microsoft Flow will prompt you to setup a connection to Azure Cognitive Services for Bing Search. 

This is where that Access keys come into play. Give the connection a name. In my example below, I used the name "BingNews". Next provide that Access key you saved earlier and click "Create".


Specify the Search Query. In my example below, I use the Logical OR between specific search terms encapsulated in quotes. The result is that any new news article that contains "Microsoft Ignite 2017" or "Skype for Business" or "Microsoft Teams". 

To keep the Bing News search from going nuts making queries to Azure Cognitive Services, click the ellipses menu in the upper right of the Bing News step. Select "Settings"


In the dialog box that appears, configure the Time Duration to be PT10M. This will configure Bing News to timeout after 10 minutes of not finding new news. Combined with the Recurrence of every 4 hours, this means we will have 2 queries to Azure Cognitive Service every 4 hours. 

Click "Done" and then "New Step" and "Add an action"


Search for "teams" and select "Microsoft Teams"


Click on "Microsoft Teams - Post message"


Select the appropriate Team ID and Channel ID. Then specify the message. As you can see below I used a combination of HTML markups and dynamic content in the message. Click "Save Flow" when done


Your flow will run immediately. Since it will run for awhile, feel free to go to "My Flows" and check on it later.


When you click on the flow later you can see the run history. The only thing I don't like with how Microsoft Flow is with the Timeout feature is that when it isn't successful before the Timeout, the flow looks like it failed, when in fact it did exactly like it was configured. So before you freak out and start changing things, let it run for a day or two and see if it is successful.


That's it! Hopefully this inspires you to experiment with some other uses for Microsoft Flow, Azure Cognitive Services and/or Microsoft Teams.




3 comments:

  1. Nice article.
    I will create new flow with Bing News.
    Regards,
    Yoshihiro Kawabata

    ReplyDelete
  2. Hey Jonathan, just curious, why not just use the built in Bing connector in Teams and do the same search?

    ReplyDelete
    Replies
    1. Great question. The Bing News in teams doesn't seem to be as flexible with search topics. Also, this could be adapted to send to somewhere else other than Teams... or multiple places at the same time.

      Delete