Grumson.Utilities.TelegramBot 1.0.1

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

// Install Grumson.Utilities.TelegramBot as a Cake Tool
#tool nuget:?package=Grumson.Utilities.TelegramBot&version=1.0.1                

TelegramBot Documentation

The TelegramBot is a C# library designed for my personal needs. It provides a service for interacting with the Telegram bot API. The library allows for bot initialization, sending messages, registering and unregistering events, and handling errors and received messages.

Installation

TelegramService is a class that implements a service for interacting with the Telegram bot API. The class allows for bot initialization, sending messages, registering and unregistering events, and handling errors and received messages.

Properties

  • _OnMessage: Event triggered upon receiving a message.
  • _OnError: Event triggered upon an error.
  • _ChatList: List of chats the bot has communicated with.
  • _botClient: Instance of TelegramBotClient that enables communication with the Telegram API.
  • _cts: CancellationTokenSource for managing operation cancellations.
  • _token: Token for bot authentication.

Constructor

  • TelegramService(string token): Initializes a new instance of TelegramService with the given token.

Public Methods

  • RegisterOnMessageRecivedEvent(Action<Message, UpdateType> onMessage): Registers an event for receiving messages.
  • UnregisterFromMessageRecivedEvent(Action<Message, UpdateType> onMessage): Unregisters an event for receiving messages.
  • RegisterOnErrorEvent(Action<Exception, HandleErrorSource> onError): Registers an event for handling errors.
  • UnregisterFromErrorEvent(Action<Exception, HandleErrorSource> onError): Unregisters an event for handling errors.
  • Init(): Initializes the bot and registers events for messages, errors, and updates.
  • GetMyInfo(): Retrieves information about the bot.
  • SendMessage(Chat chat, string message): Sends a message to a specified chat.
  • SendMessage(long chatId, string message): Sends a message to a specified chat by ID.

Private Methods

  • OnError(Exception exception, HandleErrorSource source): Handles errors and triggers the _OnError event.
  • OnMessage(Message msg, UpdateType type): Handles received messages and triggers the _OnMessage event.
  • OnUpdate(Update update): Handles other types of updates, such as callback queries.

Usage Example


using System;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;

public class Program
{
    public static async Task Main(string[] args)
    {
        string token = "YOUR_TELEGRAM_BOT_TOKEN";
        TelegramService telegramService = new TelegramService(token);

        // Registering events
        telegramService.RegisterOnMessageRecivedEvent(OnMessageReceived);
        telegramService.RegisterOnErrorEvent(OnError);

        // Initialization of the bot
        var initResult = await telegramService.Init();
        Console.WriteLine(initResult.message);

        // Getting bot information
        var myInfo = await telegramService.GetMyInfo();
        Console.WriteLine(myInfo.message);

        // Sending a message
        long chatId = 123456789; // Chat ID with your bot
        var sendMessageResult = await telegramService.SendMessage(chatId, "Hello, Telegram!");
        Console.WriteLine(sendMessageResult.message);
    }

    private static void OnMessageReceived(Message message, UpdateType updateType)
    {
        Console.WriteLine($"Received message: {message.Text}");
    }

    private static void OnError(Exception exception, HandleErrorSource source)
    {
        Console.WriteLine($"Error: {exception.Message}");
    }
}

In this example, we create an instance of TelegramService, register events for receiving messages and errors, initialize the bot, retrieve bot information, and send a message to a specified chat.

Changelog

Version 1.0.1 - 2023-08-10

  • Edited the README.md file

Version 1.0.0 - 2023-08-10

  • Initial release of the TelegramService class.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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

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
1.0.3 73 11/19/2024
1.0.2 119 8/10/2024
1.0.1 113 8/10/2024
1.0.0 113 8/10/2024