diff --git a/.gitignore b/.gitignore index 5390ce2..15278e7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ vendor_perl data/config.json data/cache data/telegrambot-brains +data/lat TODO \ No newline at end of file diff --git a/bootstrap.sh b/bootstrap.sh index fe78a4f..eb5740a 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,5 +1,5 @@ #!/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: diff --git a/cpanfile b/cpanfile index 055ec37..3b5db6d 100644 --- a/cpanfile +++ b/cpanfile @@ -1,3 +1,5 @@ +requires 'Carp'; +requires 'Log::Any'; requires 'Cwd::utf8'; requires 'DB_File'; requires 'Digest::MD5'; diff --git a/lib/Teapot/Bot/Brain.pm b/lib/Teapot/Bot/Brain.pm index 61e1103..1f76661 100644 --- a/lib/Teapot/Bot/Brain.pm +++ b/lib/Teapot/Bot/Brain.pm @@ -9,7 +9,7 @@ use warnings; use Mojo::IOLoop; 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 # 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 $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) { 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 $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) { return Teapot::Bot::Object::Chat->create_from_hash($api_response, $self); diff --git a/lib/Teapot/Bot/Object/Message.pm b/lib/Teapot/Bot/Object/Message.pm index ef69855..b9876ce 100644 --- a/lib/Teapot/Bot/Object/Message.pm +++ b/lib/Teapot/Bot/Object/Message.pm @@ -5,6 +5,8 @@ use Mojo::Base 'Teapot::Bot::Object::Base'; use Teapot::Bot::Object::User; use Teapot::Bot::Object::Chat; +use Teapot::Bot::Object::ChatMember; +use Teapot::Bot::Object::ChatPermissions; use Teapot::Bot::Object::MessageEntity; use Teapot::Bot::Object::Audio; use Teapot::Bot::Object::Document; @@ -135,6 +137,11 @@ sub reply { 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; __END__ diff --git a/lib/Teapot/Bot/Object/User.pm b/lib/Teapot/Bot/Object/User.pm index 755cd77..a7ff9b4 100644 --- a/lib/Teapot/Bot/Object/User.pm +++ b/lib/Teapot/Bot/Object/User.pm @@ -12,9 +12,9 @@ has 'first_name'; has 'last_name'; # optional has 'username'; # optional has 'language_code'; # optional -has 'can_join_groups' # 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 'can_join_groups'; # 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 sub fields { return { scalar => [qw/id is_bot first_name last_name username language_code can_join_groups diff --git a/lib/telegrambot.pm b/lib/telegrambot.pm index b31da04..94bf9e6 100644 --- a/lib/telegrambot.pm +++ b/lib/telegrambot.pm @@ -58,7 +58,7 @@ sub __on_msg { unless ($myid) { my $myObj = Teapot::Bot::Brain::getMe ($self); - unless ($myObj) do { + unless ($myObj) { sleep 3; $myObj = Teapot::Bot::Brain::getMe ($self); } @@ -108,7 +108,7 @@ sub __on_msg { if ($chatid < 0) { my $chatobj = Teapot::Bot::Brain::getChat ($self, { 'chat_id' => $chatid }); - unless ($chatobj) do { + unless ($chatobj) { sleep 3; $chatobj = Teapot::Bot::Brain::getChat ($self, { 'chat_id' => $chatid }); } @@ -117,7 +117,7 @@ sub __on_msg { $group_talk = int ($chatobj->{permissions}->{can_send_messages}); my $me = Teapot::Bot::Brain::getChatMember ($self, { 'chat_id' => $chatid, 'user_id' => $myid }); - unless ($me) do { + unless ($me) { sleep 3; $me = Teapot::Bot::Brain::getChatMember ($self, { 'chat_id' => $chatid, 'user_id' => $myid }); } @@ -182,7 +182,7 @@ sub __on_msg { sleep (int ( rand (2) + 1)); for (0..(4 + int (rand (3)))) { - Teapot::Bot::Brain::sendChatAction ($self, $chatid); + $msg->typing(); sleep(3); sleep 3 unless ($_); } @@ -259,7 +259,7 @@ sub __on_msg { } } - Teapot::Bot::Brain::sendChatAction ($self, $chatid); + $msg->typing(); sleep 1; logger sprintf ("Private chat bot reply to $vis_a_vi: %s", $reply); $msg->reply ($reply); @@ -402,7 +402,7 @@ MYHELP $reply = randomCommonPhrase(); } - Teapot::Bot::Brain::sendChatAction ($self, $chatid); + $msg->typing(); sleep (int (rand (2))); logger sprintf ('In public chat %s (%s) bot reply to %s: %s', $chatname, $chatid, $vis_a_vi, $reply); $msg->reply ($reply);