TelegramUpdater 1.4.0-preview.2.3

This is a prerelease version of TelegramUpdater.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package TelegramUpdater --version 1.4.0-preview.2.3
NuGet\Install-Package TelegramUpdater -Version 1.4.0-preview.2.3
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="TelegramUpdater" Version="1.4.0-preview.2.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TelegramUpdater --version 1.4.0-preview.2.3
#r "nuget: TelegramUpdater, 1.4.0-preview.2.3"
#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.
// Install TelegramUpdater as a Cake Addin
#addin nuget:?package=TelegramUpdater&version=1.4.0-preview.2.3&prerelease

// Install TelegramUpdater as a Cake Tool
#tool nuget:?package=TelegramUpdater&version=1.4.0-preview.2.3&prerelease

Here is Updater

** !! Preview !! **

This is your telegram updater package written in C# and dotnet core 3.1

The updater is supposed to fetch and handle new updates coming from bot api server

The updater is 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?

  • Updater uses update handlers which are a great help to write clean, simple to write and read and more powerful code base.
  • Updater provides Filter<T> class that helps you to easily choose the right handler for incoming updates. Take a look at code below describing a handler:
using Telegram.Bot.Types;
using TelegramUpdater;
using TelegramUpdater.UpdateContainer;
using TelegramUpdater.FilterAttributes.Attributes;
using TelegramUpdater.UpdateHandlers.ScopedHandlers.ReadyToUse;

namespace ConsoleApp;

[Command("test")]
internal class MyScopedMessageHandler : ScopedMessageHandler
{
    public MyScopedMessageHandler() : base(group: 0)
    { }

    protected override async Task HandleAsync(UpdateContainerAbs<Message> container)
    {
        await container.Response("Tested!");
    }
}
  • Simple setup
  • Useful options
updater = new Updater(new TelegramBotClient("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.

        allowedUpdates: new[] { UpdateType.Message, UpdateType.CallbackQuery }))

    .AddScopedMessage<MyScopedMessageHandler>(); // Scoped handler;

await updater.StartAsync(); // 🔥 Fire up and block!
  • OpenChannel Method! You can use this to wait for a user response.
  • Batch of extension methods to increase speed and make cleaner code.

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

var msg = await container.Response($"Are you ok? answer quick!",
    replyMarkup: new InlineKeyboardMarkup(
        InlineKeyboardButton.WithCallbackData("Yes i'm OK!", "ok")));

await container.ChannelUserClick(TimeSpan.FromSeconds(5), "ok")
    .IfNotNull(async answer =>
    {
        await answer.Edit(text: "Well ...");
    })
    .Else(async _ =>
    {
        await container.Response("Slow", sendAsReply: false);
    });
  • ExceptionHandlers, handle exceptions like a pro with highly customizable exceptions handlers. you specify exception type, message match, handler type and ...
    .StepTwo(CommonExceptions.ParsingException(
        (updater, ex) =>
        {
            updater.Logger.LogWarning(exception: ex, "Handler has entity parsing error!");
            return Task.CompletedTask;
        },
        allowedHandlers: new[]
        {
            typeof(AboutMessageHandler),
            typeof(MyScopedMessageHandler)
        }))
  • Supports DI and batch of extension methods for hosted or webhook apps ( thanks to Telegram.Bot webhook example )
  • Updater has a lot of base classes, interfaces and generic types to make everything highly customizable.
  • More ...

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();

Webhook app ( Asp .NET )

Webhook app is similar to IHosting app where Update Writer is external! ( Updates are written to updater from webhook Controller )

Use this nuget package for a full set of extensions for webhook aps.

And take a look at this webhook example.

Road Map

  • Add ready to use handlers for all updates
  • Tests
  • Add more filters

Next?!

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

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

NuGet packages (2)

Showing the top 2 NuGet packages that depend on TelegramUpdater:

Package Downloads
TelegramUpdater.Hosting

Hosting extension package for TelegramUpdater

TelegramUpdater.FillMyForm

This is an extension package for TelegramUpdater to help you fill a form in a blink of an eye.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.0-preview.5.3 142 4/28/2022
1.4.0-preview.5.2 117 4/10/2022
1.4.0-preview.4.9 104 4/4/2022
1.4.0-preview.4.4 116 4/3/2022
1.4.0-preview.4.1 104 4/1/2022
1.4.0-preview.3.3 118 3/14/2022
1.4.0-preview.2.3 104 3/10/2022
1.4.0-preview.2.2 114 3/8/2022
1.4.0-preview.1.4 129 3/7/2022

Watch out! lots of breaking changes.