skip to Main Content

I use WWW::Telegram::BotAPI (Perl implementation of the Telegram Bot API) for simple bot development.

I need create a custom keyboard (https://core.telegram.org/bots#keyboards) for reply (sendMessage method).

Telegram API for keyboards https://core.telegram.org/bots/api/#replykeyboardmarkup describe field ‘keyboard’ with type Array of Array of string.

Example:

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

But i make something wrong

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

in output dump – reply_markup not present. What wrong can do? How to define ‘keyboard’ field correctly?

2

Answers


  1. In a hash, all the values must be scalars. You can’t use a list as the value of keyboard. I’d try an anonymous array instead:

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

    Also note that semicolon is a statement terminator, you can’t use it instead of a comma.

    Login or Signup to reply.
  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."nn";
        my $response = $ua->get($completeUrl);
        my $content  = $response->decoded_content();
        my $resCode = $response->code();
    
    
        print "RESPONSE CODE $resCode n Content: $contentnn";
    }
    

    #That should Work

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search