All Projects → TelegramBots → Telegram.Bot.Extensions.Polling

TelegramBots / Telegram.Bot.Extensions.Polling

Licence: MIT license
Provides ITelegramBotClient extensions for polling updates

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to Telegram.Bot.Extensions.Polling

pong
Basic uptime monitoring system, with email alerts and/or push notifications
Stars: ✭ 94 (+168.57%)
Mutual labels:  telegram-bot, telegram-bot-api
tdlight-telegram-bot-api
The TDLight Telegram Bot API is an actively enhanced fork of the original Bot API, featuring experimental user support, proxies, unlimited files size, and more.
Stars: ✭ 71 (+102.86%)
Mutual labels:  telegram-bot, telegram-bot-api
Bybit-Auto-Trading-Bot-Ordes-placed-via-TradingView-Webhook
Python based Trading Bot that uses TradingView.com webhook JSON alerts to place orders(buy/sell/close/manage positions/TP/SL/TS etc.) on Bybit.com. Hire me directly here https://www.freelancer.com/u/Beannsofts for any assistance
Stars: ✭ 235 (+571.43%)
Mutual labels:  telegram-bot, telegram-bot-api
Python Telegram
Python client for the Telegram's tdlib
Stars: ✭ 246 (+602.86%)
Mutual labels:  telegram-bot, telegram-bot-api
telegram-stepfunctions-bot
Serverless Telegram bot made on 4 AWS Lambda chained by AWS Step Functions. All of this written on Serverless Framework using plugins.
Stars: ✭ 26 (-25.71%)
Mutual labels:  telegram-bot, telegram-bot-api
Telegrammer
Telegram Bot - written with Swift 5.2 / NIO, supports Linux, macOS
Stars: ✭ 248 (+608.57%)
Mutual labels:  telegram-bot, telegram-bot-api
File-Sharing-Bot
Telegram Bot to store Posts and Documents and it can Access by Special Links.
Stars: ✭ 867 (+2377.14%)
Mutual labels:  telegram-bot, telegram-bot-api
Telegram Bot Sdk
🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
Stars: ✭ 2,212 (+6220%)
Mutual labels:  telegram-bot, telegram-bot-api
grouphelperbot
A Telegram Bot made to help group admins, with Italian/English support.
Stars: ✭ 26 (-25.71%)
Mutual labels:  telegram-bot, telegram-bot-api
fp-telegram
Wrapper classes library for telegram bots API (FreePascal)
Stars: ✭ 59 (+68.57%)
Mutual labels:  telegram-bot, telegram-bot-api
Rastreiobot
Telegram Bot @RastreioBot
Stars: ✭ 196 (+460%)
Mutual labels:  telegram-bot, telegram-bot-api
telegram-bot-api-worker
Take an alternate route to Telegram Bot API :)
Stars: ✭ 75 (+114.29%)
Mutual labels:  telegram-bot, telegram-bot-api
Haskell Telegram Api
Telegram Bot API for Haskell
Stars: ✭ 184 (+425.71%)
Mutual labels:  telegram-bot, telegram-bot-api
RPi-TELEBOT
Python based Telegram bot to monitor and control the raspberry pi
Stars: ✭ 19 (-45.71%)
Mutual labels:  telegram-bot, telegram-bot-api
Java Telegram Bot Tutorial
Java Telegram Bot Tutorial. Feel free to submit issue if you found a mistake.
Stars: ✭ 165 (+371.43%)
Mutual labels:  telegram-bot, telegram-bot-api
tmdb bot
IMDB Telegram bot clone using TMDB to get info about movies and TV shows.
Stars: ✭ 22 (-37.14%)
Mutual labels:  telegram-bot, telegram-bot-api
Telegram.bot
.NET Client for Telegram Bot API
Stars: ✭ 1,964 (+5511.43%)
Mutual labels:  telegram-bot, telegram-bot-api
Teledart
A Dart library interfacing with the latest Telegram Bot API.
Stars: ✭ 142 (+305.71%)
Mutual labels:  telegram-bot, telegram-bot-api
Truecaller-Smsbomber telegram bot
Telegram bot which has truecaller and smsbomber features
Stars: ✭ 30 (-14.29%)
Mutual labels:  telegram-bot, telegram-bot-api
telegram
📚 Golang bindings for Telegram API
Stars: ✭ 15 (-57.14%)
Mutual labels:  telegram-bot, telegram-bot-api

Telegram.Bot.Extensions.Polling NuGet ci

Gitpod Ready-to-Code

Provides ITelegramBotClient extensions for polling updates.

Usage

using System;
using System.Threading;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Types;
using Telegram.Bot.Extensions.Polling;

async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
    if (update.Message is Message message)
    {
        await botClient.SendTextMessageAsync(message.Chat, "Hello");
    }
}

async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
    if (exception is ApiRequestException apiRequestException)
    {
        await botClient.SendTextMessageAsync(123, apiRequestException.ToString());
    }
}

ITelegramBotClient bot = new TelegramBotClient("<token>");

You have two ways of starting to receive updates

  1. StartReceiving does not block the caller thread. Receiving is done on the ThreadPool.

    using System.Threading;
    using Telegram.Bot.Extensions.Polling;
    
    var cts = new CancellationTokenSource();
    var cancellationToken = cts.Token;
    var receiverOptions = new ReceiverOptions
    {
        AllowedUpdates = {} // receive all update types
    };
    bot.StartReceiving(
        HandleUpdateAsync,
        HandleErrorAsync,
        receiverOptions,
        cancellationToken
    );
  2. Awaiting ReceiveAsync will block until cancellation in triggered (both methods accept a CancellationToken)

    using System.Threading;
    using Telegram.Bot.Extensions.Polling;
    
    var cts = new CancellationTokenSource();
    var cancellationToken = cts.Token;
    
    var receiverOptions = new ReceiverOptions
    {
        AllowedUpdates = {} // receive all update types
    };
    
    try
    {
        await bot.ReceiveAsync(
            HandleUpdateAsync,
            HandleErrorAsync,
            receiverOptions,
            cancellationToken
        );
    }
    catch (OperationCancelledException exception)
    {
    }

Trigger cancellation by calling cts.Cancel() somewhere to stop receiving update in both methods.


In case you want to throw out all pending updates on start there is an option ReceiveOptions.ThrowPendingUpdates. If set to true ReceiveOptions.Offset property will be ignored even if it's set to non-null value and all implemented update receivers will attempt to throw out all pending updates before starting to call your handlers. In that case ReceiveOptions.AllowedUpdates property should be set to desired values otherwise it will be effectively set to allow all updates.

Example

using System.Threading;
using Telegram.Bot.Extensions.Polling;
using Telegram.Bot.Types.Enums;

var cts = new CancellationTokenSource();
var cancellationToken = cts.Token;

var receiverOptions = new ReceiverOptions
{
    AllowedUpdates = new { UpdateType.Message, UpdateType.CallbackQuery }
    ThrowPendingUpdates = true
};

try
{
    await bot.ReceiveAsync(
        HandleUpdateAsync,
        HandleErrorAsync,
        receiverOptions,
        cancellationToken
   );
}
catch (OperationCancelledException exception)
{
}

Update streams

With .Net Core 3.1+ comes support for an IAsyncEnumerable<Update> to stream Updates as they are received.

There are two implementations:

  • BlockingUpdateReceiver blocks execution on every new getUpdates request
  • QueuedUpdateReceiver enqueues updates in an internal queue in a background process to make Update interation faster so you don't have to wait on getUpdates requests to finish

Example:

using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Extensions.Polling;

var bot = new TelegramBotClient("<token>");
var receiverOptions = new ReceiverOptions
{
    AllowedUpdates = {} // receive all update types
};
var updateReceiver = new QueuedUpdateReceiver(bot, receiverOptions);

// to cancel
var cts = new CancellationTokenSource();

try
{
    await foreach (Update update in updateReceiver.WithCancellation(cts.Token))
   {
       if (update.Message is Message message)
       {
           await bot.SendTextMessageAsync(
               message.Chat,
               $"Still have to process {updateReceiver.PendingUpdates} updates"
           );
       }
   }
}
catch (OperationCanceledException exception)
{
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].