Merge pull request #237 from aarani/master

Bug Solved
This commit is contained in:
Ilya Pirozhenko 2016-10-11 13:05:43 +03:00 committed by GitHub
commit a14dfdc1fe
2 changed files with 9 additions and 310 deletions

312
README.md
View file

@ -1,5 +1,7 @@
#TLSharp
[![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)
[![Build status](https://ci.appveyor.com/api/projects/status/95rl618ch5c4h2fa?svg=true)](https://ci.appveyor.com/project/sochix/tlsharp)
Telegram (http://telegram.org) client library implemented in C#. Only basic functionality is currently implemented. **Consider donation to speed up development process.** Bitcoin wallet: **3K1ocweFgaHnAibJ3n6hX7RNZWFTFcJjUe**
@ -10,9 +12,7 @@ It's a perfect fit for any developer who would like to send data directly to Tel
:exclamation: **Please, don't use it for SPAM!**
[How-To: Send messages to Telegram from C#](http://www.sochix.ru/how-to-send-messages-to-telegram-from-c/)
:ru: Russian description you can find [here](https://habrahabr.ru/post/277079/)
# The Docuemention Moved to [TLSharp.aarani.ir](http://tlsharp.aarani.ir)
#Table of contents?
@ -20,7 +20,6 @@ It's a perfect fit for any developer who would like to send data directly to Tel
- [Dependencies](#dependencies)
- [Starter Guide](#starter-guide)
- [Quick configuration](#quick-configuration)
- [Using TLSharp](#using-tlsharp)
- [Contributing](#contributing)
- [FAQ](#faq)
- [Donations](#donations)
@ -49,314 +48,13 @@ Telegram API isn't that easy to start. You need to do some configuration first.
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. You'll need it later.
## Using TLSharp
###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. You need your **API_ID** and **API_HASH** for this step.
```
var client = new TelegramClient(store, "session", API_ID, "API_HASH");
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:
- [IsPhoneRegistered - Check if phone is registered in Telegram](#isphoneregistered)
- [Authenticate user](#authenticate-user)
- [SignUp user](#signup-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 Contact](#get-messages-history-for-contact)
- [Get UserFull](#get-userfull)
- [Create Chat](#create-chat)
- [Add Chat user](#add-chat-user)
- [Delete Chat user](#delete-chat-user)
- [Leave Chat](#leave-chat)
- [Get Updates State](#get-updates-state)
- [Get Updates Difference](#get-updates-difference)
####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.
####SignUp user
Register new user by phone number, secret code and first/last name.
_Example_:
```
var hash = await client.SendCodeRequest(phoneNumber);
var code = "1234"; //code that you receive from Telegram
var user = await client.SignUp(phoneNumber, hash, code, "TLSharp", "User");
```
* phoneNumber - **string**, phone number in international format (eg. 791812312323) that is not yet registered in Telegram.
* firstName - **string**, new user first name
* lastName - **string**, new user last name
**Returns:** **User**, authenticated User.
####Get Contact By Phone number
Get user id by phone number.
_Example_:
```
var res = await client.ImportContactByPhoneNumber("791812312323");
```
* phoneNumber - **string**, phone number in international format (eg. 791812312323)
**Returns**: **int?**, User Id or null if no such user.
####Get Contact By Username
Get user id by userName.
_Example_:
```
var res = await client.ImportByUserName(userName);
```
* userName - **string**, user name (eg. telegram_bot)
**Returns**: **int?**, User Id or null if no such user.
####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 for Contact
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
####Get UserFull
Returns user's full information for specified userId.
_Example_:
```
var userFull = await client.GetUserFull(userId);
```
* userId - **int**, user id
**Returns**: **UserFull**, User's information
####Create Chat
Creates a new chat.
_Example_:
```
var statedMessage = await client.CreateChat(title, new List<string> { userId1, userId2 });
```
* title - **string**, chat name
* userIdsToInvite - **List<int>**, list of userIds to invite to chat. Current user will be automatically added to this chat.
**Returns**: **Messages_statedMessageConstructor**, Message that contains information about created chat.
####Add Chat user
Adds a user to a chat and sends a service message on it.
_Example_:
```
var statedMessage = await client.AddChatUser(chatId, userId);
```
* chatId - **int**, Chat ID
* userId - **int**, User ID to be added
**Returns**: **Messages_statedMessageConstructor**, Message that contains information about modified chat.
####Delete Chat user
Deletes a user from a chat and sends a service message on it.
_Example_:
```
var statedMessage = await client.DeleteChatUser(chatId, userId);
```
* chatId - **int**, Chat ID
* userId - **int**, User ID to be deleted
**Returns**: **Messages_statedMessageConstructor**, Message that contains information about modified chat.
####Leave Chat
Leaves the chat by deleting currently authenticated user from it.
_Example_:
```
var statedMessage = await client.LeaveChat(chatId);
```
* chatId - **int**, Chat ID
**Returns**: **Messages_statedMessageConstructor**, Message that contains information about modified chat.
####Get Updates State
Returns a current state of updates.
_Example_:
```
var userFull = await client.GetUpdatesState();
```
**Returns**: **UpdatesState**, Object contains info on state for further updates.
####Get Updates Difference
Returns diffetence between the current state of updates and transmitted.
_Example_:
```
var userFull = await client.GetUpdatesDifference(currentState.pts, currentState.date, currentState.qts);
```
* lastPts - **int**, The most relevant value of parameter pts of (updates.state)
* lastDate - **int**, The most relevant value of parameter date of (updates.state)
* lastQts - **int**, The most relevant value of parameter qts of (updates.state)
**Returns**: **UpdatesDifference**, Occurred changes.
## Contributing
Contributing is highly appreciated!
###How to add new functions
Adding new functions is easy.
* Just create a new Request class in Requests folder.
* Derive it from MTProtoRequest.
Requests specification you can find in [Telegram API](https://core.telegram.org/#api-methods) reference.
_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;
}
```
More advanced examples you can find in [Requests folder](https://github.com/sochix/TLSharp/tree/master/TLSharp.Core/Requests).
###What things can I Implement (Project Roadmap)?
* Factor out current TL language implementation, and use [this one](https://github.com/everbytes/SharpTL)
* Add possibility to get current user Chats and Users
* Fix Chat requests (Create, AddUser)
* Add Updates handling
* Add possibility to work with Channels
# FAQ
@ -364,7 +62,7 @@ More advanced examples you can find in [Requests folder](https://github.com/soch
TLSharp library should automatically handle this errors. If you see such errors, pls create a new issue.
#### 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.
#### 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)
@ -398,7 +96,7 @@ List of donators:
The MIT License
Copyright (c) 2015 Ilya Pirozhenko http://www.sochix.ru/
Copyright (c) 2015 Ilya Pirozhenko http://www.sochix.ru/ & Afshin Arani http://aarani.ir
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:

View file

@ -43,9 +43,10 @@ namespace TeleSharp.TL
else if (typeof(T).BaseType == typeof(TLObject))
{
int constructor = br.ReadInt32();
object obj = Activator.CreateInstance(typeof(T));
typeof(T).GetMethod("DeserializeBody").Invoke(obj, new object[] { br });
lists.Add((T)Convert.ChangeType(obj,typeof(T)));
Type type = TLContext.getType(constructor);
object obj = Activator.CreateInstance(type);
type.GetMethod("DeserializeBody").Invoke(obj, new object[] { br });
lists.Add((T)Convert.ChangeType(obj, type));
}
}
}