首页 文章

一小时后,getRepdate()方法无法通过AggregateException工作

提问于
浏览
0

我有一些使用“bot.telegram”块的电报机器人C#应用程序有同样的问题 .

我通过getUpdate()方法获取用户消息和命令 . 下面有一个简单的例子:

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小时)之后它停止并且不从用户获得消息 . 在“捕获异常”中我得到此错误:

System.AggregateException:发生一个或多个错误 . ---> Telegram.Bot.Exceptions.ApiRequestException:请求超时---> System.Threading.Tasks.TaskCanceledException:任务被取消 . 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务),位于Telegram.Bot.TelegramBotClient.d__1251的System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) .MoveNext()---内部异常堆栈跟踪结束---在Telegram.Bot.TelegramBotClient . <SendWebRequestAsync> d__1251.MoveNext()---内部异常堆栈跟踪结束---在System.Threading.Tasks . 位于C:\ Users \ Soroush \ documents中mahramanehBot.Main.GetUpdates()的System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)上的System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)中的Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) \ visual studio 2015 \ Projects \ mahramanehBot \ mahramanehBot \ Main.cs:第45行--->(内部异常#0)Telegram.Bot.Exceptions.ApiRequestException:请求超时---> System.Threading.Tasks.TaskCanceledException:任务被取消了 . 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务),位于Telegram.Bot.TelegramBotClient.d__1251的System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) .MoveNext()---内部异常堆栈跟踪结束---在Telegram.Bot.TelegramBotClient . <SendWebRequestAsync> d__1251.MoveNext()<---

如何在不停止的情况下使“从用户那里获取消息和命令”工作?


我通过在“try”之后输入此代码解决了这个问题:

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

2 回答

  • 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
               }
            }
    
  • 2

    通常 getUpdates 用于调试 . 要发布机器人,您需要使用Telegram Webhook . See this .

    getUpdates是一个拉机制,setWebhook是push . 使用Webhook而不是getUpdates有一些优点:避免机器人经常要求更新 . 避免在代码中使用某种轮询机制 .

相关问题