{"id":4465,"date":"2019-01-25T14:38:31","date_gmt":"2019-01-25T19:38:31","guid":{"rendered":"http:\/\/lisa.rushworth.us\/?p=4465"},"modified":"2019-01-25T17:18:04","modified_gmt":"2019-01-25T22:18:04","slug":"creating-an-azure-bot-internally-hosted","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=4465","title":{"rendered":"Creating An Azure Bot \u2013 Internally Hosted"},"content":{"rendered":"<p>While hosting a bot on the Azure network allows you to use <a href=\"http:\/\/lisa.rushworth.us\/?p=4051\" target=\"_blank\" rel=\"noopener\">pre-built solutions<\/a> or <a href=\"http:\/\/lisa.rushworth.us\/?p=4019\" target=\"_blank\" rel=\"noopener\">develop a bot<\/a> without purchasing dedicated hardware, the bots we\u2019ve deployed thus far do <em>not<\/em> have access to internally-housed data. And program execution can be slow (expensive, or a combination of the two) depending on the chosen pricing plan. But you can build an Azure bot that is essentially a proxy to a self-hosted bot.<\/p>\n<p>This means you can host the bot on your private network (it needs to be accessible from the Azure subnets) and access internal resources from your bot code. Obviously, there are security implications to making private data available via an Azure bot \u2013 you might want to implement <a href=\"https:\/\/blog.botframework.com\/2018\/09\/01\/using-webchat-with-azure-bot-services-authentication\/\">user authentication to verify the bot user\u2019s identity<\/a>, and I wouldn\u2019t send someone their current credit card information over a bot even <em>with<\/em> authentication.<\/p>\n<h2>How to Communicate with a Self-hosted Bot from Azure:<\/h2>\n<p>Register an Azure bot. From <a href=\"https:\/\/portal.azure.com\">https:\/\/portal.azure.com<\/a>, select \u201cCreate a resource\u201d. Search for \u201cbot\u201d and select \u201cBot Channels Registration\u201d.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1917\" height=\"657\" class=\"wp-image-4466\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-46.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-46.png 1917w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-46-300x103.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-46-768x263.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-46-1024x351.png 1024w\" sizes=\"auto, (max-width: 1917px) 100vw, 1917px\" \/><\/p>\n<p>On the pane which flies out to the right, click \u201cCreate\u201d (if you will be deploying multiple self-hosted bots to Azure, click the little heart so you can find this item on \u201cMy Saved List\u201d when creating a new resource).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"500\" class=\"wp-image-4467\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-47.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-47.png 716w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-47-300x209.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/p>\n<p>Provide a unique name for your Azure bot. If you have not yet created a resource group, you will need to create one. Make sure the hosting location is reasonable for your user base \u2013 East Asia doesn\u2019t make sense for something used on the East coast of the US!<\/p>\n<p>Select the pricing tier you want \u2013 I use F0 (free) which allows unlimited messages in standard channels (Teams, Skype, Cortana) and 10,000 messages sent\/received in premium channels (direct user interaction \u2026 which I specifically <em>don\u2019t<\/em> want in this case). Then provide the endpoint URL to interact with your locally hosted bot.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"393\" height=\"853\" class=\"wp-image-4468\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-48.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-48.png 393w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-48-138x300.png 138w\" sizes=\"auto, (max-width: 393px) 100vw, 393px\" \/><\/p>\n<p>Click \u201cCreate\u201d and Azure will begin deploying your new resource. You can click the \u201cNotifications\u201d bell icon in the upper right-hand portion of the page to view deployment progress.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"290\" class=\"wp-image-4469\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-49.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-49.png 662w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-49-300x131.png 300w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/p>\n<p>When deployment completes, click \u201cGo to resource\u201d to finish configuring your Azure bot.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"653\" height=\"303\" class=\"wp-image-4470\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-50.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-50.png 653w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-50-300x139.png 300w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/p>\n<p>Select \u201cSettings\u201d from the left-hand navigation menu, then find the application ID. Click \u201cManage\u201d.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1613\" height=\"839\" class=\"wp-image-4471\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-51.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-51.png 1613w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-51-300x156.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-51-768x399.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-51-1024x533.png 1024w\" sizes=\"auto, (max-width: 1613px) 100vw, 1613px\" \/><\/p>\n<p>This will open a new portal \u2013 you may be asked to sign in again. You are now looking at the application registration in Microsoft\u2019s developer application registration portal. There\u2019s already an application secret created <em>but<\/em> beyond the first few letters \u2026 what is it? No idea! I\u2019m a cautious person, and I don\u2019t know if MS has embedded this secret somewhere within the bot resource. Since an application can have two secrets simultaneously, I do <em>not<\/em> delete the automatically-created secret and click \u201cGenerate New Password\u201d.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1581\" height=\"763\" class=\"wp-image-4472\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-52.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-52.png 1581w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-52-300x145.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-52-768x371.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-52-1024x494.png 1024w\" sizes=\"auto, (max-width: 1581px) 100vw, 1581px\" \/><\/p>\n<p>A new pane will appear with your new secret \u2013 no, the one in the picture isn\u2019t real. Copy that and store it somewhere \u2013 you\u2019ll need to add it to your bot code later.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"603\" height=\"357\" class=\"wp-image-4473\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-53.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-53.png 603w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-53-300x178.png 300w\" sizes=\"auto, (max-width: 603px) 100vw, 603px\" \/><\/p>\n<p>Close the application registration tab and return to the Azure portal tab. Click on \u201cChannels\u201d in your bot and add channels for any interactions you want to support. In this case, I want to publish my bot to Teams. There aren\u2019t really settings* for teams \u2013 just click to create the channel.<\/p>\n<p>* You <em>can<\/em> publish a bot to the Microsoft App Source \u2026 but is your bot something that should be available to the Internet at large? It depends! If you\u2019re writing a bot to provide enterprise customers another support avenue, having the bot available through App Source makes sense. If you\u2019re creating a bot to answer employee-specific questions, then you probably want to keep the bot out of App Source<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1637\" height=\"815\" class=\"wp-image-4474\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-54.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-54.png 1637w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-54-300x149.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-54-768x382.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-54-1024x510.png 1024w\" sizes=\"auto, (max-width: 1637px) 100vw, 1637px\" \/><\/p>\n<p>Once the channel has been created, click on the \u201cGet bot embed codes\u201d hyperlink to obtain the bot URL.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1612\" height=\"555\" class=\"wp-image-4475\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-55.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-55.png 1612w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-55-300x103.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-55-768x264.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-55-1024x353.png 1024w\" sizes=\"auto, (max-width: 1612px) 100vw, 1612px\" \/><\/p>\n<p>Individuals can use the hyperlink provided to add your bot to their Teams chat.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"846\" height=\"403\" class=\"wp-image-4476\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-56.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-56.png 846w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-56-300x143.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-56-768x366.png 768w\" sizes=\"auto, (max-width: 846px) 100vw, 846px\" \/><\/p>\n<p>Ok, done! Except for one little thing \u2013 you need something to <em>answer<\/em> on that endpoint we entered earlier. You need a bot! Microsoft publishes an <a href=\"https:\/\/github.com\/Microsoft\/BotBuilder\">SDK and tools for building your bot in .NET, JavaScript, Python, and Java<\/a>.<\/p>\n<p>In this example, I am using a sample Python bot. For convenience, I am handling SSL on my reverse proxy instead of using an ssl wrapper in my Python service. Grab the BotBuilder package from git (<a href=\"https:\/\/github.com\/Microsoft\/botbuilder-python.git\">https:\/\/github.com\/Microsoft\/botbuilder-python.git<\/a>)<\/p>\n<p>Install the stuff:<\/p>\n<p>pip3 install -e .\/libraries\/botframework-connector<\/p>\n<p>pip3 install -e .\/libraries\/botbuilder-schema<\/p>\n<p>pip3 install -e .\/libraries\/botbuilder-core<\/p>\n<p>pip3 install -r .\/libraries\/botframework-connector\/tests\/requirements.txt<\/p>\n<p>In the .\/samples\/ folder, you\u2019ll find a few beginner bots. Rich-Cards-Bot requires msrest that has some async functionality and the branch in requirements.txt doesn\u2019t exist. Tried a few others and never got anything that worked properly. Same problem with EchoBot-with-State. I am using Echo-Connector-Bot because it doesn\u2019t have this msrest problem, and I can add my own state support later.<\/p>\n<p>Edit main.py and add your Azure bot application id &amp; secret to APP_ID and APP_PASSWORD<\/p>\n<p>APP_ID = &#8221;<\/p>\n<p>APP_PASSWORD = &#8221;<\/p>\n<p>PORT = 9000<\/p>\n<p>SETTINGS = BotFrameworkAdapterSettings(APP_ID, APP_PASSWORD)<\/p>\n<p>ADAPTER = BotFrameworkAdapter(SETTINGS)<\/p>\n<p>I stash my personal information in a config.py file and added an import to main.py:<\/p>\n<p>from config import strDBHostname, strDBUserName, strDBPassword, strDBDatabaseName, strDBTableName, APP_ID, APP_PASSWORD<\/p>\n<p>Tweak the code however you want \u2013 add <a href=\"https:\/\/www.nltk.org\/\">natural language processing<\/a>, make database connections to internal resources to determine responses, make calls to internal web APIs. I also added console output so I could debug bot operations.<\/p>\n<p>When you\u2019ve completed your changes, launch your bot by running \u201cpython main.py\u201d<\/p>\n<p><a href=\"http:\/\/lisa.rushworth.us\/?attachment_id=4482\" rel=\"attachment wp-att-4482\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-4482\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/Bot-SelfHosted10-1024x197.png\" alt=\"\" width=\"960\" height=\"185\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/Bot-SelfHosted10-1024x197.png 1024w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/Bot-SelfHosted10-300x58.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/Bot-SelfHosted10-768x148.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/Bot-SelfHosted10.png 1373w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>Now return to the Azure portal and select \u201cTest in Web Chat\u201d \u2013 this will allow you to test interactions with your bot. Ask questions \u2013 you should see your answers returned.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1633\" height=\"849\" class=\"wp-image-4478\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-58.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-58.png 1633w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-58-300x156.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-58-768x399.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-58-1024x532.png 1024w\" sizes=\"auto, (max-width: 1633px) 100vw, 1633px\" \/><\/p>\n<p>Once you confirm the bot is functioning properly, use the URL from the Teams channel to interact with your bot within Teams &#8212;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1375\" height=\"845\" class=\"wp-image-4479\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2019\/01\/word-image-59.png\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-59.png 1375w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-59-300x184.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-59-768x472.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/01\/word-image-59-1024x629.png 1024w\" sizes=\"auto, (max-width: 1375px) 100vw, 1375px\" \/><\/p>\n<p>URL for my bot in Teams: <a href=\"https:\/\/teams.microsoft.com\/l\/chat\/0\/0?users=28:9699546d-fc09-41bf-b549-aed33280693a\">https:\/\/teams.microsoft.com\/l\/chat\/0\/0?users=28:9699546d-fc09-41bf-b549-aed33280693a<\/a><\/p>\n<p>The answer is served out of our home automation database \u2013 data that is only accessible on our private network.<\/p>\n<p><strong><em>Security<\/em><\/strong> \u2013 as I said earlier, you\u2019ll probably want to take <em>some<\/em> measures to ensure access to your locally hosted bot is coming from legit sources. The app ID and secret provide one level of protection. If a connection does not supply the proper app ID &amp; secret (or if you\u2019ve mis-entered those values in your code!), you\u2019ll get a 401 error.<\/p>\n<p>&nbsp;<\/p>\n<p>But I don\u2019t want the entire Internet DDoS\u2019ing by bot either, and there is no reason for anyone outside of Microsoft Azure subnets should be accessing my locally hosted bot. My bot is hosted in a private container. The reverse proxy allows Internet-sourced traffic in to the private bot resource. Since communication from Azure will be sourced from a known set of networks, you can add a source IP restriction that prevents the general public from accessing your bot directly. See <a href=\"https:\/\/azurerange.azurewebsites.net\/\">https:\/\/azurerange.azurewebsites.net\/<\/a> for a convenient-to-use list of addresses.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While hosting a bot on the Azure network allows you to use pre-built solutions or develop a bot without purchasing dedicated hardware, the bots we\u2019ve deployed thus far do not have access to internally-housed data. And program execution can be slow (expensive, or a combination of the two) depending on the chosen pricing plan. But &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,677],"tags":[728,666,665],"class_list":["post-4465","post","type-post","status-publish","format-standard","hentry","category-coding","category-office-365","tag-azure-bot","tag-microsoft-teams","tag-teams"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/4465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4465"}],"version-history":[{"count":3,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/4465\/revisions"}],"predecessor-version":[{"id":4485,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/4465\/revisions\/4485"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}