项目作者: Vladimir-Novick

项目描述 :
Implementing Telegram API in .NET Core2 for Linux Server
高级语言: C#
项目地址: git://github.com/Vladimir-Novick/TLSharp.NetCore2.git
创建时间: 2018-02-23T08:57:29Z
项目社区:https://github.com/Vladimir-Novick/TLSharp.NetCore2

开源协议:MIT License

下载


TLSharp.NetCore2

Implementing Telegram API in .NET Core2

Unofficial Telegram (http://telegram.org) client library implemented in C# on .Net Core2. Thanks to Ilya Pirozhenko http://www.sochix.ru/ for TLSharp source code

Table of contents

How do I add this to my project?

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

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.

First requests

To start work, create an instance of TelegramClient and establish connection

  1. var client = new TelegramClient(apiId, apiHash);
  2. 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)

For authentication you need to run following code

  1. var hash = await client.SendCodeRequestAsync("<user_number>");
  2. var code = "<code_from_telegram>"; // you can change code in debugger
  3. var user = await client.MakeAuthAsync("<user_number>", hash, code);

Full code you can see at AuthUser test

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:

  1. //get available contacts
  2. var result = await client.GetContactsAsync();
  3. //find recipient in contacts
  4. var user = result.Users.lists
  5. .Where(x => x.GetType() == typeof (TLUser))
  6. .Cast<TLUser>()
  7. .FirstOrDefault(x => x.phone == "<recipient_phone>");
  8. //send message
  9. await client.SendMessageAsync(new TLInputPeerUser() {user_id = user.id}, "OUR_MESSAGE");

Full code you can see at SendMessage test

To send message to channel you could use the following code:

  1. //get user dialogs
  2. var dialogs = await client.GetUserDialogsAsync();
  3. //find channel by title
  4. var chat = dialogs.chats.lists
  5. .Where(c => c.GetType() == typeof(TLChannel))
  6. .Cast<TLChannel>()
  7. .FirstOrDefault(c => c.title == "<channel_title>");
  8. //send message
  9. 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

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.

  1. var fileResult = await client.UploadFile("cat.jpg", new StreamReader("data/cat.jpg"));

TLSharp provides two wrappers for sending photo and document

  1. await client.SendUploadedPhoto(new TLInputPeerUser() { user_id = user.id }, fileResult, "kitty");
  2. await client.SendUploadedDocument(
  3. new TLInputPeerUser() { user_id = user.id },
  4. fileResult,
  5. "some zips", //caption
  6. "application/zip", //mime-type
  7. new TLVector<TLAbsDocumentAttribute>()); //document attributes, such as file name

Full code you can see at SendPhotoToContactTest

To download file you should call GetFile method

  1. await client.GetFile(
  2. new TLInputDocumentFileLocation()
  3. {
  4. access_hash = document.access_hash,
  5. id = document.id,
  6. version = document.version
  7. },
  8. document.size); //size of fileChunk you want to retrieve

Full code you can see at DownloadFileFromContactTest

Available Methods

For your convenience TLSharp have wrappers for several Telegram API methods. You could add your own, see details below.

  1. IsPhoneRegisteredAsync
  2. SendCodeRequestAsync
  3. MakeAuthAsync
  4. SignUpAsync
  5. GetContactsAsync
  6. SendMessageAsync
  7. SendTypingAsync
  8. GetUserDialogsAsync
  9. SendUploadedPhoto
  10. SendUploadedDocument
  11. GetFile
  12. UploadFile
  13. SendPingAsync
  14. GetHistoryAsync

License

The MIT License

Copyright (c) 2015 Ilya Pirozhenko http://www.sochix.ru/ ( TLSharp Library )

Copyright (c) 2017 Vladimir Novick https://www.linkedin.com/in/vladimirnovick/ ( TLSharp.NetCore2 Library )

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.