Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and...

42
Build Smart Bots with Bot Framework and Microsoft Cognitive Services Hands-on lab Natural Campaign Greg Degruy Adebisi Oje

Transcript of Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and...

Page 1: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

BuildSmartBotswithBotFrameworkandMicrosoftCognitiveServicesHands-onlabNaturalCampaignGregDegruy

AdebisiOje

Page 2: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page2

CONTENTS

OVERVIEW...........................................................................................................................................3

Objectives.......................................................................................................................................4

SystemRequirementsandSetup....................................................................................................5

EXERCISE1:CREATEABOT...................................................................................................................6

Task1–RegisterBot.......................................................................................................................6

Task2–SetupStarterBotProject.................................................................................................6

Task3–TesttheBotintheBotEmulator.......................................................................................7

EXERCISE2:ADDALUISMODEL...........................................................................................................9

Task1–CreateanApplication........................................................................................................9

Task2–SetupModel...................................................................................................................10

Task3–TrainModel.....................................................................................................................11

Task4–PublishModel.................................................................................................................13

EXERCISE3:CODETHEBOT................................................................................................................14

Task1–ConnectLUISModeltoBotCode....................................................................................14

Task2–AddNewsSearchcapabilities.........................................................................................15

Task3–AddSentimentAnalysiscapabilities................................................................................17

Task4–Testbote2einBotEmulator..........................................................................................17

EXERCISE4:CONNECTBOTTOSKYPE.................................................................................................21

Task1–CreateAzureWebApp....................................................................................................21

Task2–DeployBottoAzure........................................................................................................21

Task3–ConnectBottoSkype......................................................................................................22

Page 3: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page3

Overview

MicrosoftBotFrameworkandMicrosoftCognitiveServicesprovideacomprehensiveofferingtobuildanddeployhighqualityintelligentbotsyouruserscanenjoyintheirfavoritemessagingexperience.

MicrosoftBotFramework:Developerswritingbotsallfacethesameproblems:botsrequirebasicI/O;theymusthavelanguageanddialogskills;theymustbeperformant,responsiveandscalable;andtheymustconnecttousers–ideallyinanyconversationexperienceandlanguagetheuserchooses.BotFrameworkprovidesjustwhatyouneedtobuild,connect,manageandpublishintelligentbotsthatinteractnaturallywhereveryourusersaretalking–fromtext/smstoSkype,Slack,FacebookMessenger,Kik,Office365mailandotherpopularservices.

MicrosoftCognitiveServices:AgrowingcollectionofpowerfulintelligenceAPIs,whichallowyoutoquicklyaddstateoftheartartificialintelligencetoyourexperienceswithafewlinesofcode.TheyareaproductofyearsofR&Dbyexpertsinthefieldsofcomputervision,speech,naturallanguageprocessing,knowledgeretrievalandwebsearch.AllAPIsareRESTfulandcanbeeasilyintegratedintoanyapporserviceintheprogramminglanguageandplatformofyourchoice.TheAPIsareconstantlyimproving,learningandgettingsmarter,soyourexperienceisalwaysusingthelatestinnovationscomingfromtheMicrosoftengineeringteams.SimplydroptheAPIcallintoyourbot’scodeandyouareset.

Objectives

Yourgoalinthishands-on-labistobuilda‘sentimental’newsbotusingBotFrameworkandseveralintelligentAPIsavailableinMicrosoftCognitiveServices.Whenyoucompletetheassignment,yourbotwillbeableto:

Page 4: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page4

• UnderstandandprocessuserqueriesinnaturallanguageusingMicrosoftLUIS(LanguageUnderstandingIntelligentService).

• SearchfornewsarticlesaroundtheworldusingtheBingNewsSearchAPI• Extractthesentimentofnewsarticles(negative/positive)usingtheTextAnalyticsAPI.• Returnrichresultstousers,includingCarouselsandSkypeEmoticons.

Thesentimentalnewsbotcanfindrelevantnewsarticlesaroundtheworld,andgiveyouahintabouteacharticle’ssentimentbyanalyzingitsdescription.

Attheendofthislab,youwillbecomfortabledevelopingyourownAI-poweredbotsusingBotFrameworkandMicrosoftCognitiveServices!Wecan’twaittoseewhatyou’llbuildnext!

-

Systemrequirements

Youneedthefollowingtocompletethislab:

• MicrosoftVisualStudio2015or2017

• MicrosoftAzureSubscription

Page 5: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page5

• MicrosoftAccount(e.goutlook.com)

• VisualStudioToolsforAzure(optional,butrecommendedforthelastexercise)

• Skype(latestversion)

Setup

Performthefollowingstepstoprepareyourcomputerforthislab:

1. InstallMicrosoftVisualStudio2015or2017(updateallVSextensionstotheirlatestversions).

2. InstallVisualStudio2015ToolsforAzure*TheAzureWorkloadinVisualStudio2017includesallthetoolsandfeaturesyouneedtobringthepowerofAzuretoyourapplication

3. DownloadthestarterVSproject“StarterProject_SentimentalNewsBot.zip”fromhttps://aka.ms/MvpSummit16Bot

4. InstalltheBotFrameworkEmulator

5. SignuptoMicrosoftCognitiveServicesusingyourpersonalMicrosoftaccount

Exercises

ThisHands-onlabincludesthefollowingexercises:

1. SettingupasimpleBotwiththeBotBuilderSDK(5minutes)

2. AddingaLUISmodeltogiveyourbotnaturallanguageprocessingcapabilities(15minutes)

3. IntegratingCognitiveServicesAPIstoaddsmartstoyourbot(30minutes)

4. DeployingthebottoAzureandconnectingittoSkype(10minutes)

Estimatedtimetocompletethislab:60minutes.

Exercise1:CreateaBotEstimatedtimetocomplete:5minutesTask1–RegisteryourBot

Page 6: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page6

Registeranewbotinthebotframeworkwebsite.

1. Gotohttps://dev.botframework.com/,signinwithyourpersonalMicrosoftaccountandthenclick“Registerabot”atthetopofthescreen.

2. GiveyourbotaName,aBothandle,andprovideashortDescription.

3. IntheConfigurationsection:

• AddaplaceholderURLwithanhttpsprotocol,like“https://www.contoso.com”,forthemessagingendpoint(you’lleditthiswhenyoudeployyourbottoAzure).

• Click“CreateMicrosoftAppIDandpassword”.ThenextpagewilldisplayyourMicrosoftAppID(writethisdown).Alsostayonthispage.

• Click“Generateapasswordtocontinue”.WritedownthisMicrosoftapppasswordandkeepitsafe!Thisistheonlytimethepasswordbedisplayed!Click“ok”andthenclick“FinishandgobacktoBotFramework”

4. Fillouttherestoftherequiredfields(Adminsectionisoptional),alsoagreetothePrivacystatement,Termsofuse,andCodeofconduct.Thenclick“Register”.

Task2–SetuptheStarterBotProject

1. UnzipthestarterVSproject“StarterProject_SentimentalNewsBot.zip”youdownloadedfromhttps://aka.ms/mvpsummit16bottoafolderofyourchoiceandopenthe.slnsolutionfileinVisualStudio.

a. HitOKandtrustthispromptifyougetit.

2. SetyourSolutionConfigurationtoDebugandyourSolutionPlatformtoAnyCPU.SelectMicrosoftEdge(oryourfavoritebrowser)fromtheDebugTargetdropdownmenu.

Page 7: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page7

3. ClickthegreenrunbuttontoBuildandrunyourapp.Youwillseeablankappbrowsertabdisplayingtheapplicationsdefault.htm.KeepnoteoftheportyourBotisrunningon(inourcaseport3979)aswellastheAPIURLtobeusedfortestingyourBot.

4. Then,returntoVisualStudioandclicktheredstopbuttontoexitdebugging.

Task3–TesttheBotintheBotEmulator

TheBotFrameworkprovidesanemulatorthatletsyoutestyourbotinyourlocalmachine.

1. OpentheBotFrameworkEmulator.YouinstalledthisinourSetupstep.

2. ConfiguretheEmulatortointeractwithyoubyopeningVisualStudiobackintoview.

a. InVisualStudioaddyourMicrosoftAppIdtothevaluefieldintheWeb.Configfile.

b. AddyourMicrosoftAppPasswordtothevaluefieldintheWeb.Configfilevaluefield.ThesevaluesareplacedinfieldsI’veblockedwithblueboxes.

Page 8: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page8

c. Bringtheemulatorbackintoview.AddyourMicrosoftAppIdandMicrosoftAppPasswordintheEmulatorfieldsseeninthescreenshotbelow

i. EnsuretheURLintheEmulatormatchestheURLdisplayedinyourwebbrowserwhenyourunyourfromVisualStudioandadd“/api/messages”ifnottherealreadytotheendoftheURL.Forinstance,http://localhost:3979/api/messagesasseeninthescreenshotbelow

3. RuntheprojectandtestyourBotbytypingamessageintheEmulator’stextbox.Thebotisverysimpleatthemoment.Itwillsendabriefdescription(twice!)whenyouaddittoaconversation(tip:youcanclickonNewConversationhiddenbehindthe3dotsontheEmulatortoolbartoemulatethestartofanewconversation).Thenitwillsimplyrepeatanymessageyousendtoit.

Page 9: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page9

4. That’sallforthisexercise.Inthenextexercisewillfigureouthowtomakeourbotsmarter.

Page 10: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page10

Exercise2:AddaLUISmodelEstimatedtimetocomplete:15minutes

LUIS(luis.ai)isMicrosoft’sNaturalLanguageProcessingtoolkit.Itprovidesafastandeffectivewayofaddinglanguageunderstandingtoapplications–anessentialskillforbots.InthisexerciseyouwillbuildaLUISmodelandintegrateitintoyourbot’scode.

CreatingaLUISmodelcanbesummarizedinthreesteps;createaLUISapplication,setupyourLUISmodelandtrainit,andfinallypublishthemodeltothecloud.

Task1–CreateanApplication

1. Gotowww.luis.aiandloginwithyourMicrosoftaccount.

2. Clickonthe“Myapps”tabandselect“NewApp”.

3. Fillouttherequiredinformationintheformfromthepop-up.Using“SentimentalNewsBot”forthename

andchoose“English”astheAppCultureandclick“Create”.

Page 11: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page11

Task2–CreateLUISmodel

Forthesentimentalnewsbot,youwillbuildanaturallanguageprocessingmodelthatrespondstotwouserintents(“News”and“Help”).Furthermore,foruserquerieswith“findnews”intent,themodelwillbeabletoidentifywhatisthe“newstopic”theuserisinterestedin,aswellaswhetherheislookingfornewswith“positive”or“negative”sentiment.LUISreferstoinformationextractedfromnaturallanguagequeriesasentities.

AddIntents

1. YoushouldnowbeonyourAppDashboard.AnalertdialogwillnotifyyouthatnoIntentsexisitsinyourapp,soselect“CreatanIntent”togetstarted.

2. Use“News”asthefirstIntentnameandclickSave.ThenenteranexampleUtterancethatwouldtrigger

thisintent,like“findnewsaboutgaming”,andpresstheEnterkey.BesuretohittheSavebuttonbeforemovingforward.(seeexamplescreenshotsbelow)

Page 12: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page12

Page 13: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page13

3. Repeatthesameprocesstoaddthesecondintent.ClicktheIntentslabelfromtheleftsidebarandthen

clicktheAddIntentbutton.Use“Help”astheIntentnameandclickSave.Thenenteranexample

Utterancethatwouldtriggerthisintent,like“needhelp”andpresstheEnterkey.BesuretohittheSave

buttonbeforemovingforward.

4. ClicktheIntentslabelfromtheleftsidebarandyoushouldnowseeyournewIntents!

AddEntities

Next,youwilladdtheentities,i.e.theinformationthemodelwillextractfromthenaturallanguagequeries.Thiswillbecomemoreclearinthenextstepwhenyoustarttrainingyourmodel.

1. Fromtheleftsidebar,clickontheEntitieslabel.Thesidebarisunderthelabelforshortenedversionofyourappname,inmyexamplescreenshottheshortenedappnameis“Sentimental...”.

2. ClickAddcustomentityontheEntitiespage.Use“NewsTopic”astheentitynameandkeeptheSimple

Entitytypeselected.ThenclickSave.

Page 14: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page14

3. RepeatthesameprocessandaddtwomoreEntitiesnamed“PositiveSentiment”and“NegativeSentiment”

Page 15: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page15

Afteryoucompletetheprevioussteps,EntitieslistontheEntitiespageshouldlooklikethis:

Task3-TrainModel

Tostarttrainingthemodelyoushouldprovidesampleutterancesandlabelthem.

1. SelecttheIntentslabelfromtheleftsidebar,thenletsselecttheNewsIntenttostarttraining.You

shouldseethe“findnewsaboutgaming”Utterancetextaddedfromthebeginningofthisexerciseas

seeninthescreenshotsbelow.

Page 16: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page16

3. Theutterance“findnewsaboutgaming”willbepresentedforlabelling.ClickonthewordsintheutterancetohighlightthemandlabeltheentitiesforNewsTopic,PositiveSentiment,NegativeSentiment,asshownintheexamplesbelow.ClickonSaveafteryoucompletelylabeleachutterance.

a. TotraintheNewsTopicUtterancei. Selecttheword“gaming”

4. i. ChoosetheNewsTopicentity.Thisspecifiesgamingasanewstopictolookforinfuture

utterances.

Page 17: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page17

5. i. You’llnowseegaminghasbeenlinkedtotheNewsTopicEntity,representedbythe

[$NewsTopic]thatnowappearsinourexampleutterance.ii. ClickSavetocompletetheassociation.

b. NegativeSentimentUtterance

i. Add“negativenewsaboutgaming”inthe“Typeanewutterance&pressEnter”boxandpressenter.Herewe’lladd2Entitiestoourtrainingmodel.

6. i. You’llseeournewutterancehasbeensuccessfullyadded,nowhitSave.

Page 18: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page18

ii. iii. Selecttheword“gaming”

iv. v. Selecttheword“negative”.

Page 19: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page19

vi. vii. ChoosetheNegativeSentimententity.Thisspecifiesnegativeasanegativesentimentto

lookforinfutureutterances.

viii. ix. You’llnowsee“negative”hasbeenlinkedtotheNegativeSentimentEntity,represented

bythe[$NegativeSentiment]thatnowappearsinourexampleutterance.x. ClickSavetocompletetheassociation.

b. PositiveSentimentUtterance

i. Add“goodnewsaboutgaming”asanewutterancetotrain.ii. Followthesamestepsfrompartb.wherewedefinedourNegativeSentimentUtterance,

exceptbesuretousePositiveSentimentwhenselectgood.iii. BesuretoclickSavetocompletetheassociation.

7. Continueaddingasmanyutterancesasyoucan(note:youneedtoaddatleast5examplesofeachintent

beforeyoucantrainyourmodelforthefirsttime).Themoreyouadd,thebettertheprecision/recallof

Page 20: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page20

yourmodelwillbe.Notethatyoucanre-trainyourmodelatanytimesothatitgetsbetterovertime–youcanevenreviewmessagessubmittedtothebotbyusersandre-labelthemiftheyweremis-classified!LuisreferstothisfeatureasActiveLearning.

8. SelectIntentsagainfromtheleftsidemenu.Followthesameprocesstosubmitutterancesforthe“Help”Intentaswell,addinganutterancelike“Ineedhelp”.Youdon’tneedtoidentifyanyentitiesforquerieswithHelpintent,soonlymakesurethattheHelpintentisselectedfromthedrop-downmenubeforeyousubmittheutterancefortraining.

9. Finally,clickontheTrain&Testlabelfromtheleftsidemenu.10. HittheTrainApplicationbuttonontheTestyourapplicationpage.Youwillgetaconfirmationmessage

andnoteonthelastdateandtimetheappwastrainedwhenthetrainingiscompleted(usuallytakesafewsecondsforsimplemodels).

Page 21: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page21

Page 22: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page22

Task4-PublishModel

Finally,youwillpublishyourmodeltothecloud.Inotherwords,yourmodelwillbeaccessiblebyyourbotapplicationviaaRESTendpoint.Click“PublishApp”locatedontheleftsidemenu.YouwellneedtoselectanEndpointKey.Yourgiven”BoostrapKey”shouldsufficeforthis.SelectingMyKeysatthetopmenu,you’llseethisBootstrapKeyisreallyyourProgrammaticAPIKey.

Oncepublishedyou’llseeyourLuisidandLuisSubscriptionkey(colorcodedbelow)embeddedintheURLendpoint.

Endpointurl:

https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/f32590c3-5c01-4776-b22e-abdd01537ec8?subscription-key=5d7817feda724399aaf69441f3fb18eb&timezoneOffset=0.0&verbose=true&q=

Youwillusethemlaterinyourbotcode,sotakenoteofthem.Fromthesamepop-up,youcansubmitsometestqueriestotesttheRESTendpoint.

Page 23: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page23

Exercise3:CodetheNewsBotEstimatedtimetocomplete:30minutes

SwitchtoyourbotprojectinVisualStudiotostartcodingyourbot.

Task1–ConnectLUISmodeltoBot

1. GototheControllersfolderandMessagesController.csfileandreplacethecodeinthePosttaskwiththefollowingsnippet.ThecodesimplyroutesallincomingusermessagestotheRootDialogwhereyouwilldomostofthecoding.

public async Task<HttpResponseMessage> Post([FromBody]Activity activity) { if (activity.Type == ActivityTypes.Message) {

await Conversation.SendAsync(activity, () => new RootDialog()); }

else { await this.HandleSystemMessage(activity); } var response = Request.CreateResponse(HttpStatusCode.OK);

Page 24: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page24

return response; }

2. GototheDialogsfolderandRootDialog.csfile.AddtheLuisidandLuissubscriptionkeyofyourmodelthatyougotinExercise2totheLuisModelparameterontopoftheRootDialogclassasshownbelow.

Tip:Inthecodefile,youcanalsofindtheLuisIdandsubscriptionkeyfortheLuismodelweusedforourbot(commentedoutbydefault).Youcanusetheseinsteadifyouliketotestyourbotwithourpre-builtmodel.

[Serializable] [LuisModel("YourLuisId", "YourLuisSubcriptionKey")] public class RootDialog : LuisDialog<object>

3. TheRootDialogisnowhookeduptoyourLUISmodel.Whenevertheusersendsamessagetothebot,yourbotservicewillrouteittotheRootDialog.ThedialogwillinturncallyourLUISmodeltoidentifythemessage’sintentandentitiesandfinallycallthecorrespondingfunction/tasktohandlethebot’sresponse.Forexample,iftheusermessagehasa“Help”intent,theHelptaskwillbecalled(notehowthefunctionthatiscallediscontrolledbytheLuisIntentparameterasshownbelow

[LuisIntent("Help")] public async Task Help(IDialogContext context, LuisResult result) { string response = $"I can help you find the freshest news articles…” // more string message data

await context.PostAsync(response); context.Wait(this.MessageReceived); }

Task2–CodetheNewsSearchTask

You’llnowaddthecodethatwillruntheBingnewssearchtaskwhenevertheusersendsarequestwithaNewsintent.ThenewsresultswillbepresentedtotheuserinaNewsCarousel.

1. IntheRootDialog.csfile,createanewtasknamedgetBingNewsresponsibleforfetchingthenewsfromBingforagivenquery.ReplacethiswiththeBingSearchAPIkey,whichcanbefoundinyourMicrosoftCognitiveServicessubscriptionpage.

Page 25: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page25

private async Task<BingNews> getBingNews(string query) { BingNews bingNews; String bingUri = "https://api.cognitive.microsoft.com/bing/v5.0/news/search/?count=50&q=" + query; string rawResponse; HttpClient httpClient = new HttpClient() { DefaultRequestHeaders = { {"Ocp-Apim-Subscription-Key", "YourApiKey"}, {"Accept", "application/json"} }}; try { rawResponse = await httpClient.GetStringAsync(bingUri); bingNews = JsonConvert.DeserializeObject<BingNews>(rawResponse); } catch (Exception e) { return null; } return bingNews; }

Note:ThestarterprojectalreadyincludesthemodelfortheBingNewsAPIresponse.Ifyouarecurious,youcanfindthemodelintheBingNews.csfileinyourprojectfolder.

2. Now,headtotheSearchtask,andreplacethecurrentcodetherewiththebelow.ThecodefirstensuresthetaskiscalledforanyquerythathasaNewsintent.ItthenparsestheNewsTopicentity(ifavailable)andperformsaBingNewssearchtoretrievethenewsresults.

[LuisIntent("News")] public async Task Search(IDialogContext context, IAwaitable<IMessageActivity>

activity, LuisResult result) { var message = await activity; var reply = context.MakeMessage(); EntityRecommendation newsEntity, sentimentEntity; if (result.TryFindEntity("NewsTopic", out newsEntity)) { var findPositive = result.TryFindEntity("PositiveSentiment", out

sentimentEntity); var findNegative = result.TryFindEntity("NegativeSentiment", out

sentimentEntity);

await context.PostAsync((findPositive ? "positive " : (findNegative ? "negative " : "")) +

"news about '" + newsEntity.Entity + "' coming right up \U0001F680!");

Page 26: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page26

BingNews bingNews = await getBingNews(newsEntity.Entity); if (bingNews == null || bingNews.totalEstimatedMatches == 0) { reply.Text = "Sorry, couldn't find any news about '" +

newsEntity.Entity + "' \U0001F61E."; } else { reply.AttachmentLayout = AttachmentLayoutTypes.Carousel; reply.Attachments = new List<Attachment>(); }

...

3. Continuefromwhereyouleftoff,andaddthefollowingcodetocompletethesearchtask.ThecoderendersthenewsresultsinaCarousel.ItalsoreturnsastaticmessagetotheuserwheneverLUIShasnotmanagedtosuccessfullyparsetheNewsTopicentity.Notealsothatmostmessagingchannels,includingSkype,limitthenumberofcardsbotscanshowinacarouselto10,whichiswhyweusedthislimithereaswell.

for (int i = 0; i < 10 && i < (bingNews?.totalEstimatedMatches ?? 0); i++)

{ var article = bingNews.value[i]; HeroCard attachment = new HeroCard() { Title = article.name.Length > 60 ?

article.name.Substring(0,57) + "..." : article.name, Text = article.provider[0].name + ", " +

article.datePublished.ToString("d") + " - " + article.description,

Images = new List<CardImage>() { new CardImage(article.image?.thumbnail?.contentUrl + "&w=400&h=400") },

Buttons = new List<CardAction>() { new CardAction( ActionTypes.OpenUrl, title: "View on Web", value: article.url)}}; reply.Attachments.Add(attachment.ToAttachment()); } } else { reply.Text = $"I understand you want to search for news, but I couldn't

understand the topic you're looking for \U0001F633. "; reply.Text += $"Rephrase your question or re-train your LUIS model!"; } await context.PostAsync(reply); context.Wait(this.MessageReceived); }

Page 27: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page27

4. Buildtheprojectandrunit.Headtotheemulatorandtestyourbotbysendingaquery,withanewsintent,like“findnewsaboutgaming”.Ifallgoeswell,youwillseeresultssimilartothescreenshotbelow.

Task3–AddSentimentAnalysisinNewsSearch

Forthelastcodingtask,youwilladdsentimentanalysiscapabilitiestothenewsbot.

1. CreateanewtaskintheRootDialogclasscalledgetSentimentScore.Addthefollowingcode.Thetaskreceivesastring(inourcase,thearticle’sheadlinedescription),andsendsittotheTextAnalyticsAPItoprocessthesentimentofthestring.TheTextAnalyticsAPIreturnsasentimentscorethatspansfrom0(verynegative)to1(verypositive).RemembertoreplacethiswithyourpersonalTextAnalyticskey,whichcanbefoundinyourMicrosoftCognitiveServicessubscriptionpage.SearchforTextAnalytics-PreviewandclicktheSubscriptbutton.

Page 28: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page28

private async Task<double> getSentimentScore(string documentText) { string queryUri =

"https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment"; HttpClient client = new HttpClient() {

DefaultRequestHeaders = {{"Ocp-Apim-Subscription-Key", "YourAPIKey"}, {"Accept", "application/json"}}}; var textInput = new BatchInput { documents = new List<DocumentInput> { new DocumentInput { id = 1, text = documentText, }}}; var jsonInput = JsonConvert.SerializeObject(textInput); HttpResponseMessage postMessage; BatchResult response; try { postMessage = await client.PostAsync(queryUri, new

StringContent(jsonInput, Encoding.UTF8, "application/json")); response = JsonConvert.DeserializeObject<BatchResult>(await

postMessage.Content.ReadAsStringAsync()); } catch (Exception e) { return 0.0; } return response?.documents[0]?.score ?? 0.0; }

2. Next,createanotherfunctionintheRootDialogclassnamedgetSentimentLabel.Thefunctionsimplyreturnsasentimentlabelbasedonthesentimentscoreofthearticle.

private string getSentimentLabel(double sentimentScore) { string message; if (sentimentScore <= 0.1) message = $"Extremely Negative :@";

Page 29: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page29

else if (sentimentScore <= 0.2) message = $"Very Negative (facepalm)"; else if (sentimentScore < 0.4) message = $"Negative :("; else if (sentimentScore <= 0.6) message = $"Neutral :^)"; else if (sentimentScore <= 0.8) message = $"Positive :P"; else if (sentimentScore < 0.9) message = $"Very Positive :D"; else message = $"Extremely Positive (heart)"; message += " (" + (int)(sentimentScore * 100) + "%)"; return message; }

3. Finally,makethefollowinghighlightedchangestotheSearchTaskinRootDialog.cs.Withthesenewchanges,thebotwillbeabletounderstandiftheuserwantstosearchfornewsarticleswithpositiveornegativesentiment,andwillalsorenderthesentimentlabelofeacharticleintheNewsCardresults.

for (int i = 0; i < 10 && i < (bingNews?.totalEstimatedMatches ?? 0); i++) { var article = bingNews.value[i];

var sentimentScore = await getSentimentScore(article.description);

if (findPositive && sentimentScore < 0.6) { continue; } else if (findNegative && sentimentScore > 0.4) { continue; } HeroCard attachment = new HeroCard() { Title = article.name.Length > 60 ? article.name.Substring(0, 57) +

"..." : article.name, Subtitle = getSentimentLabel(sentimentScore),

Text = article.provider[0].name + ", " + article.datePublished.ToString("d") + " - " + article.description,

Images = new List<CardImage>() { new CardImage(article.image?.thumbnail?.contentUrl + "&w=400&h=400") },

Buttons = new List<CardAction>() { new CardAction( ActionTypes.OpenUrl, title: "View on Web", value: article.url)} }; reply.Attachments.Add(attachment.ToAttachment());

Page 30: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page30

}

Task4–TestBotE2EinBotEmulator

1. Congratulations!Youcompetedallcodingsteps.Buildyourprojectandrunittotestthebote2ein

theEmulator.Hereareafewexamplesyoucantry(reminder:ifyourbotisnotabletoparsecorrectlytheintent/entitiesfromtheusermessages,youcanalwaysgobacktoLUIS.aiandre-trainyourmodelinreal-timeusingfreshutterances!)

• “Showmegoodnewsaboutgaming”

• “Positivepressaboutgaming”

Page 31: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page31

Page 32: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page32

• “Findnegativenewsaboutgaming”

Page 33: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page33

Exercise4:ConnectBottoSkypeEstimatedtimetocomplete:10minutes

Congratulationsformakingitthisfar!Inthislastexercise,youwilldeployyourbottothecloudandconnectittoSkype.Forthisstep,werecommendinstallingtheVisualStudioToolsforAzuretomaketheclouddeploymentprocesseasier.

Task1–CreateanewAzureWebApp

2. LogintoAzurePortal

3. ClickonthePlusbutton

4. SelectWeb+MobilefromthelistandthenselectWebAppfromtheFeaturedAppslist.Followtheon-screeninstructions.

• AddanAppName

• Chooseyoursubscription

• KeepCreateanewResourceGroupselectedandprovideanameforit

• ClickonAppServiceplan/locationandselectCreateNew

i. AddanamefortheAppServicePlan

ii. AddaLocation

iii. ChoosepricingtierandscrolldownuntilyouseetheFreetier

iv. ClicktheSelectbuttonintheChooseyourpricingtier

• HittheOKbuttonintheNewAppServicePlantab

Page 34: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page34

• TheWebApptabshouldnowbetheonlyoneopen.KeepApplicationInsightsoff,selectPintodashboardatthebutton,andthenclickCreate.

5. YushouldnowbenavigatetoyourAzureDashboard.You’llseeyourWebApptileanditisnowinthedeploymentprocess.YouShouldbenavigatetoyourWebAppmanagementpageautomatically,ifnotclickonyournewWebApptileonyourdashboardoncedeploymentiscompleted.

6. OnyourWebAppcopyyourapp’sURL.Youwillneeditforthenextsteps.TheURLisundertheEssentialssectiononyourWebAppmanagementpage.

Page 35: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page35

Task2–DeployyourBottoAzureWebApp

1. ReturntoVisualStudio,right-clickyourSolution”EmotionalNewsBot”intheSolutionExplorerandselectPublish

2. ThedialogwillaskyoutologintoyourAzureaccount.Onceyoulogin,selectthesubscriptionunderwhichyoucreatedyourWebAppandthenselectyourWebAppfromthelistandclickOK.ThepublishingexperienceinVisualStudio2017ismuchdifferentthenin2015,we’llnotethesedifferencesbelow:

VisualStudio2015

VisualStudio2017*chooseMicrosoftAzureAppService,chooseSelectExisting,thenclickPublish.

Page 36: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page36

You’llthenseethebelowscreen,makesureyou’veloggedintoyourAzureAccountasI'vedoneinthetopright.Thenselectyoursubscription,chooseResourceGroupview,andthenchooseyourWebAppundertheresourcegroupyoudeployedittoofromourportalwalkthroughearlierandhitOK.

Page 37: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page37

3. Ifeverythinggoeswell,theconnectionsettingswillalreadybepre-populatedintheConnectiondialog.Validatetheconnectionandthenhitpublish.Oncethepublishingprocesscompletes,navigatetohttps://{yourwebappname}.azurewebsites.nettoconfirmyourserviceissuccessfullypublished.Error!Hyperlinkreferencenotvalid.ThepublishingexperienceinVisualStudio2017ismuchdifferentthenin2015,we’llnotethesedifferencesbelow

VisualStudio2015

VisualStudio2017*you’llthenseethebelowPublishscreenandyourweb(noticetheoutputdialogatthebottom)willdeployforyouautomatically,noneedtohitPublish,unlessyoudonotseetheoutputLikeoursbelowshowingBuildandPublish1:succeededeach.

Page 38: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page38

Task3–ConnectBottoSkype

1. Gotoyourbot’spageinhttps://dev.botframework.com/bots.

2. ClickonEditontheleftsideofthescreen,nexttotheDetailslabel.

3. EdittheMessagingendpoint.AddtheURLoftheWebAppyoujustcreatedandappendthe/api/messagesendpointattheend.Itshouldlooksimilartothis.

4. ClickSavechangesatthebottom.Youshouldbeautonavigatedbacktothemainbotpage,ifnotgobacktothemainbotpage,andclickonTesttotesttheconnectiontoyourbot.Ifeverythinggoeswell,youwillreceiveaconfirmationmessage.

Page 39: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page39

5. FromtheChannelslist,findSkypeandclickonEdit

6. Inthedialogthatappears,selectEnable[Thenameofyourbotwillappearhere]onSkypeandhittheI’mdoneconfiguringbutton.

Page 40: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page40

Page 41: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page41

8. Finally,clickontheAddtoSkypebuttontoaddyourbottoyourSkypecontactsandstartinteractingwithit!YoumaybepromptedtodownloadSkypepreview,confirmthisandinstallit.Tip:YoucansharetheSkypeJoinlinkwithanyonethatwantstoplaywithyourbot.

Page 42: Build Smart Bots with Bot Framework and Microsoft … 3 Overview Microsoft Bot Framework and Microsoft Cognitive Services provide a comprehensive offering to build and deploy high

Page42

SummaryandMoreResources

CongratulationsoncompletingthisHands-onLab!Wehopeyouenjoyedtheassignment.YoushouldnowbecomfortabledevelopingyourownbotsusingMicrosoftBotFramework,andenrichingthemwithAIintelligenceviaMicrosoftCognitiveServices.

Herearesomenextstepstocontinueyourjourneyofmasteringbotdevelopment:

1. VisittheBotFrameworkDocssite(https://docs.botframework.com/)togetin-depthinformationaboutallthecapabilitiessupportedbyBotFramework.AnothergoodideaistotracktheBotBuilderSDKgithubrepoandparticipateinthetechnicaldiscussions.

2. Continuerefiningyourbot’sNLPmodelinhttps://www.luis.ai.Experimentbyaddingmoreintents/entitiesandintegratingthemintoyourbot’scode.BecomeanexpertinLUISbyreadingtheLUISdocsandwatchingthe10’gettingstartedvideo.

3. LearnmoreaboutalltheintelligenceAPIsofferedbyMicrosoftathttps://microsoft.com/cognitive.

4. Findbotcodesamplesandfullyfunctionalopen-sourcebotsattheBotBuilder-Samplesgithubrepo.Doyouhaveideasforfuturebotsamplesorfunctionalbotswecanaddtothecollection?We’dlovetohearfromyou.Simplyfileyourrequestinouruservoiceportal.

Feedback

Yourfeedbackisextremelyvaluableforus,sodon’thesitatetoreachoutifyouhaveideas,questionsorissuestoreport.

ForBotFrameworkfeedback:

- ReportissuesintheGithubissuestracker.Fortechnicalquestion,postaquestioninStackOverflow.comwiththetagbotframework.

ForMicrosoftCognitiveServicesfeedback:

- UsetheCognitiveServicesUserVoiceportaltosubmitideas,generalfeedbackorquestions.- Fortechnicalquestions,postaquestioninStackOverflow.comwiththetagmicrosoft-cognitive.