I created a Windows Forms application in C# that handles Telegram bot operations through the Telegram Bot API. The app worked perfectly before but now it crashes unexpectedly. When I debug it, I get this error: System.AggregateException occurred in mscorlib.dll pointing to this specific line: var updates = client.GetUpdatesAsync(lastUpdateId).Result;
Here’s my complete application code:
using System;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
namespace TelegramBotManager
{
public partial class MainForm : Form
{
private static string apiToken = "";
private Thread workerThread;
Telegram.Bot.TelegramBotClient client;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void btnActivate_Click(object sender, EventArgs e)
{
apiToken = txtApiToken.Text;
workerThread = new Thread(new ThreadStart(startBotService));
workerThread.Start();
}
void startBotService()
{
client = new Telegram.Bot.TelegramBotClient(apiToken);
this.Invoke(new Action(() =>
{
lblConnectionStatus.Text = "Connected";
lblConnectionStatus.ForeColor = Color.Green;
}));
int lastUpdateId = 0;
while (true)
{
var updates = client.GetUpdatesAsync(lastUpdateId).Result;
foreach (var update in updates)
{
lastUpdateId = update.Id + 1;
if (update.Message == null)
continue;
var messageText = update.Message.Text;
var user = update.Message.From;
var chatIdentifier = update.Message.Chat.Id;
if (messageText.Contains("/start"))
{
StringBuilder response = new StringBuilder();
response.AppendLine(user.Username + " Hello and welcome!");
client.SendTextMessageAsync(chatIdentifier, response.ToString());
}
}
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
workerThread.Abort();
}
}
}
I can’t figure out what’s causing this issue. I’ve searched online but haven’t found a working solution. Any help would be appreciated?
The detailed error information shows:
System.AggregateException was unhandled
HResult=-2146233088
Message=One or more errors occurred.
Source=mscorlib
InnerException: The request was aborted: Could not create SSL/TLS secure channel.