2017-10-05 6 views
0

同じ問題がある「bot.telegram」ナゲットを使用するいくつかの電信ボットC#アプリケーションがあります。AggregateExceptionによって数時間後にgetUpdate()メソッドが動作しない

getUpdate()メソッドでユーザーメッセージとコマンドを取得します。以下の簡単な例があります:

System.AggregateException: One or more errors occurred. ---> Telegram.Bot.Exceptions.ApiRequestException: Request timed out ---> System.Threading.Tasks.TaskCanceledException: A task was canceled. at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) at Telegram.Bot.TelegramBotClient.d__125 1.MoveNext() --- End of inner exception stack trace --- at Telegram.Bot.TelegramBotClient.<SendWebRequestAsync>d__125 1.MoveNext() --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task 1.get_Result() at mahramanehBot.Main.GetUpdates() in C:\Users\Soroush\documents\visual studio 2015\Projects\mahramanehBot\mahramanehBot\Main.cs:line 45 ---> (Inner Exception #0) Telegram.Bot.Exceptions.ApiRequestException: Request timed out ---> System.Threading.Tasks.TaskCanceledException: A task was canceled. at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) at Telegram.Bot.TelegramBotClient.d__125 1.MoveNext() --- End of inner exception stack trace --- at Telegram.Bot.TelegramBotClient.<SendWebRequestAsync>d__125 1.MoveNext()<---

private void Form1_Load(object sender, EventArgs e) 
{ 
    try 
    { 
     a = new Thread(new ThreadStart(GetUpdates)); 
     a.Start(); 
    } 
    catch (Exception ex) 
    { 
     bot.SendTextMessageAsync({myId}, ex.ToString()); 
    } 
} 


public void GetUpdates() 
{ 
    try 
    { 
     offset = 0; 

     while (true) 
     { 
      updates = bot.GetUpdatesAsync(offset, 100, 360000).Result; 

      foreach (var update in updates) 
      { 
       offset = update.Id + 1; 

       if (update.Message == null || update.Message.Text == null) 
        continue; 

       switch (update.Message.Text) 
       { 
        case "/start": 
         job1(); 
         break; 
        case "Msg2": 
         job2(); 
         break; 
        case "Msg3": 
         job3(); 
         break; 
        default: 
         job(4) 
         break; 
       } 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     bot.SendTextMessageAsync({myId}, ex.ToString()); 
    } 
} 

といくつかの時間後に

(2-24時間)が止まるとuser.In「キャッチ例外」からメッセージを取得しませんが、私はこのエラーを取得します

"メッセージを取得してユーザーからのコマンド"を止めるにはどうすればよいですか?


私は「試す」の後に、このコードを置くことによって、この問題を解決:

  catch (AggregateException e) 
     { 
      Thread a = new Thread(GetUpdates); 
      a.Start(); 
     } 

答えて

1

更新がタイムアウトすることができますし、これを処理する必要があります - 私は、アプリケーションのクラッシュを推定?

例外をキャッチし、結果を処理します(無視することが多い)。結果を待つ必要があるかもしれません。

 while (true) 
     { 
      try 
      { 
       updates = await bot.GetUpdatesAsync(offset, 100, 360000); 

       foreach (var update in updates) 
       { 
        offset = update.Id + 1; 

        if (update.Message == null || update.Message.Text == null) 
        continue; 

        switch (update.Message.Text) 
        { 
         case "/start": 
          job1(); 
          break; 
         case "Msg2": 
          job2(); 
          break; 
         case "Msg3": 
          job3(); 
          break; 
         default: 
          job(4) 
          break; 
        } 
       } 
      } 
      catch (ApiRequestException ex) 
      { 
       //handle or ignore 
      } 
     } 
+0

どうかありがとうございます。この例外を処理して、すぐにこの「キャッチ例外」でアップデートを受信し直すことはできますか? – Lionking89

+0

それはただ一つの例外を無視して、もう一度やり直すべきです(while(真)ループの先頭に戻ってください)現在、あなたのアプリケーションがタイムアウトを叩いてクラッシュしてループを終了していると思われます。それを再試行する必要がありますが、非同期例外は常に驚きを2回投げることができます。 – dannymc18

2

通常、デバッグにはgetUpdatesが使用されます。あなたのボットを公開するには、電文Webhookを使用する必要があります。 See this

getUpdates is a pull mechanism, setWebhook is push. There are some advantages of using a Webhook over getUpdates:

  1. Avoids your bot having to ask for updates frequently.
  2. Avoids the need for some kind of polling mechanism in your code.
関連する問題