Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram C# example send message

Tags:

c#

.net

telegram

I can't find an example of sending message by telegram protocol from C#. I tried to use this but failed. Can you give me any examples?

like image 545
Piter Griffin Avatar asked Mar 31 '15 08:03

Piter Griffin


3 Answers

You can use the WTelegramClient library to connect to Telegram Client API protocol (as a user, not a bot)

The library is very complete but also very easy to use. Follow the README on GitHub for an easy introduction.

To send a message to someone can be as simple as:

using TL;

using var client = new WTelegram.Client(); // or Client(Environment.GetEnvironmentVariable)
await client.LoginUserIfNeeded();
var result = await client.Contacts_ResolveUsername("USERNAME");
await client.SendMessageAsync(result.User, "Hello");

//or by phone number:
//var result = await client.Contacts_ImportContacts(new[] { new InputPhoneContact { phone = "+PHONENUMBER" } });
//client.SendMessageAsync(result.users[result.imported[0].user_id], "Hello");

like image 167
Wizou Avatar answered Oct 24 '22 14:10

Wizou


For my bot I use Telegram.Bot nuget package. Full sample code is here.

Here is example of sending message in reply to incoming message.

// create bot instance
var bot = new TelegramBotClient("YourApiToken");

// test your api configured correctly
var me = await bot.GetMeAsync();
Console.WriteLine($"{me.Username} started");

// start listening for incoming messages
while (true) 
{
    //get incoming messages
    var updates = await bot.GetUpdatesAsync(offset);
    foreach (var update in updates)
    {
        // send response to incoming message
        await bot.SendTextMessageAsync(message.Chat.Id,"The Matrix has you...");
    }
}
like image 21
Alex Erygin Avatar answered Oct 24 '22 15:10

Alex Erygin


TLSharp is basic implementation of Telegram API on C#. See it here https://github.com/sochix/TLSharp

like image 26
Ilia P Avatar answered Oct 24 '22 16:10

Ilia P