AggregateException 几个小时后 getUpdate() 方法不起作用
getUpdate() method does not work after some hour by AggregateException
我有一些使用 "bot.telegram" nugget 的电报机器人 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 小时)它停止并且没有收到来自 user.In 的消息 "catch exception" 我收到此错误:
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__1251.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.Task1.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__1251.MoveNext()
--- End of inner exception stack trace ---
at Telegram.Bot.TelegramBotClient.<SendWebRequestAsync>d__125
1.MoveNext()<---
我应该怎么做才能让 "getting messages and command from user" 不停地工作?
我通过将此代码放在 "try":
之后解决了这个问题
catch (AggregateException e)
{
Thread a = new Thread(GetUpdates);
a.Start();
}
更新可能会超时,您需要处理这个问题 - 我假设应用程序崩溃了?
捕获异常并处理结果(很可能是忽略它)。您可能还需要等待结果。
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
}
}
通常getUpdates
用于调试。要发布您的机器人,您需要使用 Telegram Webhook
。 See this.
getUpdates is a pull mechanism, setWebhook is push.
There are some advantages of using a Webhook over getUpdates:
- Avoids your bot having to ask for updates frequently.
- Avoids the need for some kind of polling mechanism in your code.
我有一些使用 "bot.telegram" nugget 的电报机器人 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 小时)它停止并且没有收到来自 user.In 的消息 "catch exception" 我收到此错误:
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.Task1.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__1251.MoveNext() --- End of inner exception stack trace --- at Telegram.Bot.TelegramBotClient.<SendWebRequestAsync>d__125
1.MoveNext()<---
我应该怎么做才能让 "getting messages and command from user" 不停地工作?
我通过将此代码放在 "try":
之后解决了这个问题 catch (AggregateException e)
{
Thread a = new Thread(GetUpdates);
a.Start();
}
更新可能会超时,您需要处理这个问题 - 我假设应用程序崩溃了?
捕获异常并处理结果(很可能是忽略它)。您可能还需要等待结果。
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
}
}
通常getUpdates
用于调试。要发布您的机器人,您需要使用 Telegram Webhook
。 See this.
getUpdates is a pull mechanism, setWebhook is push. There are some advantages of using a Webhook over getUpdates:
- Avoids your bot having to ask for updates frequently.
- Avoids the need for some kind of polling mechanism in your code.