Skip to content

Latest commit

 

History

History
300 lines (196 loc) · 9.14 KB

README.md

File metadata and controls

300 lines (196 loc) · 9.14 KB

#TLSharp

Build status

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

It's a perfect fit for any developer who would like to send data directly to Telegram users.

⭐ If you ❤️ library, please star it! ⭐

Please, don't use it for SPAM!

How-To: Send messages to Telegram from C#

🇷🇺 Russian description you can find here

#Table of contents?

#How do I add this to my project?

Library isn't ready for production usage, that's why no Nu-Get package available.

To use it follow next steps:

  1. Clone TLSharp from GitHub
  2. Compile source with VS2015
  3. Add reference to TLSharp.Core.dll to your awesome project.

#Dependencies

TLSharp has a few dependenices, most of functionality implemented from scratch. All dependencies listed in package.conf file.

#Starter Guide

Quick Configuration

Telegram API isn't that easy to start. You need to do some configuration first.

  1. Create a developer account in Telegram.
  2. Goto API development tools 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 requires to authenticated user. Example usage of all methods you can find in [Tests].

###Supported methods Currently supported methods:

####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:

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 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 Request Returns user's full information for specified userId.

Example:

var userFull = await client.GetUserFull(userId);
  • userId - int, user id

Returns: UserFull, User's information

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 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.

###What things can I Implement (Project Roadmap)?

  • Factor out current TL language implementation, and use this one
  • Add possibility to get current user Chats and Users
  • Fix Chat requests (Create, AddUser)
  • Add Updates handling
  • Add possibility to work with Channels

FAQ

I get an error MIGRATE_X?

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.

You should create a Telegram session. See configuration guide

Why I get FLOOD_WAIT error?

It's Telegram restrictions. See this

Why does TLSharp lacks feature XXXX?

Now TLSharp is basic realization of Telegram protocol, you can be a contributor or a sponsor to speed-up developemnt of any feature.

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.

Donations

Thanks for donations! It's highly appreciated. Bitcoin wallet: 3K1ocweFgaHnAibJ3n6hX7RNZWFTFcJjUe

List of donators:

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.