Use Teapot' sendChatAction, getChat and getChatMember, remove dups of these subs in telegramlib.pm
This commit is contained in:
@@ -15,7 +15,7 @@ use Mojo::Base 'Teapot::Bot::Brain';
|
|||||||
|
|
||||||
use conf qw(loadConf);
|
use conf qw(loadConf);
|
||||||
use botlib qw(weather logger trim randomCommonPhrase);
|
use botlib qw(weather logger trim randomCommonPhrase);
|
||||||
use telegramlib qw(getChat getChatMember sendChatAction visavi);
|
use telegramlib qw(visavi);
|
||||||
use lat qw(latAnswer);
|
use lat qw(latAnswer);
|
||||||
|
|
||||||
use Exporter qw(import);
|
use Exporter qw(import);
|
||||||
@@ -57,6 +57,12 @@ sub __on_msg {
|
|||||||
|
|
||||||
unless ($myid) {
|
unless ($myid) {
|
||||||
my $myObj = Teapot::Bot::Brain::getMe ($self);
|
my $myObj = Teapot::Bot::Brain::getMe ($self);
|
||||||
|
|
||||||
|
unless ($myObj) do {
|
||||||
|
sleep 3;
|
||||||
|
$myObj = Teapot::Bot::Brain::getMe ($self);
|
||||||
|
}
|
||||||
|
|
||||||
$myid = $myObj->id;
|
$myid = $myObj->id;
|
||||||
# TODO: use these values instead of pre-defined in config!
|
# TODO: use these values instead of pre-defined in config!
|
||||||
$myusername = $myObj->username;
|
$myusername = $myObj->username;
|
||||||
@@ -100,12 +106,23 @@ sub __on_msg {
|
|||||||
|
|
||||||
# works only for public chats, so id shoud be < 0.
|
# works only for public chats, so id shoud be < 0.
|
||||||
if ($chatid < 0) {
|
if ($chatid < 0) {
|
||||||
my $chatobj = getChat ($self, $chatid);
|
my $chatobj = Teapot::Bot::Brain::getChat ($self, { 'chat_id' => $chatid });
|
||||||
|
|
||||||
|
unless ($chatobj) do {
|
||||||
|
sleep 3;
|
||||||
|
$chatobj = Teapot::Bot::Brain::getChat ($self, { 'chat_id' => $chatid });
|
||||||
|
}
|
||||||
|
|
||||||
# actually evaluates to 1 for true and to 0 for false
|
# actually evaluates to 1 for true and to 0 for false
|
||||||
$group_talk = int ($chatobj->{permissions}->{can_send_messages});
|
$group_talk = int ($chatobj->{permissions}->{can_send_messages});
|
||||||
my $chatObj = getChatMember ($self, $chatid, $myid);
|
my $me = Teapot::Bot::Brain::getChatMember ($self, { 'chat_id' => $chatid, 'user_id' => $myid });
|
||||||
|
|
||||||
if ($chatObj->{'status'} eq 'administrator') {
|
unless ($me) do {
|
||||||
|
sleep 3;
|
||||||
|
$me = Teapot::Bot::Brain::getChatMember ($self, { 'chat_id' => $chatid, 'user_id' => $myid });
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($me->{'status'} eq 'administrator') {
|
||||||
logger "I can talk in group chat $chatname ($chatid)";
|
logger "I can talk in group chat $chatname ($chatid)";
|
||||||
$can_talk = 1;
|
$can_talk = 1;
|
||||||
} else {
|
} else {
|
||||||
@@ -114,7 +131,7 @@ sub __on_msg {
|
|||||||
$can_talk = $group_talk;
|
$can_talk = $group_talk;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
# private chat, so definely can talk
|
# private chat, so definely bot can talk
|
||||||
logger "I can talk in private chat $chatname ($chatid)";
|
logger "I can talk in private chat $chatname ($chatid)";
|
||||||
$can_talk = 1;
|
$can_talk = 1;
|
||||||
}
|
}
|
||||||
@@ -165,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)))) {
|
||||||
sendChatAction ($self, $chatid);
|
Teapot::Bot::Brain::sendChatAction ($self, $chatid);
|
||||||
sleep(3);
|
sleep(3);
|
||||||
sleep 3 unless ($_);
|
sleep 3 unless ($_);
|
||||||
}
|
}
|
||||||
@@ -242,7 +259,7 @@ sub __on_msg {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendChatAction ($self, $chatid);
|
Teapot::Bot::Brain::sendChatAction ($self, $chatid);
|
||||||
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);
|
||||||
@@ -385,7 +402,7 @@ MYHELP
|
|||||||
$reply = randomCommonPhrase();
|
$reply = randomCommonPhrase();
|
||||||
}
|
}
|
||||||
|
|
||||||
sendChatAction ($self, $chatid);
|
Teapot::Bot::Brain::sendChatAction ($self, $chatid);
|
||||||
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);
|
||||||
|
|||||||
@@ -13,62 +13,10 @@ use Teapot::Bot::Brain;
|
|||||||
# module shit
|
# module shit
|
||||||
use vars qw/$VERSION/;
|
use vars qw/$VERSION/;
|
||||||
use Exporter qw(import);
|
use Exporter qw(import);
|
||||||
our @EXPORT_OK = qw(getChat getChatMember sendChatAction visavi);
|
our @EXPORT_OK = qw(visavi);
|
||||||
|
|
||||||
$VERSION = '1.0';
|
$VERSION = '1.0';
|
||||||
|
|
||||||
# complete framework with our getChat()
|
|
||||||
sub getChat {
|
|
||||||
my $self = shift;
|
|
||||||
my $id = shift;
|
|
||||||
my $send_args = {};
|
|
||||||
$send_args->{chat_id} = $id;
|
|
||||||
|
|
||||||
my $token = $self->token;
|
|
||||||
my $url = "https://api.telegram.org/bot${token}/getChat";
|
|
||||||
my $api_response = $self->_post_request ($url, $send_args);
|
|
||||||
|
|
||||||
# we seek for permissions, when making call to this subroutine, but
|
|
||||||
# framework does just omits it, when creating object from hash, so
|
|
||||||
# pick raw json object instead
|
|
||||||
|
|
||||||
#return Teapot::Bot::Object::User->create_from_hash($api_response, $self);
|
|
||||||
return $api_response;
|
|
||||||
}
|
|
||||||
|
|
||||||
# complete framework with our getChatMember()
|
|
||||||
sub getChatMember {
|
|
||||||
my $self = shift;
|
|
||||||
my $chatid = shift;
|
|
||||||
my $userid = shift;
|
|
||||||
my $send_args = {};
|
|
||||||
$send_args->{chat_id} = $chatid;
|
|
||||||
$send_args->{user_id} = $userid;
|
|
||||||
my $token = $self->token;
|
|
||||||
my $url = "https://api.telegram.org/bot${token}/getChatMember";
|
|
||||||
my $api_response = $self->_post_request ($url, $send_args);
|
|
||||||
|
|
||||||
# return Teapot::Bot::Object::User->create_from_hash($api_response, $self);
|
|
||||||
return $api_response;
|
|
||||||
}
|
|
||||||
|
|
||||||
# complete framework with our sendChatAction()
|
|
||||||
# sets bot status to "typing" for 5 seconds
|
|
||||||
# # TODO: more actions!
|
|
||||||
sub sendChatAction {
|
|
||||||
my $self = shift;
|
|
||||||
my $chatid = shift;
|
|
||||||
my $send_args = {};
|
|
||||||
$send_args->{chat_id} = $chatid;
|
|
||||||
$send_args->{action} = 'typing';
|
|
||||||
my $token = $self->token;
|
|
||||||
my $url = "https://api.telegram.org/bot${token}/sendChatAction";
|
|
||||||
my $api_response = $self->_post_request ($url, $send_args);
|
|
||||||
|
|
||||||
# return Teapot::Bot::Object::User->create_from_hash($api_response, $self);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub visavi {
|
sub visavi {
|
||||||
my ($userid, $username, $fullname) = @_;
|
my ($userid, $username, $fullname) = @_;
|
||||||
my $name = '';
|
my $name = '';
|
||||||
|
|||||||
Reference in New Issue
Block a user