Skip to content

ed555009/telegram-bot-api-client

Repository files navigation

Telegram.Bot.Api.Client

GitHub Build Status Nuget

Coverage Quality Gate Status Reliability Rating Security Rating Vulnerabilities

Description

This is a .NET6 library for interacting with Telegram Bot Api.

This library is not finished and yet very limited

Quick start

Installation

dotnet add package Telegram.Bot.Api.Client

Appsettings.json

{
	"Telegram": {
		"Bot": {
			"BaseUrl": "https://api.telegram.org",
			"Token": "YOUR_BOT_TOKEN"
		}
	}
}

Add services

using Telegram.Bot.Api.Client.Extensions;

ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
	// this injects as SINGLETON
	services.AddTelegramBotServices(configuration);

	// you can also inject as SCOPED or TRANSIENT by specifying the ServiceLifetime
	services.AddTelegramBotServices(configuration, ServiceLifetime.Scoped);
}

Using services

using Telegram.Bot.Api.Client.Interfaces;
using Telegram.Bot.Api.Client.Services;

public class MyProcess
{
	private readonly ITelegramBotService _telegramBotService;

	public MyProcess(ITelegramBotService telegramBotService) =>
		_telegramBotService = telegramBotService;

	public async Task SendMessageAsync()
	{
		var response = await _telegramBotService.SendMessageAsync(
			new SendMessageModel
			{
				ChatId = "123456789",
				Text = "Hello World!"
			}
		);

		// or pass in another token so you can manipulate multiple bots at the same time
		var response = await _telegramBotService.SendMessageAsync(
			new SendMessageModel
			{
				ChatId = "123456789",
				Text = "Hello World!"
			},
			"ANOTHER_BOT_TOKEN"
		);

		// you should always check if the request was successful
		if (!res.IsSuccessStatusCode || !res.Content.Ok)
			// failed
			return;

		// success, do something
		...
	}
}

Reference

About

A nuget package for Telegram Bot Api client using .NET6

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages