First run of Teapot::Bot fixes.

* Fix typos in Brain.pm
* Fix typos in User.pm
* Implement typing methods in Message object and make use of it
* Fix incorrect syntax of unless loops in telegrambot.pm
* Add Log::Any as it required by Teapot::Bot
* Do not use --update in bootstrap.sh
* Add data/lat to .gitignore
* Load ChatMember and ChatPermissions in Message object, so we can use 'em and avoid crash
* Supply missing $send_args to _post_request
This commit is contained in:
eleksir
2020-10-30 17:19:40 +03:00
parent 43c0286414
commit a480786afa
7 changed files with 23 additions and 13 deletions

1
.gitignore vendored
View File

@@ -2,4 +2,5 @@ vendor_perl
data/config.json data/config.json
data/cache data/cache
data/telegrambot-brains data/telegrambot-brains
data/lat
TODO TODO

View File

@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
cpanm -l vendor_perl --self-contained --force --update --installdeps . cpanm -l vendor_perl --self-contained --force --installdeps .
# vim: set ft=sh noet ai ts=4 sw=4 sts=4: # vim: set ft=sh noet ai ts=4 sw=4 sts=4:

View File

@@ -1,3 +1,5 @@
requires 'Carp';
requires 'Log::Any';
requires 'Cwd::utf8'; requires 'Cwd::utf8';
requires 'DB_File'; requires 'DB_File';
requires 'Digest::MD5'; requires 'Digest::MD5';

View File

@@ -9,7 +9,7 @@ use warnings;
use Mojo::IOLoop; use Mojo::IOLoop;
use Mojo::UserAgent; use Mojo::UserAgent;
use Carp qw/croak, cluck, confess/; # use croak where we return error up to app that supply something wrong use Carp qw/croak cluck confess/; # use croak where we return error up to app that supply something wrong
# use cluck where we want to say that something bad but non-critical happen in # use cluck where we want to say that something bad but non-critical happen in
# lower layer (Mojo loop) # lower layer (Mojo loop)
# use confess where we want to say that fatal error happen in lower layer (Mojo loop) # use confess where we want to say that fatal error happen in lower layer (Mojo loop)
@@ -111,7 +111,7 @@ sub getChatMember {
my $token = $self->token || croak "no token?"; my $token = $self->token || croak "no token?";
my $url = "https://api.telegram.org/bot${token}/getChatMember"; my $url = "https://api.telegram.org/bot${token}/getChatMember";
my $api_response = $self->_post_request($url); my $api_response = $self->_post_request($url, $send_args);
if ($api_response) { if ($api_response) {
return Teapot::Bot::Object::ChatMember->create_from_hash($api_response, $self); return Teapot::Bot::Object::ChatMember->create_from_hash($api_response, $self);
@@ -133,7 +133,7 @@ sub getChat {
my $token = $self->token || croak "no token?"; my $token = $self->token || croak "no token?";
my $url = "https://api.telegram.org/bot${token}/getChat"; my $url = "https://api.telegram.org/bot${token}/getChat";
my $api_response = $self->_post_request($url); my $api_response = $self->_post_request($url, $send_args);
if ($api_response) { if ($api_response) {
return Teapot::Bot::Object::Chat->create_from_hash($api_response, $self); return Teapot::Bot::Object::Chat->create_from_hash($api_response, $self);

View File

@@ -5,6 +5,8 @@ use Mojo::Base 'Teapot::Bot::Object::Base';
use Teapot::Bot::Object::User; use Teapot::Bot::Object::User;
use Teapot::Bot::Object::Chat; use Teapot::Bot::Object::Chat;
use Teapot::Bot::Object::ChatMember;
use Teapot::Bot::Object::ChatPermissions;
use Teapot::Bot::Object::MessageEntity; use Teapot::Bot::Object::MessageEntity;
use Teapot::Bot::Object::Audio; use Teapot::Bot::Object::Audio;
use Teapot::Bot::Object::Document; use Teapot::Bot::Object::Document;
@@ -135,6 +137,11 @@ sub reply {
return $self->_brain->sendMessage({chat_id => $self->chat->id, text => $text}); return $self->_brain->sendMessage({chat_id => $self->chat->id, text => $text});
} }
sub typing {
my $self = shift;
return $self->_brain->sendChatAction({chat_id => $self->chat->id});
}
1; 1;
__END__ __END__

View File

@@ -12,9 +12,9 @@ has 'first_name';
has 'last_name'; # optional has 'last_name'; # optional
has 'username'; # optional has 'username'; # optional
has 'language_code'; # optional has 'language_code'; # optional
has 'can_join_groups' # optional, only for getMe() method has 'can_join_groups'; # optional, only for getMe() method
has 'can_read_all_group_messages' # optional, only for getMe() method has 'can_read_all_group_messages';# optional, only for getMe() method
has 'supports_inline_queries' # optional, only for getMe() method has 'supports_inline_queries'; # optional, only for getMe() method
sub fields { sub fields {
return { scalar => [qw/id is_bot first_name last_name username language_code can_join_groups return { scalar => [qw/id is_bot first_name last_name username language_code can_join_groups

View File

@@ -58,7 +58,7 @@ sub __on_msg {
unless ($myid) { unless ($myid) {
my $myObj = Teapot::Bot::Brain::getMe ($self); my $myObj = Teapot::Bot::Brain::getMe ($self);
unless ($myObj) do { unless ($myObj) {
sleep 3; sleep 3;
$myObj = Teapot::Bot::Brain::getMe ($self); $myObj = Teapot::Bot::Brain::getMe ($self);
} }
@@ -108,7 +108,7 @@ sub __on_msg {
if ($chatid < 0) { if ($chatid < 0) {
my $chatobj = Teapot::Bot::Brain::getChat ($self, { 'chat_id' => $chatid }); my $chatobj = Teapot::Bot::Brain::getChat ($self, { 'chat_id' => $chatid });
unless ($chatobj) do { unless ($chatobj) {
sleep 3; sleep 3;
$chatobj = Teapot::Bot::Brain::getChat ($self, { 'chat_id' => $chatid }); $chatobj = Teapot::Bot::Brain::getChat ($self, { 'chat_id' => $chatid });
} }
@@ -117,7 +117,7 @@ sub __on_msg {
$group_talk = int ($chatobj->{permissions}->{can_send_messages}); $group_talk = int ($chatobj->{permissions}->{can_send_messages});
my $me = Teapot::Bot::Brain::getChatMember ($self, { 'chat_id' => $chatid, 'user_id' => $myid }); my $me = Teapot::Bot::Brain::getChatMember ($self, { 'chat_id' => $chatid, 'user_id' => $myid });
unless ($me) do { unless ($me) {
sleep 3; sleep 3;
$me = Teapot::Bot::Brain::getChatMember ($self, { 'chat_id' => $chatid, 'user_id' => $myid }); $me = Teapot::Bot::Brain::getChatMember ($self, { 'chat_id' => $chatid, 'user_id' => $myid });
} }
@@ -182,7 +182,7 @@ sub __on_msg {
sleep (int ( rand (2) + 1)); sleep (int ( rand (2) + 1));
for (0..(4 + int (rand (3)))) { for (0..(4 + int (rand (3)))) {
Teapot::Bot::Brain::sendChatAction ($self, $chatid); $msg->typing();
sleep(3); sleep(3);
sleep 3 unless ($_); sleep 3 unless ($_);
} }
@@ -259,7 +259,7 @@ sub __on_msg {
} }
} }
Teapot::Bot::Brain::sendChatAction ($self, $chatid); $msg->typing();
sleep 1; sleep 1;
logger sprintf ("Private chat bot reply to $vis_a_vi: %s", $reply); logger sprintf ("Private chat bot reply to $vis_a_vi: %s", $reply);
$msg->reply ($reply); $msg->reply ($reply);
@@ -402,7 +402,7 @@ MYHELP
$reply = randomCommonPhrase(); $reply = randomCommonPhrase();
} }
Teapot::Bot::Brain::sendChatAction ($self, $chatid); $msg->typing();
sleep (int (rand (2))); sleep (int (rand (2)));
logger sprintf ('In public chat %s (%s) bot reply to %s: %s', $chatname, $chatid, $vis_a_vi, $reply); logger sprintf ('In public chat %s (%s) bot reply to %s: %s', $chatname, $chatid, $vis_a_vi, $reply);
$msg->reply ($reply); $msg->reply ($reply);