-
Notifications
You must be signed in to change notification settings - Fork 0
/
Telegram.cs
57 lines (50 loc) · 1.98 KB
/
Telegram.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace TelegramBuildBot;
internal static class Telegram
{
public static async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
// Only process Message updates: https://core.telegram.org/bots/api#message
if (update.Message is not { } message)
return;
// Only process text messages
if (message.Text is not { } messageText)
return;
long chatId = message.Chat.Id;
Console.WriteLine($"Received a '{messageText}' message in chat {chatId} on Telegram.");
string? responseMsg = await Program.Builder.ProcessMessage(messageText);
if (responseMsg != null)
{
Console.WriteLine($"Chat response:\n{responseMsg}");
try
{
bool isTopic = message.IsTopicMessage ?? false;
await botClient.SendTextMessageAsync(
chatId: chatId,
text: responseMsg,
parseMode: ParseMode.MarkdownV2,
messageThreadId: isTopic ? message.MessageThreadId : null,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
}
catch (Exception ex)
{
Console.WriteLine($"Unable to send msg: {ex.Message}");
}
}
}
public static Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
string errorMessage = exception switch
{
ApiRequestException apiRequestException
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}] {apiRequestException.Message}",
_ => exception.ToString()
};
Console.WriteLine(errorMessage);
return Task.CompletedTask;
}
}