Created
March 7, 2025 06:33
-
-
Save donma/d381a1911858be7b37f7ed8dfe6498b2 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) { | |
Console.WriteLine($"Received a message of type {message.Type}"); | |
_TelegramBotClient.SendMessage(message.Chat, $"Echo: Your Message Type is {message.Type}", replyMarkup: new ReplyKeyboardRemove()); | |
} | |
else if (text.StartsWith('/')) | |
{ | |
Console.WriteLine($"Command:" + message.Text); | |
_TelegramBotClient.SendMessage(message.Chat, $"Echo: Your Command is {message.Text}", replyMarkup: new ReplyKeyboardRemove()); | |
} | |
else | |
{ | |
Console.WriteLine($"純文字:" + message.Text); | |
_TelegramBotClient.SendMessage(message.Chat, $"Echo: Your Text is {message.Text}", replyMarkup: new ReplyKeyboardRemove()); | |
} | |
return Task.FromResult<object>(null); | |
} | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Test Telegram.Bot v22.4.4"); | |
InitTelegramBotClient(); | |
//因這是console 所以會這樣寫 | |
//如果你是 ASP.net 的服務可以寫在 Service End 的時候 | |
while (Console.ReadKey(true).Key != ConsoleKey.Escape) { | |
cts.Cancel(); // stop the bot | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment