EV-Telegram-TDLib

 view release on metacpan or  search on metacpan

lib/EV/Telegram/TDLib/Bots.pm  view on Meta::CPAN

                  chat_id => 0 + $chat, message_id => 0 + $message_id,
                  payload => { '@type' => 'callbackQueryPayloadData',
                               data => tl_bytes('callback data', $data) } }, $cb);
    return;
}

sub inline_query {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    my ($bot, $query, @rest) = @args;
    my %opt = opts(@rest);
    need('bot_user_id, query', $bot, $query);
    $self->send({ '@type' => 'getInlineQueryResults', bot_user_id => 0 + $bot,
                  chat_id => num('chat_id', $opt{chat_id} // 0),
                  user_location => $opt{location},
                  query => plain_text('a query', $query),
                  offset => plain_text('an offset', $opt{offset}) }, $cb);
    return;
}

sub send_inline_result {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    my ($chat, $query_id, $result_id, @rest) = @args;
    my %opt = opts(@rest);
    need('chat_id, query_id, result_id', $chat, $query_id, $result_id);
    croak "send_inline_result cannot wait for delivery: it always returns "
        . "once Telegram accepts the message"
        if defined $opt{wait} && $opt{wait} ne 'accepted';
    my %req = ('@type' => 'sendInlineQueryResultMessage', chat_id => 0 + $chat,
               query_id => plain_text('a query id', $query_id),
               result_id => plain_text('a result id', $result_id),
               hide_via_bot => json_bool($opt{hide_via_bot}));
    $req{reply_to} = { '@type' => 'inputMessageReplyToMessage',
                       message_id => num('reply_to', $opt{reply_to}) }
        if $opt{reply_to};
    if (my $o = EV::Telegram::TDLib::Messages::send_options(\%opt)) { $req{options} = $o }
    if (my $t = EV::Telegram::TDLib::Messages::topic_id(\%opt))     { $req{topic_id} = $t }
    $self->send(\%req, $cb);
    return;
}

sub start_bot {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    my ($bot, $parameter, @rest) = @args;
    my %opt = opts(@rest);
    need('bot_user_id', $bot);
    $self->send({ '@type' => 'sendBotStartMessage', bot_user_id => 0 + $bot,
                  chat_id => num('chat_id', $opt{chat_id} // $bot),
                  parameter => plain_text('a parameter', $parameter) }, $cb);
    return;
}

sub attachment_menu_bot {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    no_extra('attachment_menu_bot', 1, \@args);
    my ($bot) = @args;
    need('bot_user_id', $bot);
    $self->send({ '@type' => 'getAttachmentMenuBot', bot_user_id => 0 + $bot }, $cb);
    return;
}

# openWebApp with an empty url needs the bot to be in the attachment menu
sub toggle_attachment_menu {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    my ($bot, $on, @rest) = @args;
    my %opt = opts(@rest);
    need('bot_user_id', $bot);
    $self->send({ '@type' => 'toggleBotIsAddedToAttachmentMenu',
                  bot_user_id => 0 + $bot,
                  is_added => json_bool(defined $on ? $on : 1),
                  allow_write_access => json_bool($opt{allow_write_access}) }, $cb);
    return;
}

# the user's answer to a request_chat / request_users keyboard button; the
# source identifies the message the button was on, and button_id is the id
# you gave that button when building the keyboard
sub button_source {
    my ($chat_id, $message_id) = @_;
    return { '@type' => 'keyboardButtonSourceMessage',
             chat_id => num('chat_id', $chat_id),
             message_id => num('message_id', $message_id) };
}

sub share_chat_with_bot {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    my ($chat_id, $message_id, $button_id, $shared_chat_id, @rest) = @args;
    my %opt = opts(@rest);
    need('chat_id, message_id, button_id, shared_chat_id',
          $chat_id, $message_id, $button_id, $shared_chat_id);
    $self->send({
        '@type'          => 'shareChatWithBot',
        source           => button_source($chat_id, $message_id),
        button_id        => 0 + $button_id,
        shared_chat_id   => 0 + $shared_chat_id,
        only_check       => json_bool($opt{check_only}),
    }, $cb);
    return;
}

sub share_users_with_bot {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    my ($chat_id, $message_id, $button_id, $user_ids, @rest) = @args;
    my %opt = opts(@rest);
    need('chat_id, message_id, button_id, user_ids',
          $chat_id, $message_id, $button_id, $user_ids);
    croak 'share_users_with_bot needs an arrayref of user ids'
        unless ref $user_ids eq 'ARRAY';
    $self->send({
        '@type'            => 'shareUsersWithBot',
        source             => button_source($chat_id, $message_id),
        button_id          => 0 + $button_id,
        shared_user_ids    => num_list('user_ids', $user_ids),
        only_check         => json_bool($opt{check_only}),
    }, $cb);
    return;
}

sub allow_bot_messages {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    no_extra('allow_bot_messages', 1, \@args);
    my ($bot) = @args;
    need('bot_user_id', $bot);
    $self->send({ '@type' => 'allowBotToSendMessages',
                  bot_user_id => 0 + $bot }, $cb);

lib/EV/Telegram/TDLib/Bots.pm  view on Meta::CPAN

    }, $cb);
    return;
}

sub game_high_scores {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    no_extra('game_high_scores', 3, \@args);
    my ($chat_id, $message_id, $user_id) = @args;
    need('chat_id, message_id, user_id', $chat_id, $message_id, $user_id);
    $self->send({ '@type' => 'getGameHighScores', chat_id => 0 + $chat_id,
                  message_id => 0 + $message_id, user_id => 0 + $user_id }, $cb);
    return;
}

sub set_inline_game_score {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    my ($inline_id, $user_id, $score, @rest) = @args;
    my %opt = opts(@rest);
    need('inline_message_id, user_id, score', $inline_id, $user_id, $score);
    $self->send({
        '@type'             => 'setInlineGameScore',
        inline_message_id   => plain_text('an inline message id', $inline_id),
        edit_message        => json_bool(exists $opt{edit} ? $opt{edit} : 1),
        user_id             => 0 + $user_id,
        score               => 0 + $score,
        force               => json_bool($opt{force}),
    }, $cb);
    return;
}

sub inline_game_high_scores {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    no_extra('inline_game_high_scores', 2, \@args);
    my ($inline_id, $user_id) = @args;
    need('inline_message_id, user_id', $inline_id, $user_id);
    $self->send({ '@type' => 'getInlineGameHighScores',
                  inline_message_id =>
                      plain_text('an inline message id', $inline_id),
                  user_id => 0 + $user_id }, $cb);
    return;
}


# the button beside the message box; a web_app url makes it open a Mini App
sub set_menu_button {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    my ($user_id, @rest) = @args;
    my %opt = opts(@rest);
    # TDLib reads a null button as the commands menu and an empty text with
    # the url "default" as Telegram's default; any other empty text is refused
    my $text = plain_text('a button text', $opt{text});
    # an empty text is refused unless the url is "default", so a url with no
    # text would silently put the default button back instead of failing
    croak 'set_menu_button needs a text for its url' if !length $text && $opt{url};
    my $button = $opt{commands} ? undef
        : length $text
        ? { '@type' => 'botMenuButton', text => $text,
            url => plain_text('a url', $opt{url}) }
        : { '@type' => 'botMenuButton', text => '', url => 'default' };
    $self->send({
        '@type'      => 'setMenuButton',
        user_id      => num('user_id', $user_id // 0),
        menu_button  => $button,
    }, $cb);
    return;
}

sub menu_button {
    my ($self, @args) = @_;
    my $cb = ref $args[-1] eq 'CODE' ? pop @args : sub {};
    no_extra('menu_button', 1, \@args);
    my ($user_id) = @args;
    $self->send({ '@type' => 'getMenuButton',
                  user_id => num('user_id', $user_id // 0) }, $cb);
    return;
}

1;



( run in 1.193 second using v1.01-cache-2.11-cpan-60884a8df76 )