From 0ece30dc7c0e34b4c5911969b8fa99c33c6d023c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 30 Nov 2022 17:48:47 +0300 Subject: Telegram: update for TDLIB --- .../Telegram/tdlib/td/example/csharp/README.md | 18 ++-- .../Telegram/tdlib/td/example/csharp/TdExample.cs | 98 ++++++++++++++-------- .../tdlib/td/example/csharp/TdExample.csproj | 64 ++++++++++---- 3 files changed, 122 insertions(+), 58 deletions(-) (limited to 'protocols/Telegram/tdlib/td/example/csharp') diff --git a/protocols/Telegram/tdlib/td/example/csharp/README.md b/protocols/Telegram/tdlib/td/example/csharp/README.md index 2faa15e12e..b20d65947e 100644 --- a/protocols/Telegram/tdlib/td/example/csharp/README.md +++ b/protocols/Telegram/tdlib/td/example/csharp/README.md @@ -5,20 +5,26 @@ This is an example of building TDLib with `C++/CLI` support and an example of TD ## Building TDLib * Download and install Microsoft Visual Studio 2015 or later. -* Download and install [CMake](https://cmake.org/download/). +* Download and install [CMake](https://cmake.org/download/); choose "Add CMake to the system PATH" option while installing. * Install [vcpkg](https://github.com/Microsoft/vcpkg#quick-start) or update it to the latest version using `vcpkg update` and following received instructions. -* Install `zlib` and `openssl` for using `vcpkg`: +* Install `gperf`, `zlib`, and `openssl` using `vcpkg`: ``` -C:\src\vcpkg> .\vcpkg.exe install openssl:x64-windows openssl:x86-windows zlib:x64-windows zlib:x86-windows +cd +.\vcpkg.exe install gperf:x64-windows gperf:x86-windows openssl:x64-windows openssl:x86-windows zlib:x64-windows zlib:x86-windows ``` * (Optional. For XML documentation generation.) Download [PHP](https://windows.php.net/download#php-7.2). Add the path to php.exe to the PATH environment variable. -* Download and install [gperf](https://sourceforge.net/projects/gnuwin32/files/gperf/3.0.1/). Add the path to gperf.exe to the PATH environment variable. * Build `TDLib` with CMake enabling `.NET` support and specifying correct path to `vcpkg` toolchain file: ``` cd /example/csharp mkdir build cd build -cmake -DTD_ENABLE_DOTNET=ON -DCMAKE_TOOLCHAIN_FILE=C:\src\vcpkg\scripts\buildsystems\vcpkg.cmake ../../.. +cmake -A Win32 -DTD_ENABLE_DOTNET=ON -DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake ../../.. +cmake --build . --config Release +cmake --build . --config Debug +cd .. +mkdir build64 +cd build64 +cmake -A x64 -DTD_ENABLE_DOTNET=ON -DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake ../../.. cmake --build . --config Release cmake --build . --config Debug ``` @@ -27,6 +33,6 @@ cmake --build . --config Debug After `TDLib` is built you can open and run TdExample project. It contains a simple console C# application with implementation of authorization and message sending. -Just open it with Visual Studio 2015 or 2017 and run. +Just open it with Visual Studio 2015 or later and run. Also see TdExample.csproj for example of including TDLib in C# project with all native shared library dependencies. diff --git a/protocols/Telegram/tdlib/td/example/csharp/TdExample.cs b/protocols/Telegram/tdlib/td/example/csharp/TdExample.cs index be98e1a758..0c07ee7c7b 100644 --- a/protocols/Telegram/tdlib/td/example/csharp/TdExample.cs +++ b/protocols/Telegram/tdlib/td/example/csharp/TdExample.cs @@ -1,5 +1,5 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -23,23 +23,18 @@ namespace TdExample private static TdApi.AuthorizationState _authorizationState = null; private static volatile bool _haveAuthorization = false; - private static volatile bool _quiting = false; + private static volatile bool _needQuit = false; + private static volatile bool _canQuit = false; private static volatile AutoResetEvent _gotAuthorization = new AutoResetEvent(false); private static readonly string _newLine = Environment.NewLine; - private static readonly string _commandsLine = "Enter command (gc - GetChat, me - GetMe, sm - SendMessage, lo - LogOut, q - Quit): "; + private static readonly string _commandsLine = "Enter command (gc - GetChat, me - GetMe, sm - SendMessage, lo - LogOut, r - Restart, q - Quit): "; private static volatile string _currentPrompt = null; private static Td.Client CreateTdClient() { - Td.Client result = Td.Client.Create(new UpdatesHandler()); - new Thread(() => - { - Thread.CurrentThread.IsBackground = true; - result.Run(); - }).Start(); - return result; + return Td.Client.Create(new UpdateHandler()); } private static void Print(string str) @@ -72,33 +67,48 @@ namespace TdExample } if (_authorizationState is TdApi.AuthorizationStateWaitTdlibParameters) { - TdApi.TdlibParameters parameters = new TdApi.TdlibParameters(); - parameters.DatabaseDirectory = "tdlib"; - parameters.UseMessageDatabase = true; - parameters.UseSecretChats = true; - parameters.ApiId = 94575; - parameters.ApiHash = "a3406de8d171bb422bb6ddf3bbd800e2"; - parameters.SystemLanguageCode = "en"; - parameters.DeviceModel = "Desktop"; - parameters.SystemVersion = "Unknown"; - parameters.ApplicationVersion = "1.0"; - parameters.EnableStorageOptimizer = true; + TdApi.SetTdlibParameters request = new TdApi.SetTdlibParameters(); + request.DatabaseDirectory = "tdlib"; + request.UseMessageDatabase = true; + request.UseSecretChats = true; + request.ApiId = 94575; + request.ApiHash = "a3406de8d171bb422bb6ddf3bbd800e2"; + request.SystemLanguageCode = "en"; + request.DeviceModel = "Desktop"; + request.ApplicationVersion = "1.0"; + request.EnableStorageOptimizer = true; - _client.Send(new TdApi.SetTdlibParameters(parameters), new AuthorizationRequestHandler()); - } - else if (_authorizationState is TdApi.AuthorizationStateWaitEncryptionKey) - { - _client.Send(new TdApi.CheckDatabaseEncryptionKey(), new AuthorizationRequestHandler()); + _client.Send(request, new AuthorizationRequestHandler()); } else if (_authorizationState is TdApi.AuthorizationStateWaitPhoneNumber) { string phoneNumber = ReadLine("Please enter phone number: "); - _client.Send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, false, false), new AuthorizationRequestHandler()); + _client.Send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, null), new AuthorizationRequestHandler()); + } + else if (_authorizationState is TdApi.AuthorizationStateWaitEmailAddress) + { + string emailAddress = ReadLine("Please enter email address: "); + _client.Send(new TdApi.SetAuthenticationEmailAddress(emailAddress), new AuthorizationRequestHandler()); + } + else if (_authorizationState is TdApi.AuthorizationStateWaitEmailCode) + { + string code = ReadLine("Please enter email authentication code: "); + _client.Send(new TdApi.CheckAuthenticationEmailCode(new TdApi.EmailAddressAuthenticationCode(code)), new AuthorizationRequestHandler()); + } + else if (_authorizationState is TdApi.AuthorizationStateWaitOtherDeviceConfirmation state) + { + Console.WriteLine("Please confirm this login link on another device: " + state.Link); } else if (_authorizationState is TdApi.AuthorizationStateWaitCode) { string code = ReadLine("Please enter authentication code: "); - _client.Send(new TdApi.CheckAuthenticationCode(code, "", ""), new AuthorizationRequestHandler()); + _client.Send(new TdApi.CheckAuthenticationCode(code), new AuthorizationRequestHandler()); + } + else if (_authorizationState is TdApi.AuthorizationStateWaitRegistration) + { + string firstName = ReadLine("Please enter your first name: "); + string lastName = ReadLine("Please enter your last name: "); + _client.Send(new TdApi.RegisterUser(firstName, lastName), new AuthorizationRequestHandler()); } else if (_authorizationState is TdApi.AuthorizationStateWaitPassword) { @@ -123,9 +133,11 @@ namespace TdExample else if (_authorizationState is TdApi.AuthorizationStateClosed) { Print("Closed"); - if (!_quiting) + if (!_needQuit) { _client = CreateTdClient(); // recreate _client after previous has closed + } else { + _canQuit = true; } } else @@ -172,8 +184,12 @@ namespace TdExample _haveAuthorization = false; _client.Send(new TdApi.LogOut(), _defaultHandler); break; + case "r": + _haveAuthorization = false; + _client.Send(new TdApi.Close(), _defaultHandler); + break; case "q": - _quiting = true; + _needQuit = true; _haveAuthorization = false; _client.Send(new TdApi.Close(), _defaultHandler); break; @@ -195,37 +211,45 @@ namespace TdExample TdApi.ReplyMarkup replyMarkup = new TdApi.ReplyMarkupInlineKeyboard(new TdApi.InlineKeyboardButton[][] { row, row, row }); TdApi.InputMessageContent content = new TdApi.InputMessageText(new TdApi.FormattedText(message, null), false, true); - _client.Send(new TdApi.SendMessage(chatId, 0, false, false, replyMarkup, content), _defaultHandler); + _client.Send(new TdApi.SendMessage(chatId, 0, 0, null, replyMarkup, content), _defaultHandler); } static void Main() { // disable TDLib log - Td.Log.SetVerbosityLevel(0); - if (!Td.Log.SetFilePath("tdlib.log")) + Td.Client.Execute(new TdApi.SetLogVerbosityLevel(0)); + if (Td.Client.Execute(new TdApi.SetLogStream(new TdApi.LogStreamFile("tdlib.log", 1 << 27, false))) is TdApi.Error) { throw new System.IO.IOException("Write access to the current directory is required"); } + new Thread(() => + { + Thread.CurrentThread.IsBackground = true; + Td.Client.Run(); + }).Start(); // create Td.Client _client = CreateTdClient(); // test Client.Execute - _defaultHandler.OnResult(_client.Execute(new TdApi.GetTextEntities("@telegram /test_command https://telegram.org telegram.me @gif @test"))); + _defaultHandler.OnResult(Td.Client.Execute(new TdApi.GetTextEntities("@telegram /test_command https://telegram.org telegram.me @gif @test"))); // main loop - while (!_quiting) + while (!_needQuit) { // await authorization _gotAuthorization.Reset(); _gotAuthorization.WaitOne(); - _client.Send(new TdApi.GetChats(Int64.MaxValue, 0, 100), _defaultHandler); // preload chat list + _client.Send(new TdApi.LoadChats(null, 100), _defaultHandler); // preload main chat list while (_haveAuthorization) { GetCommand(); } } + while (!_canQuit) { + Thread.Sleep(1); + } } private class DefaultHandler : Td.ClientResultHandler @@ -236,7 +260,7 @@ namespace TdExample } } - private class UpdatesHandler : Td.ClientResultHandler + private class UpdateHandler : Td.ClientResultHandler { void Td.ClientResultHandler.OnResult(TdApi.BaseObject @object) { diff --git a/protocols/Telegram/tdlib/td/example/csharp/TdExample.csproj b/protocols/Telegram/tdlib/td/example/csharp/TdExample.csproj index ea2ad56532..6a511d8f74 100644 --- a/protocols/Telegram/tdlib/td/example/csharp/TdExample.csproj +++ b/protocols/Telegram/tdlib/td/example/csharp/TdExample.csproj @@ -33,6 +33,24 @@ TdExample + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + prompt + MinimumRecommendedRules.ruleset + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + prompt + MinimumRecommendedRules.ruleset + @@ -43,38 +61,54 @@ False - build\Debug\Telegram.Td.dll - build\Release\Telegram.Td.dll + build\$(Configuration)\Telegram.Td.dll + build64\$(Configuration)\Telegram.Td.dll - - - LIBEAY32.dll + + + zlibd1.dll PreserveNewest - - SSLEAY32.dll + + + + zlib1.dll PreserveNewest - + + + zlibd1.dll PreserveNewest - - - LIBEAY32.dll + + + zlib1.dll PreserveNewest - - SSLEAY32.dll + + + + libcrypto-1_1.dll PreserveNewest - - zlib1.dll + + libssl-1_1.dll + PreserveNewest + + + + + libcrypto-1_1-x64.dll + PreserveNewest + + + libssl-1_1-x64.dll PreserveNewest -- cgit v1.2.3