Telegram.Bots.Extensions.Polling 5.3.0

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

// Install Telegram.Bots.Extensions.Polling as a Cake Tool
#tool nuget:?package=Telegram.Bots.Extensions.Polling&version=5.3.0                

Telegram.Bots

A .NET 5 wrapper for the Telegram Bot API 5.5.

Status Nuget Downloads License

Usage

Configure the Bot Client
using System;
using Microsoft.Extensions.DependencyInjection;
using Telegram.Bots;

IServiceProvider provider = new ServiceCollection()
  .AddBotClient("<bot-token>")
  .Services
  .BuildServiceProvider();

IBotClient bot = provider.GetRequiredService<IBotClient>();
Or Configure the Bot Client with a Web Proxy
using System;
using Microsoft.Extensions.DependencyInjection;
using Telegram.Bots;

IServiceProvider provider = new ServiceCollection()
  .AddBotClient("<bot-token>")
  .ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler
  {
    Proxy = new WebProxy(Host: "<your-host>", Port: 1234),
    UseProxy = true
  })
  .Services
  .BuildServiceProvider();

IBotClient bot = provider.GetRequiredService<IBotClient>();
Get Bot Information
using Telegram.Bots.Requests;
using Telegram.Bots.Types;

// ...

GetMe request = new();

Response<MyBot> response = await bot.HandleAsync(request);

if (response.Ok)
{
  MyBot myBot = response.Result;

  Console.WriteLine(myBot.Id);
  Console.WriteLine(myBot.FirstName);
  Console.WriteLine(myBot.Username);
}
else
{
  Failure failure = response.Failure;

  Console.WriteLine(failure.Description);
}
Send a Text via Chat Id
using Telegram.Bots.Requests;
using Telegram.Bots.Types;

// ...

SendText request = new(chatId: 123456789, text: "Hello!");

Response<TextMessage> response = await bot.HandleAsync(request);

if (response.Ok)
{
  TextMessage message = response.Result;

  Console.WriteLine(message.Id);
  Console.WriteLine(message.Text);
  Console.WriteLine(message.Date.ToString("G"));
}
else
{
  Failure failure = response.Failure;

  Console.WriteLine(failure.Description);
}
Send a Text via Chat Username
using Telegram.Bots.Requests.Usernames;
using Telegram.Bots.Types;

// ...

SendText request = new(username: "@chat", text: "Hello!");

Response<TextMessage> response = await bot.HandleAsync(request);

if (response.Ok)
{
  TextMessage message = response.Result;

  // ...
}
else
{
  Failure failure = response.Failure;

  // ...
}

Send an Audio File via Chat Id
using Telegram.Bots.Requests;
using Telegram.Bots.Types;

// ...

await using var audioStream = System.IO.File.OpenRead("/path/to/audio.mp3");

SendAudioFile request = new(chatId: 123456789, audio: audioStream);

Response<AudioMessage> response = await bot.HandleAsync(request);

if (response.Ok)
{
  AudioMessage message = response.Result;

  Console.WriteLine(message.Id);
  Console.WriteLine(message.Audio.Id);
  Console.WriteLine(message.Audio.Title);
  Console.WriteLine(message.Audio.Performer);
  Console.WriteLine(message.Audio.Duration);
}
else
{
  Failure failure = response.Failure;

  Console.WriteLine(failure.Description);
}
Send a Cached Video via Chat Username
using Telegram.Bots.Requests.Usernames;
using Telegram.Bots.Types;

// ...

SendCachedVideo request = new(username: "@chat", video: "<file-id>")
{
  Caption = "New Caption"
};

Response<VideoMessage> response = await bot.HandleAsync(request);

if (response.Ok)
{
  VideoMessage message = response.Result;

  Console.WriteLine(message.Id);
  Console.WriteLine(message.Video.Id);
  Console.WriteLine(message.Video.Name);
  Console.WriteLine(message.Caption);
}
else
{
  Failure failure = response.Failure;

  Console.WriteLine(failure.Description);
}
Send a Media Group via Chat Id
using Telegram.Bots.Requests;
using Telegram.Bots.Types;

await using var photoStream = System.IO.File.OpenRead("path/to/photo.png");
await using var videoStream = System.IO.File.OpenRead("path/to/video.mp4");

var request = new SendMediaGroup(chatId: 123456789, new List<IGroupableMedia>
{
  new CachedPhoto("<photo-file-id>"),
  new CachedVideo("<video-file-id>"),
  new PhotoUrl(new Uri("https://example.com/photo.png")),
  new VideoUrl(new Uri("https://example.com/video.mp4")),
  new PhotoFile(photoStream),
  new VideoFile(videoStream)
})
{
  DisableNotification = true
};

Response<IReadOnlyList<MediaGroupMessage>> response = await bot.HandleAsync(request);

if (response.Ok)
{
  foreach (var mediaGroupMessage in response.Result)
  {
    switch (mediaGroupMessage)
    {
      case PhotoMessage message:
        Console.WriteLine(message.PhotoSet.Count);
        break;
      case VideoMessage message:
        Console.WriteLine(message.Video.Name);
        break;
    }
  }
}
else
{
  Failure failure = response.Failure;

  Console.WriteLine(failure.Description);
}
Download a File
using Telegram.Bots.Types;

await using var stream = System.IO.File.OpenWrite("path/to/file.extension");

Response<FileInfo> response = await bot.HandleAsync("<file-id>", stream);
Configuring the Serializer with ASP.NET Core
using Microsoft.Extensions.DependencyInjection;
using Telegram.Bots.Extensions.AspNetCore;

// ...

IServiceCollection services = ...

services.AddControllers()
        .AddBotSerializer();
Configuring Long Polling with Telegram.Bots
using Telegram.Bots;
using Telegram.Bots.Extensions.Polling;

...

IServiceCollection services = ...

services.AddBotClient("<bot-token>");
services.AddPolling<UpdateHandler>();
Configuring an Update Handler for Polled Updates
using Telegram.Bots.Extensions.Polling;
using Telegram.Bots.Requests;
using Telegram.Bots.Types;

...

public sealed class UpdateHandler : IUpdateHandler
{
  public Task HandleAsync(IBotClient bot, Update update, CancellationToken token)
  {
    return update switch
    {
      MessageUpdate u when u.Data is TextMessage message =>
        bot.HandleAsync(new SendText(message.Chat.Id, message.Text), token),

      EditedMessageUpdate u when u.Data is TextMessage message =>
        bot.HandleAsync(new SendText(message.Chat.Id, message.Text)
        {
          ReplyToMessageId = message.Id
        }, token),

      _ => Task.CompletedTask
    };
  }
}

License

Telegram.Bots is a .NET 5 wrapper for the Telegram Bot API 5.5.
Copyright © 2020-2022 Aman Agnihotri (amanagnihotri@pm.me)

Telegram.Bots is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Telegram.Bots is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Telegram.Bots. If not, see GNU Licenses.


Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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 Telegram.Bots.Extensions.Polling:

Package Downloads
LiraSharpLib

Package Description

LiraSharp

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.9.0 40,154 11/5/2022
5.8.0 1,473 11/3/2022
5.7.0 1,168 11/2/2022
5.6.0 1,483 6/12/2022
5.5.0 1,133 6/8/2022
5.4.0 1,013 6/8/2022
5.3.0 1,043 6/8/2022
5.2.0 981 6/8/2022
5.1.0 1,426 9/2/2021
5.0.0 1,603 6/27/2021
4.0.0 1,623 6/26/2021
2.1.0 1,259 3/21/2021
2.0.0 1,378 1/23/2021
1.1.1 1,835 11/6/2020