DEV Community

BotticelliBots
BotticelliBots

Posted on

1

Creating a Telegram Bot with BotticelliBots: A Quick Guide

Image description

BotticelliBots is an open-source .NET Core framework that streamlines the development of chatbots. In this guide, we’ll walk through creating a simple Telegram bot that displays the current UTC time, leveraging the framework's capabilities.

Prerequisites

Ensure you have .NET Core 8.0 or higher and either Visual Studio 2022 or Rider 2023.x. Download BotticelliBots v. 0.3 as a submodule in your project.

Step 1: Register Your Bot

Use the Telegram BotFather to create a new bot and obtain your bot token.

Step 2: Create a Web API Project

In Visual Studio, create a new .NET Core Web API project.

Step 3: Configure Your Bot

In Program.cs, add the necessary services:

builder.Services.AddTelegramBot(builder.Configuration, new BotOptionsBuilder<TelegramBotSettings>()
    .Set(s => s.Name = settings?.BotName));
builder.Services.AddHostedService<VeryBasicBotService>();
builder.Services.AddBotCommand<GetUtcCommand, GetUtcCommandProcessor<ReplyKeyboardMarkup>, PassValidator<GetUtcCommand>>();
Enter fullscreen mode Exit fullscreen mode

Step 4: Implement Command Processor

Create a command processor to handle the /GetUtc command:

public class GetUtcCommandProcessor<TReplyMarkupBase> : CommandProcessor<GetUtcCommand>
{
    protected override async Task InnerProcess(Message message, string args, CancellationToken token)
    {
        var utcMessageRequest = new SendMessageRequest(Guid.NewGuid().ToString())
        {
            Message = new Message
            {
                Body = $"Current UTC Time is: {DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)}",
            }
        };
        await _bot.SendMessageAsync(utcMessageRequest, null, token);
    }
}

Enter fullscreen mode Exit fullscreen mode

Step 5: Run Your Bot

Deploy your bot and ensure it’s operational. You can manage it through the admin pane, where you can register and configure your bot.

About admin pane and it's setup you can read here.

Sample Bots

For more examples and inspiration, check out the Telegram AI Sample on GitHub.

For further details, visit BotticelliBots or join the community on Telegram for support and updates!
Step 5: Run Your Bot

Deploy your bot and ensure it’s operational. You can manage it through the admin pane, where you can register and configure your bot.
Sample Bots

For more examples and inspiration, check out the Telegram AI Sample on GitHub.

For further details, visit BotticelliBots or join the community on Telegram for support and updates!

Top comments (0)