TelegramNavigation 2.0.0

dotnet add package TelegramNavigation --version 2.0.0
                    
NuGet\Install-Package TelegramNavigation -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TelegramNavigation" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TelegramNavigation" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="TelegramNavigation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TelegramNavigation --version 2.0.0
                    
#r "nuget: TelegramNavigation, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TelegramNavigation@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TelegramNavigation&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=TelegramNavigation&version=2.0.0
                    
Install as a Cake Tool

Telegram Navigation

.NET 9.0 NuGet Version

Telegram Navigation is .NET library based on .NET Client for Telegram Bot API to enchance creating navigation and multi-level telegram menus experience.

Getting started

Create inline button

var bot = new TelegramBotClient("<YOUR TOKEN>");

var inlineButton = InlineMiddleware.CreateButton("my button",
    async (route, bot, msg, from) =>
    {
        await bot.EditMessageText(msg.Chat.Id, msg.Id,
            $"\"my button\" with myArgument = {{{route.Args?["myArgument"]}}} pressed!");
        // your logic...
    }, new() { ["myArgument"] = "some data" });


await bot.SendMessage(chatId, "Message with inline button", replyMarkup: inlineButton.Button);

await bot.ReceiveAsync(async (bot, update, ct) =>
{
    if (update.Type == UpdateType.CallbackQuery)
        await InlineMiddleware.HandleAsync(bot, update.CallbackQuery!);
}, (bot, ex, ct) => Console.WriteLine(ex));

Create message hook

var bot = new TelegramBotClient("<YOUR API TOKEN>");

var messageHandler = new MessageHandler();

messageHandler.RegisterHook(targetChatId, targetUserId,
    async (bot, msg, from) =>
    {
        await bot.SendMessage(msg.Chat.Id, "Message hooked!",
               replyParameters: new ReplyParameters() { MessageId = msg.Id });
        messageHandler.UnregisterHook(msg.Chat.Id, from.Id); // remove hook
        // your logic...
    });

await bot.ReceiveAsync(async (bot, update, ct) =>
{
    if (update.Type == UpdateType.Message)
        await messageHandler.HandleAsync(bot, update.Message!);
}, (bot, ex, ct) => Console.WriteLine(ex));

Create page component

using System.Text;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
using TelegramNavigation;
using TelegramNavigation.Interfaces;
using TelegramNavigation.Routing;

namespace ExampleBot
{
    [InlineComponent(name: "examplePage")] // register component name
    class ExamplePageComponent : IInlineQueryComponent, IInlinePageComponent
    {
        private readonly Dictionary<string, InlineQueryHook> _routes;
        private readonly string[] _source = Enumerable.Range(1,20).Select(i=>$"item {i}").ToArray();
        public ExamplePageComponent()
        {
            _routes = new()
            {
                ["/moveBack"] = MoveBack,
                ["/moveNext"] = MoveNext
            };
        }
        public async Task HandleQueryAsync(Route queryRoute, ITelegramBotClient botClient, Message message, User from)
            => await _routes[queryRoute.Path!].Invoke(queryRoute, botClient, message, from);


        public async Task<Message?> InitializeAsync(Route queryRoute, ITelegramBotClient botClient, long chatId, int? messageThreadId = null)
        {
            var botMessage = await botClient.SendMessage(chatId,
                "Loading..."); // Send placeholder message
            if (_source.Length > 0)
            {
                var page = InlineMiddleware.CreatePage(chatId, botMessage.Id, _source.Length, 5, "myPage");
                await botClient.EditMessageText(chatId, botMessage.Id,
                    GetText(page),
                    replyMarkup: GetMarkup(page));
            }
            else
                await botClient.EditMessageText(chatId, botMessage.Id,
                    "No data found");
            return botMessage;
        }

        public async Task MoveBack(Route queryRoute, ITelegramBotClient botClient, Message message, User user)
        {
            var page = InlineMiddleware.GetPage(message.Chat.Id, message.Id, "myPage");
            page!.MoveBack();
            await botClient.EditMessageText(message.Chat.Id, message.Id,
                GetText(page),
                replyMarkup: GetMarkup(page));

        }

        public async Task MoveNext(Route queryRoute, ITelegramBotClient botClient, Message message, User user)
        {
            var page = InlineMiddleware.GetPage(message.Chat.Id, message.Id, "myPage");
            page!.MoveNext();
            await botClient.EditMessageText(message.Chat.Id, message.Id,
                GetText(page),
                replyMarkup: GetMarkup(page));
        }
        private string GetText(PageController page)
        {
            StringBuilder sb = new();
            var items = _source.Skip(page.Offset).Take(page.ElementsCount);
            foreach (var item in items)
                sb.AppendLine(item);
            return sb.ToString();
        }
        private static InlineKeyboardMarkup GetMarkup(PageController page)
        {
            var markup = new InlineKeyboardMarkup();
            markup.AddButton(new InlineKeyboardButton()
            {
                Text = page.PreviousPage > -1 ? "<<" : " ",
                CallbackData = page.PreviousPage > -1 ? new Route("examplePage", "/moveBack", null).ToString() : "none"
            });
            markup.AddButton($"{page.CurrentPage} / {page.PagesCount}", "none");
            markup.AddButton(new InlineKeyboardButton()
            {
                Text = page.NextPage > -1 ? ">>" : " ",
                CallbackData = page.NextPage > -1 ? new Route("examplePage", "/moveNext", null).ToString() : "none"
            });
            InlineMiddleware.AddCloseButton(markup, "Close");
            return markup;
        }
    }

    internal class Program
    {
        static async Task Main(string[] args)
        {
            var bot = new TelegramBotClient("<YOUR API TOKEN>");

            InlineMiddleware.RegisterComponent(new StandardComponent(null)); // register standard component for close button route
            InlineMiddleware.RegisterComponent(new ExamplePageComponent()); // register our page component

            // Send our component
            await InlineMiddleware.SendComponent(new Route(type: "examplePage", path: string.Empty, args: null), bot, chatId);

            await bot.ReceiveAsync(async (bot, update, ct) =>
            {
                if (update.Type == UpdateType.CallbackQuery) // receive all callback queries with inline middleware
                    await InlineMiddleware.HandleAsync(bot, update.CallbackQuery);

            }, (bot, ex, ct) => Console.WriteLine(ex));

            Console.ReadLine();
        }
    }
}

More examples

You can get more examples of handling commands, hooks, creating some forms and components. See ExampleBot project.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 35 8/27/2025
1.0.0 20 8/23/2025

- Added `InlineComponent` attribute
- Removed `type` argument in `InlineMiddleware.RegisterComponent` method
- Changes in [ExampleBot](https://github.com/ST0PL/TelegramNavigation/tree/main/ExampleBot) project related to `InlineComponent` attribute and small fixes