EV-Telegram-TDLib

 view release on metacpan or  search on metacpan

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

package EV::Telegram::TDLib::Business;

use strict;
use warnings;
use Carp qw(croak);
use overload ();

our $VERSION = '0.04';

=head1 NAME

EV::Telegram::TDLib::Business - Telegram Business methods for EV::Telegram::TDLib

=head1 DESCRIPTION

One of the mixins L<EV::Telegram::TDLib> inherits from. It has no
interface of its own and is not meant to be used directly: loading the
main module loads this one, and its methods are called on a client.

They are documented together with the rest of the API, under
L<EV::Telegram::TDLib/"Business mixin">.

=cut

sub CLONE_SKIP { 1 }


# A business_connection_id arrives only through updateBusinessConnection, so
# without these handlers every method that takes one would be unreachable.
# edited and deleted business messages, and the quick-reply updates, carry no
# hook of their own and reach on_update like any other unhandled type
our %UPDATES = (
    updateBusinessConnection => \&update_connection,
    updateNewBusinessMessage => \&update_new_business_message,
);

sub update_connection {
    my ($self, $obj) = @_;
    if (my $cb = $self->{on_business_connection}) { $cb->($obj->{connection}) }
}

sub update_new_business_message {
    my ($self, $obj) = @_;
    if (my $cb = $self->{on_business_message}) {
        $cb->({ connection_id => $obj->{connection_id},
                message => $obj->{message} });
    }
}

sub on_business_connection {
    my ($self, $cb) = @_;
    $self->{on_business_connection} = $cb if @_ > 1;
    return $self->{on_business_connection};
}

sub on_business_message {
    my ($self, $cb) = @_;
    $self->{on_business_message} = $cb if @_ > 1;
    return $self->{on_business_message};
}

my @BOT_RIGHTS = qw(can_reply can_read_messages can_delete_sent_messages
                    can_delete_all_messages can_edit_name can_edit_bio
                    can_edit_profile_photo can_edit_username
                    can_view_gifts_and_stars can_sell_gifts
                    can_change_gift_settings can_transfer_and_upgrade_gifts
                    can_transfer_stars can_manage_stories);

my @RECIPIENT_FLAGS = qw(select_existing_chats select_new_chats select_contacts
                         select_non_contacts exclude_selected);

sub bot_rights {
    my ($r) = @_;
    $r ||= {};
    croak 'rights must be a hashref' unless ref $r eq 'HASH';
    for my $k (keys %$r) {
        croak "unknown bot right '$k'" unless grep { $_ eq $k } @BOT_RIGHTS;
    }
    return { '@type' => 'businessBotRights',
             map { $_ => json_bool($r->{$_}) } @BOT_RIGHTS };
}

sub recipients {
    my ($r) = @_;
    $r ||= {};
    croak 'recipients must be a hashref' unless ref $r eq 'HASH';
    for my $k (keys %$r) {
        next if $k eq 'chat_ids' || $k eq 'excluded_chat_ids';
        croak "unknown recipient option '$k'"
            unless grep { $_ eq $k } @RECIPIENT_FLAGS;
    }
    return {
        '@type'            => 'businessRecipients',
        chat_ids           => num_list('chat_ids', $r->{chat_ids} // []),
        excluded_chat_ids  => num_list('excluded_chat_ids',
                                       $r->{excluded_chat_ids} // []),
        map { $_ => json_bool($r->{$_}) } @RECIPIENT_FLAGS,
    };
}

sub away_schedule {
    my ($s) = @_;



( run in 0.429 second using v1.01-cache-2.11-cpan-57ff5998125 )