Breaking API changes (handle Mojo loop errors), fix sendMessage method broken in 033441c

* Use general method to handle Mojo loop unsuccessful operations
* Do not die if tg api returns error via Mojo loop, lift up error status to upper layer
* Die with confess only in case of Mojo loop does not returns either is_success or is_error set
* Handle incomming updates related to poll event
* Update inline POD in Brain.pm
* Fix overlooked die removal in sendMessage, now it is croak
This commit is contained in:
eleksir
2020-10-30 15:42:36 +03:00
parent c1b6ca013d
commit 29c1fa8d07

View File

@@ -149,10 +149,10 @@ sub sendMessage {
my $args = shift || {}; my $args = shift || {};
my $send_args = {}; my $send_args = {};
croak "no chat_id supplied" unless $args->{chat_id}; croak "no chat_id supplied in sendMessage" unless $args->{chat_id};
$send_args->{chat_id} = $args->{chat_id}; $send_args->{chat_id} = $args->{chat_id};
croak "no text supplied" unless $args->{text}; croak "no text supplied in sendMessage" unless $args->{text};
$send_args->{text} = $args->{text}; $send_args->{text} = $args->{text};
# these are optional, send if they are supplied # these are optional, send if they are supplied
@@ -164,14 +164,14 @@ sub sendMessage {
# check reply_markup is the right kind # check reply_markup is the right kind
if (exists $args->{reply_markup}) { if (exists $args->{reply_markup}) {
my $reply_markup = $args->{reply_markup}; my $reply_markup = $args->{reply_markup};
croak "Incorrect reply_markup" if ( ref($reply_markup) ne 'Teapot::Bot::Object::InlineKeyboardMarkup' && croak "Incorrect reply_markup in sendMessage" if ( ref($reply_markup) ne 'Teapot::Bot::Object::InlineKeyboardMarkup' &&
ref($reply_markup) ne 'Teapot::Bot::Object::ReplyKeyboardMarkup' && ref($reply_markup) ne 'Teapot::Bot::Object::ReplyKeyboardMarkup' &&
ref($reply_markup) ne 'Teapot::Bot::Object::ReplyKeyboardRemove' && ref($reply_markup) ne 'Teapot::Bot::Object::ReplyKeyboardRemove' &&
ref($reply_markup) ne 'Teapot::Bot::Object::ForceReply' ); ref($reply_markup) ne 'Teapot::Bot::Object::ForceReply' );
$send_args->{reply_markup} = $reply_markup; $send_args->{reply_markup} = $reply_markup;
} }
my $token = $self->token || croak "no token?"; my $token = $self->token || croak "no token in sendMessage?";
my $url = "https://api.telegram.org/bot${token}/sendMessage"; my $url = "https://api.telegram.org/bot${token}/sendMessage";
my $api_response = $self->_post_request($url, $send_args); my $api_response = $self->_post_request($url, $send_args);
@@ -188,19 +188,19 @@ sub forwardMessage {
my $self = shift; my $self = shift;
my $args = shift || {}; my $args = shift || {};
my $send_args = {}; my $send_args = {};
croak "no chat_id supplied" unless $args->{chat_id}; croak "no chat_id supplied in forwardMessage" unless $args->{chat_id};
$send_args->{chat_id} = $args->{chat_id}; $send_args->{chat_id} = $args->{chat_id};
croak "no from_chat_id supplied" unless $args->{from_chat_id}; croak "no from_chat_id supplied in forwardMessage" unless $args->{from_chat_id};
$send_args->{from_chat_id} = $args->{from_chat_id}; $send_args->{from_chat_id} = $args->{from_chat_id};
croak "no message_id supplied" unless $args->{message_id}; croak "no message_id supplied in forwardMessage" unless $args->{message_id};
$send_args->{message_id} = $args->{message_id}; $send_args->{message_id} = $args->{message_id};
# these are optional, send if they are supplied # these are optional, send if they are supplied
$send_args->{disable_notification} = $args->{disable_notification} if exists $args->{disable_notification}; $send_args->{disable_notification} = $args->{disable_notification} if exists $args->{disable_notification};
my $token = $self->token || croak "no token?"; my $token = $self->token || croak "no token in forwardMessage?";
my $url = "https://api.telegram.org/bot${token}/forwardMessage"; my $url = "https://api.telegram.org/bot${token}/forwardMessage";
my $api_response = $self->_post_request($url, $send_args); my $api_response = $self->_post_request($url, $send_args);
@@ -225,7 +225,7 @@ sub sendPhoto {
# to fetch, or a file_id string) or a file on disk to upload - we need # to fetch, or a file_id string) or a file on disk to upload - we need
# to handle that last case here as it changes the way we create the HTTP # to handle that last case here as it changes the way we create the HTTP
# request # request
croak "no photo supplied" unless $args->{photo}; croak "no photo supplied in sendPhoto" unless $args->{photo};
if (-e $args->{photo}) { if (-e $args->{photo}) {
$send_args->{photo} = { photo => { file => $args->{photo} } }; $send_args->{photo} = { photo => { file => $args->{photo} } };
} }
@@ -233,7 +233,7 @@ sub sendPhoto {
$send_args->{photo} = $args->{photo}; $send_args->{photo} = $args->{photo};
} }
my $token = $self->token || croak "no token?"; my $token = $self->token || croak "no token in sendPhoto?";
my $url = "https://api.telegram.org/bot${token}/sendPhoto"; my $url = "https://api.telegram.org/bot${token}/sendPhoto";
my $api_response = $self->_post_request($url, $send_args); my $api_response = $self->_post_request($url, $send_args);
@@ -251,7 +251,7 @@ sub sendChatAction {
my $args = shift || {}; my $args = shift || {};
my $send_args = {}; my $send_args = {};
croak "no chat_id supplied" unless $args->{chat_id}; croak "no chat_id supplied in sendChatAction" unless $args->{chat_id};
$send_args->{chat_id} = $args->{chat_id}; $send_args->{chat_id} = $args->{chat_id};
if ($args->{action} eq 'upload_photo') { if ($args->{action} eq 'upload_photo') {
@@ -285,7 +285,7 @@ sub sendChatAction {
$send_args->{action} = 'typing'; $send_args->{action} = 'typing';
} }
my $token = $self->token || croak "no token?"; my $token = $self->token || croak "no token in sendChatAction?";
my $url = "https://api.telegram.org/bot${token}/sendChatAction"; my $url = "https://api.telegram.org/bot${token}/sendChatAction";
my $api_response = $self->_post_request ($url, $send_args); my $api_response = $self->_post_request ($url, $send_args);