0

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

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

用于键盘的电报 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 不存在。能做错什么?如何正确定义“键盘”字段?

4

2 回答 2

1

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

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

另请注意,分号是语句终止符,不能使用它代替逗号。

于 2015-09-09T09:48:26.860 回答
0
#!/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";
}

#这应该工作

于 2016-10-23T10:38:28.647 回答