NAME

Perlgram::Update - Process Telegram updates with custom handlers

SYNOPSIS

use Perlgram;
use Perlgram::Update;

my $bot = Perlgram->new(token => 'YOUR_BOT_TOKEN');
my $update_handler = Perlgram::Update->new(
    bot => $bot,
    update => $update_data,
    handlers => {
        message => sub {
            my ($self, $message) = @_;
            my $chat_id = $message->{chat}{id};
            my $text = $message->{text} || '';
            $self->{bot}->sendMessage(
                chat_id => $chat_id,
                text => "Received: $text",
            );
        },
        callback_query => sub {
            my ($self, $callback_query) = @_;
            my $query_id = $callback_query->{id};
            $self->{bot}->answerCallbackQuery(
                callback_query_id => $query_id,
                text => 'Button clicked!',
            );
        },
    },
);
$update_handler->process();

# Alternatively, register handlers later
$update_handler->register_handler('inline_query', sub {
    my ($self, $inline_query) = @_;
    my $query_id = $inline_query->{id};
    $self->{bot}->answerInlineQuery(
        inline_query_id => $query_id,
        results => encode_json([{ type => 'article', id => '1', title => 'Custom', input_message_content => { message_text => 'Custom result' } }]),
    );
});

DESCRIPTION

Perlgram::Update processes Telegram update types, such as messages, inline queries, callback queries, and more. Users can define custom handlers for each update type via the constructor or register_handler. Default handlers are provided for some update types but can be overridden.

METHODS

new(bot => $bot, update => $update_data, [handlers => \%handlers])

Creates a new update processor. The handlers hash maps update types to callback functions.

register_handler($type, $callback)

Registers a callback function for a specific update type. The callback receives the update object and the update data.

process()

Processes the update by calling the appropriate handler based on the update type.

UPDATE TYPES

Supported update types include:

- message - edited_message - channel_post - edited_channel_post - inline_query - chosen_inline_result - callback_query - shipping_query - pre_checkout_query - poll - poll_answer - my_chat_member - chat_member - chat_join_request

AUTHOR

AmiRCandy, <amirhosen.1385.cmo@gmail.com>

LICENSE

Artistic License 2.0