Created
March 7, 2025 08:35
-
-
Save donma/c2c2c52b15a358322f65e1cd427ae689 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static TelegramBotClient _TelegramBotClient; | |
static CancellationTokenSource cts = new CancellationTokenSource(); | |
public static void InitTelegramBotClient() | |
{ | |
_TelegramBotClient = new TelegramBotClient("1234567890:AAD98dMandhVhEW4jDEu-LTm6fX9MYL9PKU4",httpClient:default,cts.Token); | |
_TelegramBotClient.OnMessage += _TelegramBotClient_OnMessage; | |
} | |
private static Task _TelegramBotClient_OnMessage(Telegram.Bot.Types.Message message, Telegram.Bot.Types.Enums.UpdateType type) | |
{ | |
if (message.Text is not { } text) | |
{ | |
_TelegramBotClient.SendMessage(message.Chat.Id, "抱歉我們只支持指令", replyMarkup: new ReplyKeyboardRemove()); | |
} | |
//設定 /menu 當作指令 | |
if (message.Text.ToLower().StartsWith("/menu")) | |
{ | |
InlineKeyboardMarkup inlineKeyboard = new(new[] | |
{ | |
new [] | |
{ | |
InlineKeyboardButton.WithCallbackData(text: "同意", callbackData: "agree:VoteA"), | |
InlineKeyboardButton.WithCallbackData(text: "不同意", callbackData: "deny:VoteA"), | |
} | |
}); | |
_TelegramBotClient.SendMessage( | |
//該用戶或是群組的 Id | |
chatId: message.Chat.Id, | |
text: message.From.FirstName + " 您好,請選擇你要使用的功能", | |
replyMarkup: inlineKeyboard, | |
cancellationToken: cts.Token); | |
} | |
else | |
{ | |
Console.WriteLine($"純文字:" + message.Text); | |
_TelegramBotClient.SendMessage(message.Chat, $"Echo: Your Text is {message.Text}", replyMarkup: new ReplyKeyboardRemove()); | |
} | |
return Task.FromResult<object>(null); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment