summaryrefslogtreecommitdiff
path: root/protocols/Telegram/tdlib/td/example/csharp
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2022-11-30 17:48:47 +0300
committerGeorge Hazan <ghazan@miranda.im>2022-11-30 17:48:47 +0300
commit0ece30dc7c0e34b4c5911969b8fa99c33c6d023c (patch)
tree671325d3fec09b999411e4e3ab84ef8259261818 /protocols/Telegram/tdlib/td/example/csharp
parent46c53ffc6809c67e4607e99951a2846c382b63b2 (diff)
Telegram: update for TDLIB
Diffstat (limited to 'protocols/Telegram/tdlib/td/example/csharp')
-rw-r--r--protocols/Telegram/tdlib/td/example/csharp/README.md18
-rw-r--r--protocols/Telegram/tdlib/td/example/csharp/TdExample.cs98
-rw-r--r--protocols/Telegram/tdlib/td/example/csharp/TdExample.csproj64
3 files changed, 122 insertions, 58 deletions
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 <path to vcpkg>
+.\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 <path to TDLib sources>/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=<path to vcpkg>/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=<path to vcpkg>/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 <chatId> - GetChat, me - GetMe, sm <chatId> <message> - SendMessage, lo - LogOut, q - Quit): ";
+ private static readonly string _commandsLine = "Enter command (gc <chatId> - GetChat, me - GetMe, sm <chatId> <message> - 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 @@
<PropertyGroup>
<RootNamespace>TdExample</RootNamespace>
</PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
+ <DebugSymbols>true</DebugSymbols>
+ <OutputPath>bin\x64\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <DebugType>full</DebugType>
+ <PlatformTarget>x64</PlatformTarget>
+ <ErrorReport>prompt</ErrorReport>
+ <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
+ <OutputPath>bin\x64\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <Optimize>true</Optimize>
+ <DebugType>pdbonly</DebugType>
+ <PlatformTarget>x64</PlatformTarget>
+ <ErrorReport>prompt</ErrorReport>
+ <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
+ </PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
@@ -43,38 +61,54 @@
<Reference Include="System.Xml.Linq" />
<Reference Include="Telegram.Td, Version=0.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
- <HintPath Condition=" '$(Configuration)' == 'Debug' ">build\Debug\Telegram.Td.dll</HintPath>
- <HintPath Condition=" '$(Configuration)' == 'Release' ">build\Release\Telegram.Td.dll</HintPath>
+ <HintPath Condition=" '$(Platform)' == 'x86' ">build\$(Configuration)\Telegram.Td.dll</HintPath>
+ <HintPath Condition=" '$(Platform)' == 'x64' ">build64\$(Configuration)\Telegram.Td.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="TdExample.cs" />
</ItemGroup>
- <ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
- <Content Include="build\Debug\LIBEAY32.dll">
- <Link>LIBEAY32.dll</Link>
+ <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+ <Content Include="build\Debug\zlibd1.dll">
+ <Link>zlibd1.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
- <Content Include="build\Debug\SSLEAY32.dll">
- <Link>SSLEAY32.dll</Link>
+ </ItemGroup>
+ <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+ <Content Include="build\Release\zlib1.dll">
+ <Link>zlib1.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
- <Content Include="build\Debug\zlibd1.dll">
+ </ItemGroup>
+ <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
+ <Content Include="build64\Debug\zlibd1.dll">
<Link>zlibd1.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
- <ItemGroup Condition=" '$(Configuration)' == 'Release' ">
- <Content Include="build\Release\LIBEAY32.dll">
- <Link>LIBEAY32.dll</Link>
+ <ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
+ <Content Include="build64\Release\zlib1.dll">
+ <Link>zlib1.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
- <Content Include="build\Release\SSLEAY32.dll">
- <Link>SSLEAY32.dll</Link>
+ </ItemGroup>
+ <ItemGroup Condition=" '$(Platform)' == 'x86' ">
+ <Content Include="build\$(Configuration)\libcrypto-1_1.dll">
+ <Link>libcrypto-1_1.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
- <Content Include="build\Release\zlib1.dll">
- <Link>zlib1.dll</Link>
+ <Content Include="build\$(Configuration)\libssl-1_1.dll">
+ <Link>libssl-1_1.dll</Link>
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
+ </ItemGroup>
+ <ItemGroup Condition=" '$(Platform)' == 'x64' ">
+ <Content Include="build64\$(Configuration)\libcrypto-1_1-x64.dll">
+ <Link>libcrypto-1_1-x64.dll</Link>
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
+ <Content Include="build64\$(Configuration)\libssl-1_1-x64.dll">
+ <Link>libssl-1_1-x64.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>