private static void GetSendedCode() { Console.WriteLine("Enter code that send to your number : "); code = Console.ReadLine(); try { telegram.AuthUser(hash, mobile.Trim(), code.Trim()).GetAwaiter().GetResult(); } catch (CloudPasswordNeededException) { GetPassword(); } catch (InvalidPhoneCodeException) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Invalid code"); Console.ForegroundColor = ConsoleColor.White; GetSendedCode(); } }
public IActionResult SetTelegramSettings([FromBody] TelegramSetupSettings telegramSetupSettings) { if (!string.IsNullOrWhiteSpace(telegramSetupSettings.CodeToAuthenticate)) { if (string.IsNullOrWhiteSpace(telegramSetupSettings.ApiHash) || string.IsNullOrWhiteSpace(telegramSetupSettings.ApiId) || string.IsNullOrWhiteSpace(telegramSetupSettings.NumberToAuthenticate)) { return(new JsonResult(new TelegramSettingsMissingJsonResult(telegramSetupSettings))); } // trying to auth _telegramSettings.ApiId = telegramSetupSettings.ApiId; _telegramSettings.ApiHash = telegramSetupSettings.ApiHash; _telegramSettings.NumberToAuthenticate = telegramSetupSettings.NumberToAuthenticate; _telegramSettings.CodeToAuthenticate = telegramSetupSettings.CodeToAuthenticate; _telegramService.Reset(_telegramSettings); try { _telegramService.AuthUser().Wait(); var info = new AuthSuccessfulJsonResult(); Log.Logger.Information(info.MessageText); return(new JsonResult(info)); } catch (Exception e) { if (e.InnerException != null && !string.IsNullOrWhiteSpace(e.InnerException.Message) && (e.InnerException.Message.Contains( "The numeric code used to authenticate does not match the numeric code sent by SMS/Telegram") || e.InnerException.Message.Contains("AUTH_RESTART"))) { return(new JsonResult(new TelegramTwoWayAuthPerformingResult())); } Console.WriteLine(e); return(new JsonResult(new TelegramSettingsMissingJsonResult(telegramSetupSettings))); } } if (string.IsNullOrWhiteSpace(telegramSetupSettings.ApiHash) || string.IsNullOrWhiteSpace(telegramSetupSettings.ApiId) || string.IsNullOrWhiteSpace(telegramSetupSettings.NumberToAuthenticate)) { return(new JsonResult(new TelegramSettingsMissingJsonResult(telegramSetupSettings))); } _telegramSettings.ApiId = telegramSetupSettings.ApiId; _telegramSettings.ApiHash = telegramSetupSettings.ApiHash; _telegramSettings.NumberToAuthenticate = telegramSetupSettings.NumberToAuthenticate; _telegramSettings.CodeToAuthenticate = string.Empty; try { _telegramService.Reset(_telegramSettings); _telegramService.AuthUser().Wait(); return(new JsonResult(new TelegramTwoWayAuthPerformingResult())); } catch (InvalidPhoneCodeException ex) { return(new JsonResult(new TelegramTwoWayAuthPerformingResult())); } catch (Exception e) { return(new JsonResult(new TelegramTwoWayAuthPerformingResult())); } return(Ok(null)); }