TelegramUpdater

Here is Updater

!! Preview !!

This is your telegram updater package written in C# and .NET 6. The updater is supposed to fetch and handle new updates coming from bot api server. It's written on top of TelegramBots/Telegram.Bot: .NET Client for Telegram Bot API package

More support

We can talk through @TUTalkings if you want to.

Why use this?

using Telegram.Bot.Types;
using TelegramUpdater.FilterAttributes.Attributes;
using TelegramUpdater.Filters;
using TelegramUpdater.UpdateContainer;
using TelegramUpdater.UpdateHandlers.Scoped.ReadyToUse;

namespace ConsoleApp;

[Command("test"), Private]
internal class MyScopedMessageHandler : MessageHandler
{
    protected override async Task HandleAsync(IContainer<Message> _)
    {
        await ResponseAsync("Tested!");
    }
}
updater = new Updater("BotToken",
    new UpdaterOptions(
        maxDegreeOfParallelism: 10, // maximum update process tasks count at the same time
                                    // Eg: first 10 updates are answers quickly, but others should wait
                                    // for any of that 10 to be done.

    .AddScopedUpdateHandler<MyScopedMessageHandler, Message>(); // Scoped handler;

await updater.StartAsync(); // 🔥 Fire up and block!

As instance ChannelUserClick is an helper method for OpenChannel that waits for a user click.

[Command("start"), Private]
internal sealed class MyMessageHandler : MessageHandler
{
    protected override async Task HandleAsync(IContainer<Message> _)
    {
        await AwaitButtonClickAsync(
            TimeSpan.FromSeconds(10), new CallbackQueryRegex("^hello"))

        .IfNotNull(async answer =>
        {
            await answer.EditAsync(text: "Well ...");
        })
        .Else(async _ =>
        {
            await ResponseAsync("Slow");
        });
    }
}
    .StepTwo(CommonExceptions.ParsingException(
        (updater, ex) =>
        {
            updater.Logger.LogWarning(exception: ex, "Handler has entity parsing error!");
            return Task.CompletedTask;
        },
        allowedHandlers: new[]
        {
            typeof(AboutMessageHandler),
            typeof(MyScopedMessageHandler)
        }))

Getting Started

Here are starting pack for common SDKs in .NET

TelegramUpdater in available in nuget, Install it first.

Basic

If you're using a console app with no Hosting and IServiceCollection then it's your choice And even if you don't, you're suggested to!

Base class of this package is Updater, but there's a helper class in case of basic apps called UpdaterBuilder which helps you get familiar with the package.

UpdaterBuilder helps you build Updater in steps with fully documented methods. See UpdaterProduction and ConsoleApp for instance.

If you're looking for a quick basic example:

// See https://aka.ms/new-console-template for more information
using TelegramUpdater;

var updater = new UpdaterBuilder("BOT_TOKEN")

    .StepOne()

    .StepTwo(inherit: false) // Add default exception handler

    .StepThree( // Quick handler
        async container => await container.Response("Started!"),
        FilterCutify.OnCommand("start"));

// ---------- Start! ----------

await updater.StartAsync(); // 🔥 Fire up and block!

Of course this can be even less, but these're required for production! For instance StepTwo adds a default exception handler ( Take a look at methods docs! )

IHost apps

It maybe better if you use Updater in a app where IServiceCollection and IServiceProvider are available. Like WorkerService!

Use this package for a set of useful extensions for IHosting apps.

Take a look at two examples of worker services

A quick worker service

using WorkerService;

IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddTelegramUpdater(
            "BOT_TOKEN",
            default,
            (builder) => builder
                .AddMessageHandler<SimpleMessageHandler>()
        );
    })
    .Build();

await host.RunAsync();

Road Map

Next?

Find documents under https://telegramupdater.github.io/Docs/ ( Yet Working on it ... )

Home

Home

Changelogs

2022

Getting Started

First More Second

Installations

Linux Windows