From e4e20c7f8eb35cab927f5f969caae3858c276ed4 Mon Sep 17 00:00:00 2001 From: Aquatica <81624781+Aquathing@users.noreply.github.com> Date: Fri, 6 May 2022 12:28:34 +0200 Subject: [PATCH 1/4] Update README.md --- README.md | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/README.md b/README.md index 8b1378917..f625e0c70 100644 --- a/README.md +++ b/README.md @@ -1 +1,127 @@ +# What is CatraProto? +CatraProto is a fully-asynchronous library that implements the MTProto protocol and the Telegram API. +This means you can interact with the Telegram API (**as a regular user** as well) **without having any knowledge of the encryption and communication protocol**, it also implements the API so **you won't have to take care of any database implementation or updates handling.** + +# Getting started +## Retrieving API credentials +In order to interact with the API, we will need a combination of api_id and api_hash which can be retrieved by logging-in to my.telegram.org. +**Never share these credentials with anyone.** + +More information can be found [here](app_configuration.md) +## Adding CatraProto to your project +After retrieving our API credentials we must add CatraProto to your project. You can do this by referencing CatraProto.Client from NuGet or by downloading the nupkg package from GitHub's Releases page. +## Receiving messages +To receiving incoming updates, we start off by creating an EventHandler class which extends the [IEventHandler](https://github.com/CatraProto/Client/blob/master/src/CatraProto.Client/Updates/Interfaces/IEventHandler.cs) interface. +```cs +public class EventHandler : IEventHandler +{ + private readonly TelegramClient _client; + + public EventHandler(TelegramClient client) + { + _client = client; + } + + public async Task OnUpdateAsync(UpdateBase update) + { + if (update is UpdateNewMessage { Message: Message { Out: false } message }) + { + var asPeerId = PeerId.FromPeer(message.PeerId); + if (asPeerId.Type is not PeerType.User) + { + // We only want to reply to messages sent in private chat. + return; + } + + await _client.Api.CloudChatsApi.Messages.SendMessageAsync(asPeerId, "Hello user. Thank you for contacting me and trying CatraProto!"); + } + } +} +``` + +## Initializing the client +Now that we have written the EventHandler and implemented our own logic, we initialize CatraProto and login for the first time. + +```cs +// Create the SeriLog logger to use. +var logger = Logger.CreateDefaultLogger(); + +// We define the parameters used to connect to Telegram. You can change anything you want here (as long as you use an existing lang pack and **don't use official api credentials**) +var apiSettings = new ApiSettings(YOUR_API_ID, YOUR_API_HASH, "CatraProto", "1.0", "en", "android", "en", "1.0"); + +// Now, we need to tell CatraProto where to store the session and the data it needs to work with. +var sessionSettings = new SessionSettings(new FileSerializer($"data/MySession.catra"), new DatabaseSettings("data/MySession.db", 50), sessionName); + +// In your my.telegram.org page you can also see there is an addresses for production DCs, and an address for test DCs. +// You can choose whichever you like based on your needs but keep in mind that test DCs is a completely different environment. +var connectionInfo = new ConnectionInfo(IPAddress.Parse(DC_IP), IS_TEST, 443, ID); + +var connectionSettings = new ConnectionSettings(connectionInfo, 86400, 30); + +// We put everything together +var settings = new ClientSettings(sessionSettings, apiSettings, connectionSettings); + +// Instanciate the client +await using var client = new TelegramClient(settings, logger); + +// Set the event handler +client.SetEventHandler(new EventHandler(client, logger)); + +// Initialize the client +var clientState = await client.InitClientAsync(); + +if(clientState is ClientState.Corrupted) +{ + //The session is corrupted, you need to delete it and login again. + return; +} + +if(clientState is ClientState.Unauthenticated) +{ + // We need to login, either as a bot or as a user. + var login = client.GetLoginFlow(); + // You can obviously also use Console.ReadLine() to get the phone number for the console + var authentication = await login.AsUser(PHONE_NUMBER_HERE, new CodeSettings()); + // If you want to login as a user, you can just uncomment the line below and comment the line above. + //var authentication = await login.AsBotAsync(BOT_TOKEN_HERE); + var finished = false; + while (!finished) + { + switch (authentication) + { + case LoginNeedsCode completeLogin: + logger.Information("Now insert the login code: "); + authentication = await completeLogin.WithCodeAsync(Console.ReadLine()); + break; + case LoginNeedsSignup signup: + logger.Information("Creating account"); + authentication = await signup.WithProfileData("My CatraProto", "Account"); + break; + case LoginFailed loginFailed: + logger.Error("Login failed error {Error}", loginFailed.FailReason); + finished = true; + break; + case LoginSuccessful loginSuccessful: + logger.Information("Login successful, user: {User}", loginSuccessful.LoggedUser.ToJson()); + finished = true; + break; + default: + logger.Error("Type {Authentication} is not supported", authentication); + return; + } + } +} + +logger.Information("Press any key to kill the client"); +Console.ReadLine(); +await client.ForceSaveAsync(); +``` + +Upon exiting, you must **always** save the session by calling `client.ForceSaveAsync()` and dispose the `TelegramClient` object (in this example, it is done by declaring the _client_ variable with `await using`). + + # Full documentation + Full documentation is available at https://catraproto.github.io/docs + + # License + Licensed under LGPL 3.0. See [COPYING](COPYING) and [COPYING.LESSER](COPYING.LESSER) From 2c5ed4c55cf1f5e36eabaa5db6e3f264083620d0 Mon Sep 17 00:00:00 2001 From: Aquatica <81624781+Aquathing@users.noreply.github.com> Date: Fri, 6 May 2022 13:20:23 +0200 Subject: [PATCH 2/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f625e0c70..df27655c7 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ More information can be found [here](app_configuration.md) ## Adding CatraProto to your project After retrieving our API credentials we must add CatraProto to your project. You can do this by referencing CatraProto.Client from NuGet or by downloading the nupkg package from GitHub's Releases page. ## Receiving messages -To receiving incoming updates, we start off by creating an EventHandler class which extends the [IEventHandler](https://github.com/CatraProto/Client/blob/master/src/CatraProto.Client/Updates/Interfaces/IEventHandler.cs) interface. +To start receiving incoming updates, we start off by creating an EventHandler class which extends the [IEventHandler](https://github.com/CatraProto/Client/blob/master/src/CatraProto.Client/Updates/Interfaces/IEventHandler.cs) interface. ```cs public class EventHandler : IEventHandler { From 993ad03433ffa4f161355ab907a844ec3ec9c83f Mon Sep 17 00:00:00 2001 From: Aquatica <81624781+Aquathing@users.noreply.github.com> Date: Fri, 6 May 2022 13:47:01 +0200 Subject: [PATCH 3/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index df27655c7..9d75b67a7 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This means you can interact with the Telegram API (**as a regular user** as well In order to interact with the API, we will need a combination of api_id and api_hash which can be retrieved by logging-in to my.telegram.org. **Never share these credentials with anyone.** -More information can be found [here](app_configuration.md) +More information can be found [here](https://catraproto.github.io/docs/app_configuration.html") ## Adding CatraProto to your project After retrieving our API credentials we must add CatraProto to your project. You can do this by referencing CatraProto.Client from NuGet or by downloading the nupkg package from GitHub's Releases page. ## Receiving messages From de58f8a7e9d81ad220f8dd595b712b56f63e4a7c Mon Sep 17 00:00:00 2001 From: Aquatica <81624781+Aquathing@users.noreply.github.com> Date: Fri, 6 May 2022 13:47:56 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9d75b67a7..15229404d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This means you can interact with the Telegram API (**as a regular user** as well In order to interact with the API, we will need a combination of api_id and api_hash which can be retrieved by logging-in to my.telegram.org. **Never share these credentials with anyone.** -More information can be found [here](https://catraproto.github.io/docs/app_configuration.html") +More information can be found [here](https://catraproto.github.io/docs/app_configuration.html) ## Adding CatraProto to your project After retrieving our API credentials we must add CatraProto to your project. You can do this by referencing CatraProto.Client from NuGet or by downloading the nupkg package from GitHub's Releases page. ## Receiving messages