如果构造,如何替换丑陋?
我有一种基于按压内置按钮的方法,可以接收 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}");
}
}
I have a method that receives a CallbackQuery callbackQuery based on the pressed built-in button.
Based on the received data, I want to call several methods
How can I do it right? The solution that I have at the moment is ugly, because if there are 10 or more buttons, this equals 10 conditionals. One of my solutions was a dictionary but I couldn't implement it due to calling multiple methods.
Excuse me for my english, my method would be at the bottom:
private static async Task BotOnCallbackQueryReceived(ITelegramBotClient botClient, CallbackQuery callbackQuery)
{
string url = quot;https://api.telegram.org/bot{Configuration.BotToken}/sendMessage?chat_id={Configuration.idPrivateChannelProgrammers}&text={quot;{Configuration.textMessageToSend} @{callbackQuery.From.Username}."}";
if (callbackQuery.Data == "testString1")
{
SendsAMessageToUrlApiTelegramBot(url);
_ = SendsAMessageToTheUserAsync(botClient, callbackQuery);
}
if (callbackQuery.Data == "testString2")
{
//string url = quot;https://api.telegram.org/bot{Configuration.BotToken}/sendMessage?chat_id={Configuration.idPrivateChannelItManager}&text={quot;{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: quot;{messageCallb}");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如解释在这里,您可以在开关语句中使用字符串作为区别参数:
As explained here, you can use a string as distinction argument in a switch statement: