【发布时间】:2020-03-08 19:17:46
【问题描述】:
我使用 C# 编写了一个简单的 Windows 应用程序,用于使用 Telegram Bots API 管理 Telegram 机器人
以前它工作正常。但最近它不起作用并将自动关闭。当我在调试模式下运行它时,它说:An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll
并参考这一行:Telegram.Bot.Types.Update[] update = bot.GetUpdatesAsync(offset).Result;
我的应用程序的完整代码在这里:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace bot_test1
{
public partial class Form1 : Form
{
private static string Token = "";
private Thread botThread;
Telegram.Bot.TelegramBotClient bot;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnStart_Click(object sender, EventArgs e)
{
Token = txtToken.Text;
botThread = new Thread(new ThreadStart(runBot));
botThread.Start();
}
void runBot()
{
bot = new Telegram.Bot.TelegramBotClient(Token);
this.Invoke(new Action(() =>
{
lblStatus.Text = "Online";
lblStatus.ForeColor = Color.Green;
}));
int offset = 0;
while (true)
{
Telegram.Bot.Types.Update[] update = bot.GetUpdatesAsync(offset).Result;
foreach (var up in update)
{
offset = up.Id + 1;
if (up.Message == null)
continue;
var text = up.Message.Text;
var from = up.Message.From;
var chatId = up.Message.Chat.Id;
if (text.Contains("/start"))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(from.Username + " Welcome to this bot!");
bot.SendTextMessageAsync(chatId, sb.ToString());
}
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
botThread.Abort();
}
}
}
我找不到问题所在。我也搜索了网络,但找不到解决方案。你能指导我吗?
PS。错误的更多细节:
System.AggregateException was unhandled
HResult=-2146233088
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
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 bot_test1.Form1.runBot() in C:\Users\Person1\Documents\Visual Studio 2015\Projects\bot_test1\bot_test1\Form1.cs:line 47
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
HResult=-2146233088
Message=An error occurred while sending the request.
Source=mscorlib
StackTrace:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Telegram.Bot.TelegramBotClient.<MakeRequestAsync>d__54`1.MoveNext()
InnerException:
HResult=-2146233079
Message=The request was aborted: Could not create SSL/TLS secure channel.
Source=System
StackTrace:
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
InnerException:
【问题讨论】:
-
AggregateExceptions 只是其他异常的容器。错误消息中应该有更多的文字,你能提供吗?
-
您可能希望使用诸如wireshark 或fiddler 之类的嗅探器来捕获文本消息。我怀疑服务器未运行或您的令牌已过期。您能否使用 cmd.exe > Ping IP(或主机名)来 ping 服务器。尝试名称和 IP。
-
@jdweng 令牌未过期。我还将它替换为新机器人的新令牌。顺便说一句,我可以使用运行代码的系统连接 Telegram 网站。即使我更改系统并在任何计算机上都出现相同的错误。关于嗅探器,我不知道,但会搜索并尝试它们。
-
@BennoStraub 我编辑了我的帖子并在帖子中添加了有关错误的更多详细信息。能说说是什么问题吗?
-
HTTPS(安全)用于加密 TLS/SSL。 HTTP 使用 TCP 作为传输层,并且 TLS/SSL 在 TCP 消息上。所以你没有通过 TLS/SSL 加密。 TLS (1.0,1.1,1.2,1.3) 和 SSL (1.0,2.0,3.0) 有多种模式。你需要一个带有密钥的证书。我怀疑你在工作机器上有证书,但在远程机器上没有。
标签: c# updates telegram telegram-bot mscorlib