TLSharp/README.md

260 lines
11 KiB
Markdown
Raw Normal View History

2015-09-28 04:38:19 +02:00
#TLSharp
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-10-02 18:13:02 +02:00
[![Join the chat at https://gitter.im/TLSharp/Lobby](https://badges.gitter.im/TLSharp/Lobby.svg)](https://gitter.im/TLSharp/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
2016-02-10 11:58:46 +01:00
[![Build status](https://ci.appveyor.com/api/projects/status/95rl618ch5c4h2fa?svg=true)](https://ci.appveyor.com/project/sochix/tlsharp)
2016-10-29 16:31:08 +02:00
[![NuGet version](https://badge.fury.io/nu/TLSharp.svg)](https://badge.fury.io/nu/TLSharp)
2017-01-29 15:06:22 +01:00
<a href="https://github.com/sochix/telegram-tools"><img src=https://img.shields.io/badge/Telegram%20Tools-1.0.0-blue.svg /></a>
2015-09-28 04:38:19 +02:00
2016-10-11 16:31:30 +02:00
_Unofficial_ Telegram (http://telegram.org) client library implemented in C#. Latest TL scheme supported, thanks to Afshin Arani
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-02-28 14:45:20 +01:00
If you have difficulties with library usage, I can support you ( 75$/hour ). Contact me at @sochix.
2017-01-29 15:06:22 +01:00
2017-02-28 14:45:20 +01:00
If you have difficulties with console or writing code, you can try [Telegram Tools](https://github.com/sochix/telegram-tools). It's a GUI for TLSharp.
2017-01-29 15:06:22 +01:00
2016-10-11 16:31:30 +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)
- [Dependencies](#dependencies)
- [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)
2015-09-28 04:38:19 +02:00
- [Contributing](#contributing)
- [FAQ](#faq)
2016-02-14 12:34:28 +01:00
- [Donations](#donations)
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
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
# Dependencies
2015-09-28 04:38:19 +02:00
2015-10-12 06:18:36 +02:00
TLSharp has a few dependenices, most of functionality implemented from scratch.
2015-09-28 04:38:19 +02:00
All dependencies listed in [package.conf file](https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/packages.config).
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
var user = result.users.lists
.Where(x => x.GetType() == typeof (TLUser))
.Cast<TLUser>()
.FirstOrDefault(x => x.phone == "<recipient_phone>");
//send message
await client.SendMessageAsync(new TLInputPeerUser() {user_id = user.id}, "OUR_MESSAGE");
```
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
var dialogs = await client.GetUserDialogsAsync();
//find channel by title
var chat = dialogs.chats.lists
.Where(c => c.GetType() == typeof(TLChannel))
.Cast<TLChannel>()
.FirstOrDefault(c => c.title == "<channel_title>");
//send message
await client.SendMessageAsync(new TLInputPeerChannel() { channel_id = chat.id, access_hash = chat.access_hash.Value }, "OUR_MESSAGE");
```
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
await client.SendUploadedPhoto(new TLInputPeerUser() { user_id = user.id }, fileResult, "kitty");
await client.SendUploadedDocument(
new TLInputPeerUser() { user_id = user.id },
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()
{
access_hash = document.access_hash,
id = document.id,
version = document.version
},
document.size); //size of fileChunk you want to retrieve
```
Full code you can see at [DownloadFileFromContactTest](https://github.com/sochix/TLSharp/blob/master/TLSharp.Tests/TLSharpTests.cs#L167)
# 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
1. SendPingAsync
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()
{
action = new TLSendMessageTypingAction(),
peer = peer
};
//run request, and deserialize response to Boolean
return await SendRequestAsync<Boolean>(req);
```
**Where you can find a list of requests and its params?**
The only way is [Telegram API docs](https://core.telegram.org/methods). Yes, it's outdated. But there is no other source.
2016-10-11 17:03:12 +02:00
Latest scheme in JSON format you can find [here](https://gist.github.com/aarani/b22b7cda024973dff68e1672794b0298)
2016-10-11 16:31:30 +02:00
2016-10-23 12:46:28 +02:00
# Contributing
2015-09-28 04:38:19 +02:00
2017-01-29 15:06:22 +01:00
Contributing is highly appreciated! Donations required <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-04 15:05:48 +01:00
2016-10-23 12:46:28 +02:00
## What things can I Implement (Project Roadmap)?
2016-10-13 08:20:55 +02:00
2016-10-23 12:46:28 +02:00
### Release 1.0.0
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
2016-02-07 11:28:41 +01:00
* Add Updates handling
2016-10-29 16:31:08 +02:00
* [DONE] Add NuGet package
2016-10-23 12:46:28 +02:00
* [DONE] Add wrappers for media uploading and downloading
2016-10-11 16:31:30 +02:00
* Store user session as JSON
2015-09-28 04:38:19 +02:00
# FAQ
2016-10-11 16:41:51 +02:00
#### What API layer is supported?
2016-10-15 12:37:01 +02:00
The latest one - 57. Thanks to Afshin Arani for his TLGenerator
2016-10-11 16:41:51 +02:00
#### I get a xxxMigrationException or a MIGRATE_X error!
2015-09-28 04:38:19 +02: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?
It's likely [Telegram restrictions](https://core.telegram.org/api/errors#420-flood), or a bug in TLSharp (if you feel it's the latter, please open a Github issue). You can know the time to wait by accessing the FloodException::TimeToWait property.
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
Now TLSharp is basic realization of Telegram protocol, you can be a contributor or a sponsor to speed-up developemnt of any feature.
2016-02-07 11:28:41 +01:00
#### Nothing helps
2016-10-11 16:31:30 +02:00
Ask your question at gitter or create an issue in project bug tracker.
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
2016-10-11 16:31:30 +02:00
# Contributors
* [Afshin Arani](http://aarani.ir) - TLGenerator, and a lot of other usefull things
2016-10-23 09:59:20 +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
2016-10-11 16:31:30 +02:00
Copyright (c) 2015 Ilya Pirozhenko http://www.sochix.ru/
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.