【问题标题】:Perl, Telegram, WWW::Telegram::BotAPI how send array of array?Perl, Telegram, WWW::Telegram::BotAPI 如何发送数组数组?
【发布时间】:2015-09-09 09:34:48
【问题描述】:

我使用 WWW::Telegram::BotAPI(Telegram Bot API 的 Perl 实现)进行简单的机器人开发。

我需要创建一个自定义键盘 (https://core.telegram.org/bots#keyboards) 用于回复(sendMessage 方法)。

用于键盘的 Telegram API https://core.telegram.org/bots/api/#replykeyboardmarkup 描述字段“键盘”,其类型为字符串数组数组。

例子:

my @buttons=(['one','two'],['three','four'],['five']);

但我做错了

print Dumper $api->SendMessage
                    ({
                    chat_id => $from_id,
                    text    => 'question text ?',
                    reply_to_message_id => $message_id,
                    reply_markup => {
                                    keyboard =>  (['one','two'],['three','four'],['five']);
                                    resize_keyboard => 1,
                                    one_time_keyboard => 1
                                    }
                    });

在输出转储中 - reply_markup 不存在。能做错什么?如何正确定义“键盘”字段?

【问题讨论】:

    标签: json perl telegram telegram-bot


    【解决方案1】:

    在散列中,所有值都必须是标量。您不能使用列表作为keyboard 的值。我会尝试使用匿名数组:

    { keyboard => [ [ 'one', 'two' ], [ 'three', 'four' ], [ 'five' ] ],
      resize_keyboard => ...
    

    还要注意分号是语句终止符,不能用它代替逗号。

    【讨论】:

    • 是匿名数组引用,不是匿名数组。
    • @simbabque:好的,但是由于没有引用就无法实现匿名数组,因此我使用了较短的术语。
    • 这也是真的。
    【解决方案2】:
    #!/usr/bin/perl
    
    #libs
    use JSON;
    
    #telegram Reply Menus
    
    my $telegramEndPoint = "https://ip+token/sendMessage";
    my $textMessage = "My Keyboard";
    my $chat_id = 12345;
    
    #create a hash of your reply markup
    my %replyMarkup  = (
            keyboard    => [[ "One", "Two" ]]
            );#your keyboard must be an array of array
    
    #Json Encode them
    my $buttons = encode_json \%replyMarkup;
    sendTelegram($telegramEndPoint,$textMessage,$chat_id,$replyMarkup)
    
    sub sendTelegramMenus{
        #usage
        #sendTelegram($telegramEndPoint,$textMessage,$chat_id,$replyMarkup)
        my(@values) = @_;
        my $telegramEndPoint = $values[0];
        my $textMessage = $values[1];
        my $chat_id = $values[2];
        my $replyMarkup = $values[3];
    
        my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
        my $completeUrl =  $telegramEndPoint.'chat_id='.$chat_id.'&text='.$textMessage.'&reply_markup='.$replyMarkup;
        print "URL: ".$completeUrl."\n\n";
        my $response = $ua->get($completeUrl);
        my $content  = $response->decoded_content();
        my $resCode = $response->code();
    
    
        print "RESPONSE CODE $resCode \n Content: $content\n\n";
    }
    

    #应该可以的

    【讨论】:

      猜你喜欢
      • 2015-12-01
      • 1970-01-01
      • 2018-01-19
      • 2021-08-07
      • 2017-04-30
      • 2016-04-06
      • 1970-01-01
      • 2020-10-09
      • 2015-09-20
      相关资源
      最近更新 更多