Browse Source

???????????????

Tigran 4 years ago
parent
commit
e49e9c93e1

+ 3 - 3
MafiaTelegramBot/Bot.cs

@@ -1,5 +1,4 @@
-using System;
-using System.Threading;
+using System.Threading;
 using MafiaTelegramBot.Controllers;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot;
@@ -25,7 +24,8 @@ namespace MafiaTelegramBot
         {
             var cts = new CancellationTokenSource();
             Client.StartReceiving(new DefaultUpdateHandler(MessageController.HandleUpdateAsync, MessageController.HandleErrorAsync), cts.Token);
-            Console.ReadLine();
+            WaitHandle.WaitAny(new[] { cts.Token.WaitHandle });
+            Logs.LogOut("Token canceled");
             cts.Cancel();
         }
     }

+ 11 - 11
MafiaTelegramBot/Controllers/MessageController.cs

@@ -62,18 +62,18 @@ namespace MafiaTelegramBot.Controllers
             return new UpdateHandler();
         }
 
-        public static Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
-            CancellationToken cancellationToken)
+        public static Task HandleErrorAsync(ITelegramBotClient bot, Exception e, CancellationToken ct)
         {
-            switch (exception)
+            switch (e)
             {
                 case ApiRequestException apiRequestException:
                     LogOutWarning($"API Error:[{apiRequestException.ErrorCode}] - {apiRequestException.Message}");
                     break;
                 default:
-                    LogOutError(exception.ToString());
+                    LogOutError(e);
                     break;
             }
+            LogOut(ct);
             return Task.CompletedTask;
         }
 
@@ -86,7 +86,7 @@ namespace MafiaTelegramBot.Controllers
             }
             catch (Exception e)
             {
-                LogOutWarning("Can't send text message " + e);
+                LogOutWarning(e.Message);
             }
             return new Message();
         }
@@ -100,7 +100,7 @@ namespace MafiaTelegramBot.Controllers
             }
             catch (Exception e)
             {
-                LogOutWarning("Can't send text message with html " + e);
+                LogOutWarning(e.Message);
             }
             return new Message();
         }
@@ -114,7 +114,7 @@ namespace MafiaTelegramBot.Controllers
             }
             catch (Exception e)
             {
-                LogOutWarning("Can't send sticker " + e);
+                LogOutWarning(e.Message);
             }
             return new Message();
         }
@@ -128,7 +128,7 @@ namespace MafiaTelegramBot.Controllers
             }
             catch (Exception e)
             {
-                LogOutWarning("Can't edit message text " + e);
+                LogOutWarning(e.Message);
             }
             return new Message();
         }
@@ -142,7 +142,7 @@ namespace MafiaTelegramBot.Controllers
             }
             catch (Exception e)
             {
-                LogOutWarning("Can't edit reply markup " + e);
+                LogOutWarning(e.Message);
             }
             return new Message();
         }
@@ -155,7 +155,7 @@ namespace MafiaTelegramBot.Controllers
             }
             catch (Exception e)
             {
-                LogOutWarning("Can't delete message " + e);
+                LogOutWarning(e.Message);
             }
         }
 
@@ -168,7 +168,7 @@ namespace MafiaTelegramBot.Controllers
             }
             catch (Exception e)
             {
-                LogOutWarning("Can't send photo " + e);
+                LogOutWarning(e.Message);
             }
             return new Message();
         }

+ 3 - 3
MafiaTelegramBot/Logs.cs

@@ -6,15 +6,15 @@ namespace MafiaTelegramBot
 {
     public static class Logs
     {
-        public static void LogOut(string message)
+        public static void LogOut(object message)
         {
             Console.WriteLine($"[INFO] [{DateTime.Now.ToString(CultureInfo.CurrentCulture)}] {message}");
         }
-        public static void LogOutWarning(string message)
+        public static void LogOutWarning(object message)
         {
             Console.WriteLine($"[WARNING] [{DateTime.Now.ToString(CultureInfo.CurrentCulture)}] {message}");
         }
-        public static void LogOutError(string message)
+        public static void LogOutError(object message)
         {
             Console.WriteLine($"[ERROR] [{DateTime.Now.ToString(CultureInfo.CurrentCulture)}] {message}");
         }