2021-12-05 16:14:06 +01:00
# (LIBRARY IS ARCHIVED AND NOT MAINTAINED)
TLSharp
2017-05-04 17:01:13 +02:00
-------------------------------
2015-09-28 04:38:19 +02:00
2021-12-05 16:14:06 +01:00
🚩🚩🚩 **WARNING** 🚩🚩🚩🚩
## LIBRARY IS NO LONGER MAINTAINED
As a workaround you can use -> [WTelegramClient ](https://github.com/wiz0u/WTelegramClient )
WTelegramClient is another C#/.NET open-source library for accessing Telegram Client API and is:
- offering up-to-date API (latest layer)
- safer (latest MTProto v2 implementation and many security checks)
- feature-complete (handling of updates, multiple-DC connections)
- easy-to-use (API calls are direct methods with fully documented parameters in VS)
- designed for .NET 5.0+, but also available for .NET Standard 2.0 (.NET Framework 4.6.1+ & .NET Core 2.0+)
-------------------------------
2021-12-02 11:19:07 +01:00
2020-04-03 13:56:12 +02:00

2016-10-29 16:31:08 +02:00
[](https://badge.fury.io/nu/TLSharp)
2015-09-28 04:38:19 +02:00
2020-09-18 10:38:21 +02:00
_Unofficial_ Telegram (http://telegram.org) client library implemented in C#.
2015-09-28 04:38:19 +02:00
2016-10-11 16:31:30 +02:00
It's a perfect fit for any developer who would like to send data directly to Telegram users or write own custom Telegram client.
2015-09-28 04:38:19 +02:00
2016-10-11 16:31:30 +02:00
:star2: If you :heart: library, please star it! :star2:
2016-02-12 09:43:10 +01:00
2017-05-04 17:01:13 +02:00
# Table of contents
2015-09-28 04:38:19 +02:00
- [How do I add this to my project? ](#how-do-i-add-this-to-my-project )
- [Starter Guide ](#starter-guide )
- [Quick configuration ](#quick-configuration )
2016-10-11 16:31:30 +02:00
- [First requests ](#first-requests )
2016-10-23 12:46:28 +02:00
- [Working with files ](#working-with-files )
2016-10-11 16:31:30 +02:00
- [Available Methods ](#available-methods )
2018-04-25 12:23:29 +02:00
- [Contributors ](#contributors )
2015-09-28 04:38:19 +02:00
- [FAQ ](#faq )
2016-02-14 12:34:28 +01:00
- [Donations ](#donations )
2018-04-25 12:23:29 +02:00
- [Support ](#support )
2015-09-28 04:38:19 +02:00
- [License ](#license )
2016-10-11 16:31:30 +02:00
# How do I add this to my project?
2015-09-28 04:38:19 +02:00
2016-10-29 16:31:08 +02:00
Install via NuGet
2016-02-04 15:05:48 +01:00
2016-10-29 16:31:08 +02:00
```
2016-10-31 07:08:38 +01:00
> Install-Package TLSharp
2016-10-29 16:31:08 +02:00
```
or build from source
2016-02-04 15:08:28 +01:00
2016-02-04 15:05:48 +01:00
1. Clone TLSharp from GitHub
2016-10-21 17:43:53 +02:00
1. Compile source with VS2015 or MonoDevelop
2016-02-04 15:08:28 +01:00
1. Add reference to ```TLSharp.Core.dll``` to your awesome project.
2015-09-28 04:38:19 +02:00
2016-10-11 16:31:30 +02:00
# Starter Guide
2015-09-28 04:38:19 +02:00
## Quick Configuration
2016-02-04 15:05:48 +01:00
Telegram API isn't that easy to start. You need to do some configuration first.
2015-09-30 04:24:32 +02:00
2016-02-04 15:05:48 +01:00
1. Create a [developer account ](https://my.telegram.org/ ) in Telegram.
2016-04-21 13:02:44 +02:00
1. Goto [API development tools ](https://my.telegram.org/apps ) and copy **API_ID** and **API_HASH** from your account. You'll need it later.
2015-09-30 04:24:32 +02:00
2016-10-11 16:31:30 +02:00
## First requests
To start work, create an instance of TelegramClient and establish connection
```csharp
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](https://core.telegram.org/api/auth))
For authentication you need to run following code
```csharp
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);
```
Full code you can see at [AuthUser test ](https://github.com/sochix/TLSharp/blob/master/TLSharp.Tests/TLSharpTests.cs#L70 )
When user is authenticated, TLSharp creates special file called _session.dat_ . In this file TLSharp store all information needed for user session. So you need to authenticate user every time the _session.dat_ file is corrupted or removed.
You can call any method on authenticated user. For example, let's send message to a friend by his phone number:
```csharp
//get available contacts
var result = await client.GetContactsAsync();
//find recipient in contacts
2018-03-19 05:27:59 +01:00
var user = result.Users
2016-10-11 16:31:30 +02:00
.Where(x => x.GetType() == typeof (TLUser))
.Cast< TLUser > ()
2018-03-19 05:27:59 +01:00
.FirstOrDefault(x => x.Phone == "< recipient_phone > ");
2016-10-11 16:31:30 +02:00
//send message
2018-03-19 05:27:59 +01:00
await client.SendMessageAsync(new TLInputPeerUser() {UserId = user.Id}, "OUR_MESSAGE");
2016-10-11 16:31:30 +02:00
```
Full code you can see at [SendMessage test ](https://github.com/sochix/TLSharp/blob/master/TLSharp.Tests/TLSharpTests.cs#L87 )
To send message to channel you could use the following code:
```csharp
//get user dialogs
2018-03-20 23:23:48 +01:00
var dialogs = (TLDialogsSlice) await client.GetUserDialogsAsync();
2016-10-11 16:31:30 +02:00
//find channel by title
2018-03-19 05:27:59 +01:00
var chat = dialogs.Chats
2016-10-11 16:31:30 +02:00
.Where(c => c.GetType() == typeof(TLChannel))
.Cast< TLChannel > ()
2018-03-19 05:27:59 +01:00
.FirstOrDefault(c => c.Title == "< channel_title > ");
2016-10-11 16:31:30 +02:00
//send message
2018-03-19 05:27:59 +01:00
await client.SendMessageAsync(new TLInputPeerChannel() { ChannelId = chat.Id, AccessHash = chat.AccessHash.Value }, "OUR_MESSAGE");
2016-10-11 16:31:30 +02:00
```
Full code you can see at [SendMessageToChannel test ](https://github.com/sochix/TLSharp/blob/master/TLSharp.Tests/TLSharpTests.cs#L107 )
2016-10-23 12:46:28 +02:00
## Working with files
Telegram separate files to two categories -> big file and small file. File is Big if its size more than 10 Mb. TLSharp tries to hide this complexity from you, thats why we provide one method to upload files **UploadFile** .
2016-10-11 16:31:30 +02:00
2016-10-23 12:46:28 +02:00
```csharp
var fileResult = await client.UploadFile("cat.jpg", new StreamReader("data/cat.jpg"));
```
TLSharp provides two wrappers for sending photo and document
```csharp
2018-03-19 05:27:59 +01:00
await client.SendUploadedPhoto(new TLInputPeerUser() { UserId = user.Id }, fileResult, "kitty");
2016-10-23 12:46:28 +02:00
await client.SendUploadedDocument(
2018-03-19 05:27:59 +01:00
new TLInputPeerUser() { UserId = user.Id },
2016-10-23 12:46:28 +02:00
fileResult,
"some zips", //caption
"application/zip", //mime-type
new TLVector< TLAbsDocumentAttribute > ()); //document attributes, such as file name
```
Full code you can see at [SendPhotoToContactTest ](https://github.com/sochix/TLSharp/blob/master/TLSharp.Tests/TLSharpTests.cs#L125 ) and [SendBigFileToContactTest ](https://github.com/sochix/TLSharp/blob/master/TLSharp.Tests/TLSharpTests.cs#L143 )
To download file you should call **GetFile** method
```csharp
await client.GetFile(
new TLInputDocumentFileLocation()
{
2018-03-19 05:27:59 +01:00
AccessHash = document.AccessHash,
Id = document.Id,
Version = document.Version
2016-10-23 12:46:28 +02:00
},
2018-03-19 05:27:59 +01:00
document.Size); //size of fileChunk you want to retrieve
2016-10-23 12:46:28 +02:00
```
Full code you can see at [DownloadFileFromContactTest ](https://github.com/sochix/TLSharp/blob/master/TLSharp.Tests/TLSharpTests.cs#L167 )
2020-01-09 08:44:08 +01:00
# Events Sample code
```csharp
2020-05-13 00:30:12 +02:00
using System;
2020-05-10 19:19:52 +02:00
using System.Threading.Tasks;
using TeleSharp.TL;
using TLSharp.Core;
using System.Linq;
using TeleSharp.TL.Messages;
namespace TLSharpPOC
{
class MainClass
{
const int APIId = 0;
const string APIHash = "???";
const string phone = "???";
public static void Main(string[] args)
{
new MainClass().MainAsync(args).Wait();
}
private async Task MainAsync(string[] args)
{
TelegramClient client = null;
client = new TelegramClient(APIId, APIHash);
// subscribe an event to receive live messages
client.Updates += ClientUpdates;
await client.ConnectAsync();
Console.WriteLine($"Authorised: {client.IsUserAuthorized()}");
TLUser user = null;
// -- If the user has already authenticated, this step will prevent account from being blocked as it
// -- reuses the data from last authorisation.
if (client.IsUserAuthorized())
user = client.Session.TLUser;
else
{
var registered = await client.IsPhoneRegisteredAsync(phone);
var hash = await client.SendCodeRequestAsync(phone);
Console.Write("Code: ");
var code = Console.ReadLine();
if (!registered)
{
Console.WriteLine($"Sign up {phone}");
user = await client.SignUpAsync(phone, hash, code, "First", "Last");
}
Console.WriteLine($"Sign in {phone}");
user = await client.MakeAuthAsync(phone, hash, code);
}
var contacts = await client.GetContactsAsync();
Console.WriteLine("Contacts:");
foreach (var contact in contacts.Users.OfType< TLUser > ())
{
var contactUser = contact as TLUser;
Console.WriteLine($"\t{contact.Id} {contact.Phone} {contact.FirstName} {contact.LastName}");
}
var dialogs = (TLDialogs) await client.GetUserDialogsAsync();
Console.WriteLine("Channels: ");
foreach (var channelObj in dialogs.Chats.OfType< TLChannel > ())
{
var channel = channelObj as TLChannel;
Console.WriteLine($"\tChat: {channel.Title}");
}
Console.WriteLine("Groups:");
TLChat chat = null;
foreach (var chatObj in dialogs.Chats.OfType< TLChat > ())
{
chat = chatObj as TLChat;
Console.WriteLine($"Chat name: {chat.Title}");
var request = new TLRequestGetFullChat() { ChatId = chat.Id };
var fullChat = await client.SendRequestAsync< TeleSharp.TL.Messages.TLChatFull > (request);
var participants = (fullChat.FullChat as TeleSharp.TL.TLChatFull).Participants as TLChatParticipants;
foreach (var p in participants.Participants)
{
if (p is TLChatParticipant chatParticipant)
{
Console.WriteLine($"\t{chatParticipant.UserId}");
}
else if (p is TLChatParticipantAdmin chatParticipantAdmin)
{
Console.WriteLine($"\t{chatParticipantAdmin.UserId}**");
}
else if (p is TLChatParticipantCreator chatParticipantCreator)
{
Console.WriteLine($"\t{chatParticipantCreator.UserId}**");
}
}
var peer = new TLInputPeerChat() { ChatId = chat.Id };
var msg = await client.GetHistoryAsync(peer, 0, 0, 0);
Console.WriteLine(msg);
if (msg is TLMessages messages)
2020-05-13 00:30:12 +02:00
{
2020-05-10 19:19:52 +02:00
foreach (var message in messages.Messages)
{
if (message is TLMessage m1)
{
Console.WriteLine($"\t\t{m1.Id} {m1.Message}");
}
else if (message is TLMessageService msgService)
{
Console.WriteLine($"\t\t{msgService.Id} {msgService.Action}");
}
}
}
2020-05-13 00:30:12 +02:00
else if (msg is TLMessagesSlice messagesSlice)
2020-05-10 19:19:52 +02:00
{
bool done = false;
int total = 0;
while (!done)
{
foreach (var m1 in messagesSlice.Messages)
{
if (m1 is TLMessage message)
{
Console.WriteLine($"\t\t{message.Id} {message.Message}");
++total;
}
else if (m1 is TLMessageService messageService)
{
Console.WriteLine($"\t\t{messageService.Id} {messageService.Action}");
++total;
done = messageService.Action is TLMessageActionChatCreate;
}
}
msg = await client.GetHistoryAsync(peer, total, 0, 0);
}
}
}
// -- Wait in a loop to handle incoming updates. No need to poll.
2020-05-13 00:30:12 +02:00
while(true)
2020-05-10 19:19:52 +02:00
{
await client.WaitEventAsync(TimeSpan.FromSeconds(1));
}
}
private void ClientUpdates(TelegramClient client, TLAbsUpdates updates)
{
Console.WriteLine($"Got update: {updates}");
if (updates is TLUpdateShort updateShort)
{
Console.WriteLine($"Short: {updateShort.Update}");
if (updateShort.Update is TLUpdateUserStatus status)
{
Console.WriteLine($"User {status.UserId} is {status.Status}");
if (status.Status is TLUserStatusOnline)
{
var peer = new TLInputPeerUser() { UserId = status.UserId };
client.SendMessageAsync(peer, "Você está online.").Wait();
}
}
}
else if (updates is TLUpdateShortMessage message)
{
Console.WriteLine($"Message: {message.Message}");
MarkMessageRead(client, new TLInputPeerUser() { UserId = message.UserId }, message.Id);
}
else if (updates is TLUpdateShortChatMessage shortChatMessage)
{
Console.WriteLine($"Chat Message: {shortChatMessage.Message}");
MarkMessageRead(client, new TLInputPeerChat() { ChatId = shortChatMessage.ChatId }, shortChatMessage.Id);
}
else if (updates is TLUpdates allUpdates)
{
foreach (var update in allUpdates.Updates)
{
Console.WriteLine($"\t{update}");
if (update is TLUpdateNewChannelMessage metaMessage)
{
var channelMsg = metaMessage.Message as TLMessage;
Console.WriteLine($"Channel message: {channelMsg.Message}");
var channel = allUpdates.Chats[0] as TLChannel;
MarkMessageRead(client,
new TLInputPeerChannel() { ChannelId = channel.Id, AccessHash = channel.AccessHash.Value },
channelMsg.Id);
}
}
foreach (var user in allUpdates.Users)
{
Console.WriteLine($"{user}");
}
foreach (var chat in allUpdates.Chats)
{
Console.WriteLine($"{chat}");
}
}
}
private void MarkMessageRead(TelegramClient client, TLAbsInputPeer peer, int id)
{
try
{
var request = new TLRequestReadHistory();
request.MaxId = id;
request.Peer = peer;
client.SendRequestAsync< bool > (request).Wait();
}
catch (InvalidOperationException e)
{
Console.WriteLine($"MarkMessageRead Error: {e.Message}");
}
}
}
}
2020-01-09 08:44:08 +01:00
```
2016-10-23 12:46:28 +02:00
# Available Methods
2016-10-11 16:31:30 +02:00
For your convenience TLSharp have wrappers for several Telegram API methods. You could add your own, see details below.
1. IsPhoneRegisteredAsync
1. SendCodeRequestAsync
1. MakeAuthAsync
1. SignUpAsync
1. GetContactsAsync
1. SendMessageAsync
1. SendTypingAsync
1. GetUserDialogsAsync
2016-10-23 12:46:28 +02:00
1. SendUploadedPhoto
1. SendUploadedDocument
1. GetFile
1. UploadFile
2016-11-07 00:40:19 +01:00
1. SendPingAsync
2017-01-09 18:25:55 +01:00
1. GetHistoryAsync
2016-10-11 16:31:30 +02:00
**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:
```csharp
//Create request
var req = new TLRequestSetTyping()
{
2018-03-19 05:27:59 +01:00
Action = new TLSendMessageTypingAction(),
Peer = new TLInputPeerUser() { UserId = user.Id }
2016-10-11 16:31:30 +02:00
};
//run request, and deserialize response to Boolean
2018-03-19 05:27:59 +01:00
return await client.SendRequestAsync< Boolean > (req);
2016-10-11 16:31:30 +02:00
```
**Where you can find a list of requests and its params?**
2020-09-18 10:38:21 +02:00
The only way is [Telegram API docs ](https://core.telegram.org/methods ). Latest scheme in JSON format you can find [here ](https://core.telegram.org/schema/json ).
2016-10-11 16:31:30 +02:00
2016-10-23 12:46:28 +02:00
## What things can I Implement (Project Roadmap)?
2016-10-13 08:20:55 +02:00
2020-09-18 10:38:21 +02:00
### Latest Release
2016-02-07 11:28:41 +01:00
2016-10-15 12:37:01 +02:00
* [DONE] Add PHONE_MIGRATE handling
2016-10-29 16:31:08 +02:00
* [DONE] Add FILE_MIGRATE handling
* [DONE] Add NuGet package
2016-10-23 12:46:28 +02:00
* [DONE] Add wrappers for media uploading and downloading
2020-09-18 10:38:21 +02:00
* Add Updates handling ([WIP](https://github.com/sochix/TLSharp/pull/940))
* Store user session as JSON ([WIP](https://github.com/nblockchain/TgSharp/pull/18))
* Upgrade MTProto protocol version to 2.0 ([WIP](https://github.com/nblockchain/TgSharp/pull/23))
* SRP/2FA support ([WIP](https://github.com/nblockchain/TgSharp/pull/17))
2015-09-28 04:38:19 +02:00
# FAQ
2016-10-11 16:41:51 +02:00
#### What API layer is supported?
2020-09-18 10:38:21 +02:00
The latest layer supported by TLSharp is 66. If you need a higher layer, help us test the preview version of [TgSharp ](https://github.com/nblockchain/TgSharp ) (your feedback is welcome!)
2016-10-11 16:41:51 +02:00
2016-10-30 09:16:55 +01:00
#### I get a xxxMigrationException or a MIGRATE_X error!
2015-09-28 04:38:19 +02:00
2016-10-30 09:16:55 +01:00
TLSharp library should automatically handle these errors. If you see such errors, please open a new Github issue with the details (include a stacktrace, etc.).
2015-09-28 04:38:19 +02:00
2016-10-02 16:31:37 +02:00
#### I get an exception: System.IO.EndOfStreamException: Unable to read beyond the end of the stream. All test methos except that AuthenticationWorks and TestConnection return same error. I did every thing including setting api id and hash, and setting server address.-
2015-09-30 04:29:29 +02:00
You should create a Telegram session. See [configuration guide ](#sending-messages-set-up )
Throw FloodException instead of calling Thread.Sleep()
Doing this is better when looking at the problem from at least
these 2 points of view:
a) You're working on TLSharp itself: You might be testing some
new things or running TLSharp's tests. Suddenly, if a FLOOD_WAIT
response happens, there's no clear way to know. You just see the
tests taking forever. But if a test has reached a situation in
which Telegram has returned a FLOOD_WAIT error, it's very likely
that what happens is that the TLSharp operation being tested is
actually buggy, which means that the test should FAIL FAST and
show a stacktrace of the problem so that you can see where in the
code was the FLOOD_WAIT received/caused. You shouldn't need to
kill the run of the test suite and hope to hit the problem again
only when you were using the debugger (to be able to pause
execution and examine a stacktrace of where the execution flow is).
b) You're using TLSharp: if you hit a FLOOD_WAIT problem it may
happen because:
b1) TLSharp has a bug: in this case it's better to throw an
exception so that the user can copy the stacktrace and paste
it into a new Github issue.
b2) Your program uses TLSharp sending excessive requests: you
want to have your program know when you hit the limit, to be
able to fix your program to not be so floody. But a call to
Thread.Sleep() doesn't help you to know this, you just know
that suddenly your program has hung, and you don't know why.
You cannot react to the problem, however with an exception you
can react to the problem (for example by examining the time
that the exception provides, as a TimeSpan property, to know
how much your program needs to wait to be able to use TLSharp
again).
2016-11-01 16:38:28 +01:00
#### Why do I get a FloodException/FLOOD_WAIT error?
2020-04-10 09:07:02 +02:00
After you get this, you cannot use Telegram's API for a while. You can know the time to wait by accessing the FloodException::TimeToWait property.
If this happens too often and/or the TimeToWait value is too long, there may be something odd going on. First and foremost, are you using TLSharp to manage more than one telegram account from the same host(server)? If yes, it's likely that you're hitting [Telegram restrictions ](https://core.telegram.org/api/errors#420-flood ). We recommend that you use TLSharp in a standalone-device app (so that each instance of your program only uses one telegram account), so for example a mobile app, not a web app.
If, on the other hand, you're completely sure that you found a bug in TLSharp about this, please open a Github issue.
2015-10-03 03:27:41 +02:00
2016-02-13 08:57:30 +01:00
#### Why does TLSharp lacks feature XXXX?
2015-09-28 04:38:19 +02:00
2018-03-02 04:35:21 +01:00
TLSharp only covers basic functionality of the Telegram protocol, you can be a contributor or a sponsor to speed-up developemnt of any more new features.
2015-09-28 04:38:19 +02:00
2018-03-02 04:35:21 +01:00
#### Where else to ask for help?
If you think you have found a bug in TLSharp, create a github issue. But if you just have questions about how to use TLSharp, use our gitter channel (https://gitter.im/TLSharp/Lobby) or our Telegram channel (https://t.me/joinchat/AgtDiBEqG1i-qPqttNFLbA).
2016-02-07 11:28:41 +01:00
2016-10-11 16:31:30 +02:00
**Attach following information**:
2016-02-07 11:28:41 +01:00
* Full problem description and exception message
* Stack-trace
* Your code that runs in to this exception
Without information listen above your issue will be closed.
2016-10-11 16:31:30 +02:00
2016-02-14 12:34:28 +01:00
# Donations
Thanks for donations! It's highly appreciated.
2017-01-29 15:06:22 +01:00
< a href = "https://www.paypal.me/IPirozhenko" title = "Support project" > < img src = "https://img.shields.io/badge/Support%20project-paypal-brightgreen.svg" > < / a >
2016-02-14 12:34:28 +01:00
List of donators:
* [mtbitcoin ](https://github.com/mtbitcoin )
2016-02-07 11:28:41 +01:00
2018-04-25 12:23:29 +02:00
# Support
If you have troubles while using TLSharp, I can help you for an additional fee.
My pricing is **219$/hour** . I accept PayPal. To request a paid support write me at Telegram @sochix , start your message with phrase [PAID SUPPORT].
2016-10-11 16:31:30 +02:00
# Contributors
* [Afshin Arani ](http://aarani.ir ) - TLGenerator, and a lot of other usefull things
2020-04-10 09:53:27 +02:00
* [knocte ](https://github.com/knocte )
2016-10-11 16:31:30 +02:00
2015-09-28 04:38:19 +02:00
# License
**Please, provide link to an author when you using library**
The MIT License
2021-12-02 11:19:07 +01:00
Copyright (c) 2015 Ilya Pirozhenko
2015-09-28 04:38:19 +02:00
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.