2015-09-28 04:38:19 +02:00
#TLSharp
2016-02-10 11:58:46 +01:00
[](https://ci.appveyor.com/project/sochix/tlsharp)
2015-09-28 04:38:19 +02:00
2015-10-12 06:18:36 +02:00
Telegram (http://telegram.org) client library implemented in C#. Only basic functionality is currently implemented. **Consider donation to speed up development process.**
2015-09-28 04:38:19 +02:00
2015-10-12 06:18:36 +02:00
It's a perfect fit for any developer who would like to send data directly to Telegram users.
2015-09-28 04:38:19 +02:00
2016-02-04 15:05:48 +01:00
:star: If you :heart: library, please star it! :star:
2016-01-27 12:07:51 +01:00
:exclamation: **Please, don't use it for SPAM!**
2015-09-28 04:38:19 +02:00
2016-02-12 09:43:10 +01:00
:ru: Russian description you can find [here ](https://habrahabr.ru/post/277079/ )
2016-02-04 15:05:48 +01: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 )
- [Using TLSharp ](#using-tlsharp )
- [Contributing ](#contributing )
- [FAQ ](#faq )
- [License ](#license )
#How do I add this to my project?
2016-02-04 15:05:48 +01:00
Library isn't ready for production usage, that's why no Nu-Get package available.
To use it follow next steps:
2016-02-04 15:08:28 +01:00
2016-02-04 15:05:48 +01:00
1. Clone TLSharp from GitHub
2016-02-04 15:08:28 +01:00
1. Compile source with VS2015
1. Add reference to ```TLSharp.Core.dll``` to your awesome project.
2015-09-28 04:38:19 +02:00
#Dependencies
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 ).
#Starter Guide
## 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.
1. Goto [API development tools ](https://my.telegram.org/apps ) and copy **API_ID** and **API_HASH** from your account to [TelegramClient.cs ](https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/TelegramClient.cs )
2015-09-30 04:24:32 +02:00
2015-09-28 04:38:19 +02:00
## Using TLSharp
2016-02-04 15:05:48 +01:00
###Initializing client
To initialize client you need to create a store in which TLSharp will save Session info.
```
var store = new FileSessionStore();
```
Next, create client instance and connect to Telegram server.
```
var client = new TelegramClient(store, "session");
await client.Connect();
```
Now, you can call methods.
All methods except [IsPhoneRegistered ](#IsPhoneRegistered ) requires to authenticated user. Example usage of all methods you can find in [Tests].
###Supported methods
Currently supported methods:
2016-02-07 11:34:12 +01:00
- [IsPhoneRegistered - Check if phone is registered in Telegram ](#isphoneregistered )
- [Authenticate user ](#authenticate-user )
- [Get Contact by Phone number ](#get-contact-by-phone-number )
- [Get Contact by Username ](#get-contact-by-username )
- [Send Message to Contact ](#send-message-to-contact )
- [Send Media to Contact ](#send-media-to-contact )
- [Get Messages History for a Contact ](#get-messages-history )
2016-02-04 15:05:48 +01:00
####IsPhoneRegistered
Check if phone number registered to Telegram.
_Example_:
```
var result = await client.IsPhoneRegistered(phoneNumber)
```
* phoneNumber - **string** , phone number in international format (eg. 791812312323)
**Returns:** **bool** , is phone registerd in Telegram or not.
####Authenticate user
Authenticate user by phone number and secret code.
_Example_:
```
var hash = await client.SendCodeRequest(phoneNumber);
var code = "1234"; //code that you receive from Telegram
var user = await client.MakeAuth(phoneNumber, hash, code);
```
* phoneNumber - **string** , phone number in international format (eg. 791812312323)
**Returns:** **User** , authenticated User.
####Get Contact By Phone number
Get user id by phone number.
_Example_:
```
2016-02-07 11:28:41 +01:00
var res = await client.ImportContactByPhoneNumber("791812312323");
2016-02-04 15:05:48 +01:00
```
* phoneNumber - **string** , phone number in international format (eg. 791812312323)
2016-02-07 11:28:41 +01:00
**Returns**: **int?** , User Id or null if no such user.
2016-02-04 15:05:48 +01:00
####Get Contact By Username
Get user id by userName.
_Example_:
```
var res = await client.ImportByUserName(userName);
```
* userName - **string** , user name (eg. telegram_bot)
2016-02-07 11:28:41 +01:00
**Returns**: **int?** , User Id or null if no such user.
2016-02-04 15:05:48 +01:00
####Send Message To Contact
Send text message to specified user
_Example_:
```
await client.SendMessage(userId, message);
```
* userId - **int** , user id
* message - **string** , message
####Send Media To Contact
Send media file to specified contact.
_Example_:
```
var mediaFile = await client.UploadFile(file_name, file);
var res = await client.SendMediaMessage(userId, mediaFile);
```
* file_name - **string** , file name with extension (eg. "file.jpg")
* file - **byte[]** , file content
* userId - **int** , user id
* mediaFile - **InputFile** , reference to uploaded file
**Returns**: **bool** , file sent or not
####Get Messages History
Returns messages history for specified userId.
_Example_:
```
var hist = await client.GetMessagesHistoryForContact(userId, offset, limit);
```
* userId - **int** , user id
* offset - **int** , from what index start load history
* limit - **int** , how much items return
**Returns**: **List\<Message\>** , message history
2015-09-28 04:38:19 +02:00
## Contributing
2016-02-04 15:05:48 +01:00
Contributing is highly appreciated!
###How to add new functions
2016-02-07 11:28:41 +01:00
Adding new functions is easy.
* Just create a new Request class in Requests folder.
* Derive it from MTProtoRequest.
2016-02-07 11:34:12 +01:00
Requests specification you can find in [Telegram API ](https://core.telegram.org/#api-methods ) reference.
2016-02-07 11:28:41 +01:00
_Example_:
```
public class ExampleRequest : MTProtoRequest
{
private int _someParameter;
// pass needed parameters through constructor, and save it to private vars
public InitConnectionRequest(int someParameter)
{
_someParameter = someParameter;
}
// send all needed params to Telegram
public override void OnSend(BinaryWriter writer)
{
writer.Write(_someParameter);
}
// read a received data from Telegram
public override void OnResponse(BinaryReader reader)
{
_someParameter = reader.ReadUInt32();
}
public override void OnException(Exception exception)
{
throw new NotImplementedException();
}
public override bool Responded { get; }
public override bool Confirmed => true;
}
```
2016-02-07 11:34:12 +01:00
More advanced examples you can find in [Requests folder ](https://github.com/sochix/TLSharp/tree/master/TLSharp.Core/Requests ).
2016-02-07 11:28:41 +01:00
###What things can I Implement (Project Roadmap)?
2016-02-07 11:34:12 +01:00
* Factor out current TL language implementation, and use [this one ](https://github.com/everbytes/SharpTL )
2016-02-07 11:28:41 +01:00
* Add possibility to get current user Chats and Users
* Fix Chat requests (Create, AddUser)
* Add Updates handling
* Add possibility to work with Channels
2015-09-28 04:38:19 +02:00
# FAQ
2015-10-01 04:52:40 +02:00
#### I get an error MIGRATE_X?
2015-09-28 04:38:19 +02:00
2016-01-27 12:07:51 +01:00
TLSharp library should automatically handle this errors. If you see such errors, pls create a new issue.
2015-09-28 04:38:19 +02:00
2015-09-30 04:29:29 +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.
You should create a Telegram session. See [configuration guide ](#sending-messages-set-up )
2015-10-03 03:27:41 +02:00
#### Why I get FLOOD_WAIT error?
It's Telegram restrictions. See [this ](https://core.telegram.org/api/errors#420-flood )
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
Create an issue in project bug tracker.
**Attach this information**:
* 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.
2015-09-28 04:38:19 +02:00
# License
**Please, provide link to an author when you using library**
The MIT License
Copyright (c) 2015 Ilya Pirozhenko http://www.sochix.ru/
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.