Logo

dev-resources.site

for different kinds of informations.

Deploy Your Bot to Azure in 5 Minutes with Azure Developer CLI (azd)

Published at
12/22/2024
Categories
dotnet
aspnetcore
azure
Author
Oleksii Nikiforov
Categories
3 categories in total
dotnet
open
aspnetcore
open
azure
open
Deploy Your Bot to Azure in 5 Minutes with Azure Developer CLI (azd)

TL;DR

Source code: https://github.com/NikiforovAll/azd-bot-service-starter

Introduction

Bots are everywhere, as developers, we should embrace this trend and learn how to prototype and deploy bots quickly.

Using Azure Developer CLI (azd), we can quickly deploy and manage our solutions. This tutorial shows you how to deploy a bot to Azure in just five minutes using a starter template.

🚀 Let’s get started and deploy your bot to Azure!

Install From Template

Since I already prepared a starter template, you can deploy a bot to Azure in just a few minutes.

Here’s how:

azd init --template https://github.com/NikiforovAll/azd-bot-service-starter

Here is the sneak peek of the bot that we are going to deploy:

// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddNewtonsoftJson();
builder.Services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
builder.Services.AddTransient<IBot, EchoBotHandler>();

var app = builder.Build();
app
    .UseDefaultFiles()
    .UseStaticFiles()
    .UseWebSockets()
    .UseRouting()
    .UseAuthorization();

app.MapPost(
    "/api/messages",
    async (IBotFrameworkHttpAdapter adapter, IBot bot, HttpRequest req, HttpResponse res) =>
    {
        await adapter.ProcessAsync(req, res, bot);
    }
);
app.MapHealthChecks("/health");
app.Run();

It’s a simple echo bot that repeats the user’s message back to them. Nothing special, but this is a good starting point for your bot.

public class EchoBotHandler : ActivityHandler
{
    protected override async Task OnMessageActivityAsync(
        ITurnContext<IMessageActivity> turnContext,
        CancellationToken cancellationToken
    )
    {
        var replyText = $"Echo: {turnContext.Activity.Text}";
        await turnContext.SendActivityAsync(
            MessageFactory.Text(replyText, replyText),
            cancellationToken
        );
    }
}

Run and Deploy

Now, we can create a new azd environment:

azd env new azd-bot-service-dev

And deploy the bot to Azure.

azd up

Here is the output of the deployment (it took me less than 5 minutes to deploy the bot 🤯):

The azd CLI outputs the URL of the deployed bot. You can test it by sending a message to the bot.

The benefit of using azd template is that it automatically creates a new resource group, app service plan, application insights, vault, and app service for you. You don’t need to worry about the infrastructure, just focus on your code.

For example, here is an application insights dashboard for the bot:

Teardown

When you are done with the bot, you can delete the resources using the following command:

azd down

This command will delete all the resources created by the azd up command.

Conclusion

🙌 I hope you found it helpful. If you have any questions, please feel free to reach out. If you’d like to support my work, a star on GitHub would be greatly appreciated! 🙏

References

Featured ones: