我有一个基于按下的内置按钮接收CallbackQuery callbackQuery的方法。基于接收到的数据,我想调用几个方法,我如何才能做到这一点呢?我目前的解决方案是丑陋的,因为如果有10个或更多的按钮,这等于10个条件。我的解决方案之一是字典,但由于调用多个方法,我无法实现它。请原谅我的英语,我的方法在下面:
private static async Task BotOnCallbackQueryReceived(ITelegramBotClient botClient, CallbackQuery callbackQuery)
{
string url = $"https://api.telegram.org/bot{Configuration.BotToken}/sendMessage?chat_id={Configuration.idPrivateChannelProgrammers}&text={$"{Configuration.textMessageToSend} @{callbackQuery.From.Username}."}";
if (callbackQuery.Data == "testString1")
{
SendsAMessageToUrlApiTelegramBot(url);
_ = SendsAMessageToTheUserAsync(botClient, callbackQuery);
}
if (callbackQuery.Data == "testString2")
{
//string url = $"https://api.telegram.org/bot{Configuration.BotToken}/sendMessage?chat_id={Configuration.idPrivateChannelItManager}&text={$"{Configuration.textMessageToSend} @{callbackQuery.From.Username}."}";
SendsAMessageToUrlApiTelegramBot(url);
_ = SendsAMessageToTheUserAsync(botClient, callbackQuery);
}
if (callbackQuery.Data == "HelpUser")
{
const string messageCallb = "testString3";
await botClient.SendTextMessageAsync(
chatId: callbackQuery.Message.Chat.Id,
text: $"{messageCallb}");
}
}
发布于 2022-05-21 12:14:25
正如解释过的这里一样,您可以在开关语句中使用字符串作为区分参数:
string str = "one";
// passing string "str" in
// switch statement
switch (str) {
case "one":
Console.WriteLine("It is 1");
break;
case "two":
Console.WriteLine("It is 2");
break;
default:
Console.WriteLine("Nothing");
break;
}
https://stackoverflow.com/questions/72329405
复制相似问题