2020-04-02 09:54:10 +02:00
TgSharp
2017-05-04 17:01:13 +02:00
-------------------------------
2015-09-28 04:38:19 +02:00
2020-04-03 14:21:29 +02:00

2020-04-02 09:54:10 +02:00
[](https://badge.fury.io/nu/TgSharp)
2015-09-28 04:38:19 +02:00
2020-04-02 09:54:10 +02:00
_Unofficial_ Telegram (http://telegram.org) client library implemented in C#.
2018-04-25 18:02:57 +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
```
2020-04-02 09:54:10 +02:00
> Install-Package TgSharp
2016-10-29 16:31:08 +02:00
```
or build from source
2016-02-04 15:08:28 +01:00
2020-04-02 09:54:10 +02:00
1. Clone TgSharp from GitHub
2016-10-21 17:43:53 +02:00
1. Compile source with VS2015 or MonoDevelop
2020-04-02 10:37:22 +02:00
1. Add reference to ```TgSharp.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
2020-04-02 09:54:10 +02:00
2016-10-11 16:31:30 +02:00
## First requests
2020-04-02 09:54:10 +02:00
2020-04-14 00:38:32 +02:00
To start work, create an instance of TelegramClient and establish connection:
2016-10-11 16:31:30 +02:00
```csharp
2020-04-14 00:38:32 +02:00
var client = new TelegramClient(apiId, apiHash);
await client.ConnectAsync();
2016-10-11 16:31:30 +02:00
```
2020-04-14 00:38:32 +02:00
2016-10-11 16:31:30 +02:00
Now you can work with Telegram API, but ->
2020-04-14 00:38:32 +02:00
2016-10-11 16:31:30 +02:00
> Only a small portion of the API methods are available to unauthorized users. ([full description](https://core.telegram.org/api/auth))
2020-04-14 00:38:32 +02:00
For authentication you need to run following code:
2016-10-11 16:31:30 +02:00
```csharp
2020-04-14 00:38:32 +02:00
var hash = await client.SendCodeRequestAsync("< user_number > ");
var code = "< code_from_telegram > "; // you can change code in debugger
2016-10-11 16:31:30 +02:00
2020-04-14 00:38:32 +02:00
var user = await client.MakeAuthAsync("< user_number > ", hash, code);
2016-10-11 16:31:30 +02:00
```
2020-04-14 00:38:32 +02:00
You can see the full code at [AuthUser test ](https://github.com/nblockchain/TgSharp/blob/master/src/TgSharp.Tests/TgSharpTests.cs#L70 )
2016-10-11 16:31:30 +02:00
2020-04-14 00:38:32 +02:00
When the user is authenticated, TgSharp creates special file called _session.dat_ . In this file TgSharp 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.
2016-10-11 16:31:30 +02:00
2020-04-14 00:38:32 +02:00
You can call any method on authenticated users. For example, let's send message to a friend by his phone number:
2016-10-11 16:31:30 +02:00
```csharp
2020-04-14 00:38:32 +02:00
//get available contacts
var result = await client.GetContactsAsync();
//find recipient in contacts
var user = result.Users
2020-04-14 00:46:44 +02:00
.OfType< TLUser > ()
2020-04-14 00:38:32 +02:00
.FirstOrDefault(x => x.Phone == "< recipient_phone > ");
//send message
await client.SendMessageAsync(new TLInputPeerUser() { UserId = user.Id }, "OUR_MESSAGE");
2016-10-11 16:31:30 +02:00
```
2020-04-14 00:38:32 +02:00
You can see the full code at [SendMessage test ](https://github.com/nblockchain/TgSharp/blob/master/src/TgSharp.Tests/TgSharpTests.cs#L87 )
To send a message to a channel you could use the following code:
2016-10-11 16:31:30 +02:00
```csharp
2020-04-14 00:38:32 +02:00
//get user dialogs
var dialogs = (TLDialogsSlice) await client.GetUserDialogsAsync();
2016-10-11 16:31:30 +02:00
2020-04-14 00:38:32 +02:00
//find channel by title
var chat = dialogs.Chats
2020-04-14 00:46:44 +02:00
.OfType< TLChannel > ()
2020-04-14 00:38:32 +02:00
.FirstOrDefault(c => c.Title == "< channel_title > ");
2016-10-11 16:31:30 +02:00
2020-04-14 00:38:32 +02:00
//send message
await client.SendMessageAsync(new TLInputPeerChannel() { ChannelId = chat.Id, AccessHash = chat.AccessHash.Value }, "OUR_MESSAGE");
2016-10-11 16:31:30 +02:00
```
2020-04-14 00:38:32 +02:00
You can see the full code at [SendMessageToChannel test ](https://github.com/nblockchain/TgSharp/blob/master/src/TgSharp.Tests/TgSharpTests.cs#L107 )
2020-04-02 09:54:10 +02:00
2016-10-23 12:46:28 +02:00
## Working with files
2020-04-14 00:38:32 +02:00
2020-04-02 09:54:10 +02:00
Telegram separate files to two categories -> big file and small file. File is Big if its size more than 10 Mb. TgSharp 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
2020-04-14 00:38:32 +02:00
var fileResult = await client.UploadFile("cat.jpg", new StreamReader("data/cat.jpg"));
2016-10-23 12:46:28 +02:00
```
2020-04-14 00:38:32 +02:00
TgSharp provides two wrappers for sending photo and document:
2016-10-23 12:46:28 +02:00
```csharp
2020-04-14 00:38:32 +02:00
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 > ());
2016-10-23 12:46:28 +02:00
```
2020-04-14 00:38:32 +02:00
You can see the Full code at [SendPhotoToContactTest ](https://github.com/nblockchain/TgSharp/blob/master/src/TgSharp.Tests/TgSharpTests.cs#L125 ) and [SendBigFileToContactTest ](https://github.com/nblockchain/TgSharp/blob/master/src/TgSharp.Tests/TgSharpTests.cs#L143 )
To download a file you should call the **GetFile** method:
2016-10-23 12:46:28 +02:00
```csharp
2020-04-14 00:38:32 +02:00
await client.GetFile(new TLInputDocumentFileLocation()
{
AccessHash = document.AccessHash,
Id = document.Id,
2020-04-03 09:29:10 +02:00
FileReference = document.FileReference,
ThumbSize = "250x250"
2020-04-14 00:38:32 +02:00
},
//size of fileChunk you want to retrieve
document.Size);
2016-10-23 12:46:28 +02:00
```
2020-04-14 00:38:32 +02:00
You can see the Full code at [DownloadFileFromContactTest ](https://github.com/nblockchain/TgSharp/blob/master/src/TgSharp.Tests/TgSharpTests.cs#L167 )
2016-10-23 12:46:28 +02:00
2020-04-02 09:54:10 +02:00
2016-10-23 12:46:28 +02:00
# Available Methods
2016-10-11 16:31:30 +02:00
2020-04-02 09:54:10 +02:00
For your convenience TgSharp have wrappers for several Telegram API methods. You could add your own, see details below.
2016-10-11 16:31:30 +02:00
1. SendCodeRequestAsync
1. MakeAuthAsync
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
2020-04-02 09:54:10 +02:00
2016-10-11 16:31:30 +02:00
**What if you can't find needed method at the list?**
2020-04-14 00:38:32 +02:00
Don't panic. You can call any method with help of `SendRequestAsync` function. For example, send user typing method:
2016-10-11 16:31:30 +02:00
```csharp
2020-04-14 00:38:32 +02:00
//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);
2016-10-11 16:31:30 +02:00
```
2020-04-02 09:54:10 +02:00
2020-04-14 00:38:32 +02:00
**Where can you find a list of requests and its parameters?**
2016-10-11 16:31:30 +02:00
2020-04-03 09:31:29 +02:00
The only way is [Telegram API docs ](https://core.telegram.org/methods ). Yes, it's no longer outdated.
Latest scheme in JSON format you can find [here ](https://core.telegram.org/schema/json )
2016-10-11 16:31:30 +02:00
2020-04-02 09:54:10 +02:00
2020-04-03 06:29:15 +02:00
## What things can I help on (Project Roadmap)?
2015-09-28 04:38:19 +02:00
2020-04-03 06:29:15 +02:00
* Add Updates handling via events (WIP PR: https://github.com/sochix/TLSharp/pull/892 )
* GithubActions CI job for running tests
2020-04-11 10:32:38 +02:00
* Upgrade to .NETStandard 2.0.
2020-04-02 09:54:10 +02:00
2015-09-28 04:38:19 +02:00
# FAQ
2016-10-11 16:41:51 +02:00
#### What API layer is supported?
2020-04-03 06:29:15 +02:00
2020-04-03 09:29:10 +02:00
Layer 108. Thanks to Afshin Arani for his TLGenerator
2016-10-11 16:41:51 +02:00
2020-04-02 09:54:10 +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
2020-04-02 09:54:10 +02:00
TgSharp 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
2020-04-03 06:29:15 +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 )
2020-04-02 09:54:10 +02:00
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-03 06:29:15 +02:00
2020-04-11 19:57:27 +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 TgSharp to manage more than one telegram account from the same host? If yes, it's likely that you're hitting [Telegram restrictions ](https://core.telegram.org/api/errors#420-flood ). We recommend that you use TgSharp 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 TgSharp about this, please open a Github issue.
2015-10-03 03:27:41 +02:00
2015-09-28 04:38:19 +02:00
2020-04-02 09:54:10 +02:00
#### Why does TgSharp lacks feature XXXX?
TgSharp 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?
2020-04-02 09:54:10 +02:00
If you just have questions about how to use TgSharp, use our Telegram channel (https://t.me/joinchat/AgtDiBEqG1i-qPqttNFLbA) first. Don't create a github issue until you have confirmed with the maintainer and/or contributors if what you're experiencing is really a bug. When this has been confirmed, then:
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
2020-04-02 09:54:10 +02:00
* Steps to reproduce the issue (including your code)
* Expected outcome.
* Current outcome: if it's a crash, attach the full exception details (to get this, just paste what you get from `ex.ToString()` , which gives you exception type, exception message, stacktrace, and inner exceptions recursively).
2020-04-03 06:29:15 +02:00
Without information listed above your issue will be closed.
2016-02-07 11:28:41 +01:00
2016-10-11 16:31:30 +02:00
2016-02-14 12:34:28 +01:00
# Donations
2020-04-02 09:54:10 +02:00
Please send your donation to this ETH address (owned by Afshin Arani): 0xbfd1b684e0DdA5C219e11315682a9722b3194131
2016-02-07 11:28:41 +01:00
2018-04-25 12:23:29 +02:00
# Support
2020-04-02 09:54:10 +02:00
If you have troubles while using TgSharp, we may be able to help you; access our telegram channel first, and ask for "Paid support".
2018-04-25 12:23:29 +02:00
2016-10-11 16:31:30 +02:00
# Contributors
2020-04-02 09:54:10 +02:00
* [knocte ](https://github.com/knocte ) - Maintainer
* [Afshin Arani ](http://aarani.ir ) - Main contributor
* [CheshireCaat ](http://github.com/CheshireCaat ) - Occasional contributor
* [sochix ](https://github.com/sochix ) - Original author
2016-10-11 16:31:30 +02:00
2015-09-28 04:38:19 +02:00
# License
2020-04-03 06:29:15 +02:00
**Please, provide link to the original authors when using library**
2015-09-28 04:38:19 +02:00
The MIT License
2020-04-03 06:29:15 +02:00
2016-10-11 16:31:30 +02:00
Copyright (c) 2015 Ilya Pirozhenko http://www.sochix.ru/
2020-04-03 06:29:15 +02:00
2020-04-02 09:54:10 +02:00
Copyright (c) 2015-2020 TLSharp/TgSharp contributors
2015-09-28 04:38:19 +02:00
2020-04-03 06:29:15 +02:00
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.