Skip to content

Instantly share code, notes, and snippets.

@little-einstien
Last active July 16, 2021 05:45
Show Gist options
  • Save little-einstien/7e62372e09d49ab9b01c1b240ba401cc to your computer and use it in GitHub Desktop.
Save little-einstien/7e62372e09d49ab9b01c1b240ba401cc to your computer and use it in GitHub Desktop.
<h4>Deploy your bot</h4><p>In this article we will show you how to deploy a basic bot to Azure. We will explain how to prepare your bot for deployment, deploy your bot to Azure, and test your bot in Web Chat.</p><h4>Prerequisites</h4><ul><li>A subscription to <a href="https://azure.microsoft.com/free/">Microsoft Azure</a>.</li><li>A C# bot that you have developed on your local machine.</li><li>Latest version of the <a href="https://docs.microsoft.com/en-us/cli/azure/install-azure-cli">Azure CLI</a>.</li><li>Familiarity with <a href="https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-overview">Azure CLI and ARM templates</a>.</li></ul><h4>Prepare for deployment</h4><p>Make sure that bot has been built in Release mode. In Visual Studio, make sure that the solution configuration is set to Release and perform a clean rebuild of the solution before continuing. The deployment may fail if the solution configuration is set to Debug.</p><p>When you Create a bot, the source code generated includes a DeploymentTemplates folder that contains ARM templates. The deployment process documented here uses one of the ARM templates to provision required resources for the bot in Azure by using the Azure CLI.</p><p>With the release of Bot Framework SDK 4.3, the use of a .bot file is deprecated. Instead, we use the appsettings.json configuration file to manage bot resources.</p><h4>Login to Azure</h4><p>Once you've created and tested a bot locally, you can deploy it to Azure. Open a command prompt to log in to the Azure portal.</p><div style="background: #707070;"><div style="background: grey; padding: 5px 5px;">CMD</div><div style="overflow-x: scroll; padding: 5px 5px;"><pre>az login</pre></div></div><p>A browser window will open, allowing you to sign in.</p><h4>Set the subscription</h4><p>Set the default subscription to use.</p><div style="background: #707070;"><div style="background: grey; padding: 5px 5px;">CMD</div><div style="overflow-x: scroll; padding: 5px 5px;"><pre>az account set --subscription "&lt;azure-subscription-id&gt;"</pre></div></div><p>If you aren't sure which subscription to use for deploying the bot, you can view the list of subscriptions for your account by using <code>az account list</code> command.</p><h4>Create the application registration</h4><p>In this step you will create an Azure application registration, which will allow:</p><ul><li>The user to interact with the bot via a set of channels, such as Web Chat.</li><li>The definition of OAuth Connection Settings to authenticate a user and to create a token used by the bot to access protected resources on behalf of the user.</li></ul><h4>Create the Azure application registration</h4><p>To create an Azure application registration, execute the following command:</p><div style="background: #707070;"><div style="background: grey; padding: 5px 5px;">CMD</div><div style="overflow-x: scroll; padding: 5px 5px;"><pre>az ad app create --display-name "displayName" --password "AtLeastSixteenCharacters_0" --available-to-other-tenants</pre></div></div><h4>Record the appId and appSecret values</h4><p>Copy and save the <code>appId</code> and <code>password</code> values. You will need them in the ARM deployment step.</p><h4>Create the bot application service</h4><p>When creating the bot application service, you can deploy your bot in a new or in an existing resource group, both via the <a href="https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/overview">Azure Resource Manager (ARM) template</a>. An ARM template is a JSON file that declaratively defines one or more Azure resources and that defines dependencies between the deployed resources. Make sure that you have the correct path to your bot project ARM deployment templates directory <code>DeploymentTemplates</code>, you need it to assign the value to the template file. We are going to deploy in new resource group</p><h4>Deploy via ARM template with new resource group</h4><p>In this step, you create a bot application service which sets the deployment stage for the bot. You use an ARM template, a new service plan and a new resource group. Run the following Azure cli command to start a deployment at subscription scope from a local template file.</p><div style="background: #707070;"><div style="background: grey; padding: 5px 5px;">CMD</div><div style="overflow-x: scroll; padding: 5px 5px;"><pre>az deployment sub create --template-file "&lt;path-to-template-with-new-rg.json" --location &lt;region-location-name&gt; --parameters appId="&lt;app-id-from-previous-step&gt;" appSecret="&lt;password-from-previous-step&gt;" botId="&lt;id or bot-app-service-name&gt;" botSku=F0 newAppServicePlanName="&lt;new-service-plan-name&gt;" newWebAppName="&lt;bot-app-service-name&gt;" groupName="&lt;new-group-name&gt;" groupLocation="&lt;region-location-name&gt;" newAppServicePlanLocation="&lt;region-location-name&gt;" --name "&lt;bot-app-service-name&gt;"</pre></div></div><p>This step can take a few minutes to complete.</p><h4>Create New App Service Plan</h4><div style="background: #707070;"><div style="background: grey; padding: 5px 5px;">CMD</div><div style="overflow-x: scroll; padding: 5px 5px;"><pre>az deployment group create --resource-group "&lt;name-of-resource-group&gt;" --template-file "&lt;path-to-template-with-preexisting-rg.json&gt;" --parameters appId="&lt;app-id-from-previous-step&gt;" appSecret="&lt;password-from-previous-step&gt;" botId="&lt;id or bot-app-service-name&gt;" newWebAppName="&lt;bot-app-service-name&gt;" newAppServicePlanName="&lt;name-of-app-service-plan&gt;" newAppServicePlanLocation="&lt;region-location-name&gt;" --name "&lt;bot-app-service-name&gt;"</pre></div></div><h4>Prepare your code for deployment</h4><h4>Assign app Id and password</h4><p>Add the app ID and password for the Azure Bot resource to your bot project configuration file.</p><p>The</p><pre>appsettings.json</pre><p>file contains these settings:</p><div style="background: #707070;"><div style="background: grey; padding: 5px 5px;">JSON</div><div style="overflow-x: scroll; padding: 5px 5px;"><pre>{ "MicrosoftAppId": "&lt;your app id&gt;", "MicrosoftAppPassword": "&lt;your password&gt;"}</pre></div></div><h4>Prepare project</h4><p>You need to prepare your project files before you can deploy your bot.</p><p>Make sure you're in your bot's project folder. Then prepare your bot code for deployment.</p><div style="background: #707070;"><div style="background: grey; padding: 5px 5px;">CMD</div><div style="overflow-x: scroll; padding: 5px 5px;"><pre>az bot prepare-deploy --lang Csharp --code-dir "." --proj-file-path "MyBot.csproj"</pre></div></div><p>You must provide the path to the .csproj file relative to --code-dir. This can be performed via the --proj-file-path argument. The command would resolve --code-dir and --proj-file-path to "./MyBot.csproj".</p><p>This command generates a</p><pre>.deployment</pre><p>file in your bot project folder.</p><h4>Package project</h4><p>Create a zip file of your project</p><p><em>Before zipping your project files, make sure that you are in the bot's project folder.</em></p><p>For C# bots, it is the folder that has the .csproj file.</p><p>Within the project folder, make sure you select all the files and folders before running the command to create the zip file. This will create a single zip file within the project folder. If your root folder location is incorrect, the bot will fail to run in the Azure portal.</p><h4>Deploy code to Azure</h4><p>Run the following command from the command line to perform deployment using the kudu zip push deployment for a web app.</p><div style="background: #707070;"><div style="background: grey; padding: 5px 5px;">CMD</div><div style="overflow-x: scroll; padding: 5px 5px;"><pre>az webapp deployment source config-zip --resource-group "&lt;resource-group-name&gt;" --name "&lt;name-of-web-app&gt;" --src "&lt;project-zip-path&gt;"</pre></div></div><h4>Test in Web Chat</h4><ol><li>In your browser, navigate to the <a href="https://ms.portal.azure.com/">Azure portal</a>.</li><li>In the left panel, click Resource groups.</li><li>In the right panel, search for your group.</li><li>Click on your group name.</li><li>Click the link of your Bot Channels Registration.</li><li>In the Bot Channels Registration panel, click Test in Web Chat. Alternatively, in the right panel, click the Test box</li></ol>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment