Skip to content

Flexible Telegram API wrapper for .NET.

License

Notifications You must be signed in to change notification settings

StrangerM/Telegram.Net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🤖 Telegram.NET 💬

Hey! This is new Telegram API wrapper for .NET!

Other languages:

If you want to test it you can install NuGet package:

Package manager:

Install-Package TelegramDotNet -Version 0.5.6

.NET CLI:

dotnet add package TelegramDotNet --version 0.5.6

🧰 Tech info

  • Language: C#
  • Version of language: 9.0
  • Target framework: .NET 5.0

❓ Why Telegram.NET?

Telegram.NET is a flexible API wrapper. You can create your own extensions for library without contributing. For example, you can create a command service to create command easier. Also, if you don't want to create your own extensions, you can get DenVot's extensions for library. With Telgram.NET you can use classes wich provides interfaces for Telegram API. For example, if you have a ITelegramChannel instance, you can send a message to specified channel:

await chat.SendMessageAsync("This is my first message! Yeah 👏"); //Realy easy!

✍️ Contributing ✍️

If you want to contribute, the first thing you should do is clone this repository, create some cool stuff, create unit tests for this stuff, and create pull request.

🙋 Contributors 🙋‍♂️:

🚀 Quick start 🚀

First, we should initialize a new instance of TelegramClient like this:

var client = new TelegramClient("<YOUR_SECRET_TOKEN_HERE>");

We need to make sure everything working right. Try to get your client as TelegramUser.

var me = client.Me;

Console.WriteLine(me);

/*
Output:
    User thebestbot with id 777
*/

If you want to get messages you should use method TelegramClient.Start() like this:

client.Start();

If you want stop receiving:

client.Stop();

💻 Full code:

var client = new TelegramClient("<YOUR_SECRET_TOKEN_HERE>");
var me = client.Me;

Console.WriteLine(me);
client.Start();

Console.WriteLine("Press any key to stop bot.");
Console.ReadKey();
client.Stop();

Congrats! You've created your first bot! 🥳


🤫 More interesting features 🤫

So, we started our fist bot, nice. But our bot can't do anything! Let's create simple repeater bot.

using Moq;

namespace TheBestBotEverCreated
{
    public class Programm
    {
        public static void Main(string[] args)
        {
            var client = new TelegramClient("<YOUR_SECRET_TOKEN_HERE>", new Mock<ILogger>()).Object);
            var me = client.Me;

            Console.WriteLine(me);
            client.Start();

            client.OnMessageReceived += OnMessage;

            Console.WriteLine("Press any key to stop bot.");
            Console.ReadKey();
            client.Stop();
        }

        public static async Task OnMessage(TelegramMessage message)
        {
            var chat = message.Chat;

            await chat.SendMessageAsync(message.Text);
        }
    }
}

About

Flexible Telegram API wrapper for .NET.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages