如果构造,如何替换丑陋?

发布于 2025-01-31 04:27:59 字数 1523 浏览 3 评论 0原文

我有一种基于按压内置按钮的方法,可以接收 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶舞 2025-02-07 04:28:00

如解释在这里,您可以在开关语句中使用字符串作为区别参数:

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;
}

As explained here, you can use a string as distinction argument in a switch statement:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文