A simple Unofficial Telegram (http://telegram.org) MTProto client implemented in C#. The Api client library that implements the MtProto 2.0 protocol is supported by .Net >= 6
If you want more information about Telegram API, you can go here. And here you will find extra information about mtproto.
You need to do some configuration first.
- Create a developer account in Telegram.
- Goto API development tools and copy API_ID and API_HASH from your account. You'll need it later.
To start work, create an instance of TelegramClient and establish connection:
var client = new TelegramClient(apiId, apiHash);
await client.ConnectAsync();
Now you can work with Telegram API, but ->
Only a small portion of the API methods are available to unauthorized users. (full description)
For authentication you need to run following code:
var hash = await client.SendCodeRequestAsync("<user_number>");
var code = "<code_from_telegram>"; // you can change code in debugger
var user = await client.MakeAuthAsync("<user_number>", hash, code);
When the user is authenticated, TeleNet creates special file called session.dat. In this file TeleNet stores all information needed for the user's session. So you need to authenticate user every time the session.dat file is corrupted or removed.
You can call any method on authenticated users. For example, let's send message to a friend by his phone number:
//get available contacts
var result = await client.GetContactsAsync();
//find recipient in contacts
var user = result.Users
.OfType<TLUser>()
.FirstOrDefault(x => x.Phone == "<recipient_phone>");
//send message
await client.SendMessageAsync(new TLInputPeerUser() { UserId = user.Id }, "OUR_MESSAGE");
To send a message to a channel you could use the following code:
//get user dialogs
var dialogs = (TLDialogsSlice) await client.GetUserDialogsAsync();
//find channel by title
var chat = dialogs.Chats
.OfType<TLChannel>()
.FirstOrDefault(c => c.Title == "<channel_title>");
//send message
await client.SendMessageAsync(new TLInputPeerChannel() { ChannelId = chat.Id, AccessHash = chat.AccessHash.Value }, "OUR_MESSAGE");
Telegram separate files to two categories -> big file and small file. File is Big if its size more than 10 Mb. TeleNet tries to hide this complexity from you, thats why we provide one method to upload files UploadFile.
var fileResult = await client.UploadFile("cat.jpg", new StreamReader("data/cat.jpg"));
TeleNet provides two wrappers for sending photo and document:
await client.SendUploadedPhoto(new TLInputPeerUser() { UserId = user.Id }, fileResult, "kitty");
await client.SendUploadedDocument(new TLInputPeerUser() { UserId = user.Id },
fileResult,
"application/zip", //mime-type
//document attributes, such as file name
new TLVector<TLAbsDocumentAttribute>());
To download a file you should call the GetFile method:
await client.GetFile(new TLInputDocumentFileLocation()
{
AccessHash = document.AccessHash,
Id = document.Id,
FileReference = document.FileReference,
ThumbSize = "250x250"
},
//size of fileChunk you want to retrieve
document.Size);
For your convenience TeleNet have wrappers for several Telegram API methods. You could add your own, see details below.
- SendCodeRequestAsync
- MakeAuthAsync
- GetContactsAsync
- SendMessageAsync
- SendTypingAsync
- GetUserDialogsAsync
- SendUploadedPhoto
- SendUploadedDocument
- GetFile
- UploadFile
- SendPingAsync
- GetHistoryAsync
- UpdateStatusAsync
- SearchUserAsync
- SignUpAsync
What if you can't find needed method at the list?
Don't panic. You can call any method with help of SendRequestAsync
function. For example, send user typing method:
//Create request
var req = new TLRequestSetTyping()
{
Action = new TLSendMessageTypingAction(),
Peer = new TLInputPeerUser() { UserId = user.Id }
};
//run request, and deserialize response to Boolean
return await client.SendRequestAsync<Boolean>(req);
Where can you find a list of requests and its parameters?
The only way is Telegram API docs. Yes, it's no longer outdated. Latest scheme in JSON format you can find here
- sochix - Original TLSharp author
- Afshin Arani - TLGenerator, and a lot of other usefull things
Please, provide link to the original authors when using library
The project is released under the Mit License
Copyright (c) 2015 Ilya Pirozhenko (sochix)
Copyright (c) 2015-2020 TLSharp/TgSharp contributors