13

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?

Loofer
  • 6,841
  • 9
  • 61
  • 102
Piter Griffin
  • 179
  • 1
  • 1
  • 3
  • what is your problem? – Milad Mar 31 '15 at 08:30
  • 1
    First message after one year member ? Well welcome to SO. If you want some help, you have to help yourself first. [What have you tried](http://mattgemmell.com/what-have-you-tried/) so far ? – aloisdg Mar 31 '15 at 08:32

6 Answers6

13

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

Ilia P
  • 616
  • 5
  • 16
  • I got API hash by registering here(https://my.telegram.org/auth) .. however executing above code failed stating your hash is not registered. – Vishwaram Sankaran Jul 10 '15 at 15:54
  • How about receiving messages over that line? Is it possible too? – Saeed Neamati Sep 29 '15 at 05:25
  • Yep, it'll be possible if we found a contributor or someone donates for this feature – Ilia P Sep 30 '15 at 07:22
  • dear @SochiX i am using your library but it is so hard to understand and use because of documentation weekness – Navid_pdp11 Nov 11 '15 at 13:25
  • 1
    It's open-source so you're welcome to contribute for documentation improvement! – Ilia P Nov 12 '15 at 02:30
  • hey SochiX I may be interested in contributing to your library; btw I'm wondering if it uses simple PUSH mode? see http://stackoverflow.com/questions/29542714/how-do-simple-push-events-work-in-telegram – knocte May 06 '16 at 03:26
  • Dear @SochiX i am using your library but it doesn't work, await client.ConnectAsync(); is success but i can't get contacts with this method GetContactsAsync() – M.Mohammadi May 05 '17 at 23:39
  • 1
    This library is abandoned – Victor Dec 29 '22 at 04:38
3

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");

Wizou
  • 1,336
  • 13
  • 24
2

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...");
    }
}
Alex Erygin
  • 3,161
  • 1
  • 22
  • 22
0

The simplest way is to send http request directly to the Telegram BOT API as url string, you may test those url strings even in your browser, please see details in my another answer here: https://stackoverflow.com/a/57341990/11687179

VVP
  • 349
  • 2
  • 6
0

at the first step you have to generate a bot in botfather then use the code in bellow in C#

    private void SendMessage(string msg)
    {
        string url = "https://api.telegram.org/{botid}:{botkey}/sendMessage?chat_id={@ChanalName}&text={0}";
        WebClient Client = new WebClient();
        /// If you need to use proxy 
        if (Program.UseProxy)
        {
            /// proxy elements are variable in Program.cs
            Client.Proxy = new WebProxy(Program.ProxyUrl, Program.ProxyPort);
            Client.Proxy.Credentials = new NetworkCredential("hjolany", "klojorasic");
        }
        Client.DownloadString(string.Format(url, msg));
        }));
    }
Hamid Jolany
  • 800
  • 7
  • 11
-1

Telegram has an official API that can do exactly what you need, you will have to look into http requests though..

Here is the documentation on sending a message:

Function

messages.sendMessage

Params

peer    InputPeer   User or chat where a message will be sent
message string  Message text
random_id   long    Unique client message ID required to prevent message resending

Query example

(messages.sendMessage (inputPeerSelf) "Hello, me!" 12345678901)

Return errors

Code    Type    Description
400 BAD_REQUEST PEER_ID_INVALID Invalid peer
400 BAD_REQUEST MESSAGE_EMPTY   Empty or invalid UTF8 message was sent
400 BAD_REQUEST MESSAGE_TOO_LONG    Message was too long.

Current maximum length is 4096 UTF8 characters

For the full documentation go here.

Maarten Peels
  • 1,002
  • 1
  • 13
  • 29
  • I see official API, but i can't understand how to use that. Some solutions have quick start with authorization and etc. – Piter Griffin Mar 31 '15 at 08:45
  • [this](https://core.telegram.org/api/auth) is a pretty good explanation on how to authenticate though, if you know http requests with c# this wouldn't be really hard to implement. – Maarten Peels Mar 31 '15 at 08:58
  • I agree, but some solutions has implemented protocol on async request and support encryption. – Piter Griffin Mar 31 '15 at 09:05
  • it's ok how we can send the query to DC and wait for replay i read to many about serlizing and TL languae and payload but i didn't succed to send query to send authcode – Khalid Omar Apr 12 '15 at 15:46
  • @KhalidOmar how far have you been able to go? Have you gone beyond the AuthKey part? I wish there is better documentations, it makes it harder than it actually is – Charles Okwuagwu Sep 29 '15 at 06:56
  • @CharlesO I decide to go with telegram-bot it's easy to use and have a good lib at GitHub https://telegram.org/blog/bot-revolution https://github.com/MrRoundRobin/telegram.bot – Khalid Omar Sep 29 '15 at 09:18
  • @KhalidOmar if it gives you what you need. that's fine. I have not looked at the bot in detail though. – Charles Okwuagwu Sep 29 '15 at 09:40