Add replyMd() to message object, implement canTalk() check in Teapot lib, re-arrange code
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package Teapot::Bot::Object::ChatPermissions;
|
||||
# ABSTRACT: The base class for Telegram 'ChatPermissions' type objects
|
||||
|
||||
use Carp qw(carp);
|
||||
use Mojo::Base 'Teapot::Bot::Object::Base';
|
||||
|
||||
$Teapot::Bot::Object::ChatPermissions::VERSION = '0.022';
|
||||
@@ -28,6 +28,53 @@ sub fields {
|
||||
};
|
||||
}
|
||||
|
||||
sub canTalk {
|
||||
my $self = shift;
|
||||
my $chatid = shift;
|
||||
|
||||
my $can_talk = 0;
|
||||
|
||||
if ($chatid < 0) {
|
||||
# group chat
|
||||
my $chatobj = $self->_brain->getChat ({ 'chat_id' => $chatid });
|
||||
|
||||
# on api error, keep silence
|
||||
unless ($chatobj) {
|
||||
carp "Unable to get chat info for $chatid from telegram API";
|
||||
return 0;
|
||||
}
|
||||
|
||||
my $myObj = $self->_brain->getMe ();
|
||||
|
||||
# on api error, keep silence
|
||||
unless ($chatobj) {
|
||||
carp "Unable to get chat info for $chatid from telegram API";
|
||||
return 0;
|
||||
}
|
||||
|
||||
my $myid = $myObj->id;
|
||||
my $me = $self->_brain->getChatMember ({ 'chat_id' => $chatid, 'user_id' => $myid });
|
||||
|
||||
# on api error, keep silence
|
||||
unless ($me) {
|
||||
carp 'Unable to get chat info for bot itself from telegram API';
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ($me->{'status'} eq 'administrator') {
|
||||
$can_talk = 1;
|
||||
} else {
|
||||
my $group_talk = int ($chatobj->{permissions}->{can_send_messages});
|
||||
$can_talk = $group_talk;
|
||||
}
|
||||
} else {
|
||||
# 1 on 1 chat with user
|
||||
$can_talk = 1;
|
||||
}
|
||||
|
||||
return $can_talk;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
@@ -29,6 +29,7 @@ use Teapot::Bot::Object::PassportData;
|
||||
use Teapot::Bot::Object::InlineKeyboardMarkup;
|
||||
|
||||
use Data::Dumper;
|
||||
use Carp qw(cluck);
|
||||
|
||||
$Teapot::Bot::Object::Message::VERSION = '0.022';
|
||||
|
||||
@@ -134,12 +135,44 @@ sub arrays {
|
||||
sub reply {
|
||||
my $self = shift;
|
||||
my $text = shift;
|
||||
return $self->_brain->sendMessage({chat_id => $self->chat->id, text => $text});
|
||||
|
||||
my $chatid = $self->chat->id;
|
||||
|
||||
if (Teapot::Bot::Object::ChatPermissions::canTalk($self, $chatid)) {
|
||||
return $self->_brain->sendMessage({chat_id => $chatid, text => $text});
|
||||
}
|
||||
|
||||
return {'error' => 1};
|
||||
}
|
||||
|
||||
sub replyMd {
|
||||
my $self = shift;
|
||||
my $text = shift;
|
||||
|
||||
my $chatid = $self->chat->id;
|
||||
|
||||
if (Teapot::Bot::Object::ChatPermissions::canTalk ($self, $chatid)) {
|
||||
my $send_args;
|
||||
$send_args->{text} = $text;
|
||||
$send_args->{parse_mode} = 'Markdown';
|
||||
$send_args->{chat_id} = $chatid;
|
||||
|
||||
return $self->_brain->sendMessage($send_args);
|
||||
}
|
||||
|
||||
return {'error' => 1};
|
||||
}
|
||||
|
||||
sub typing {
|
||||
my $self = shift;
|
||||
return $self->_brain->sendChatAction({chat_id => $self->chat->id});
|
||||
|
||||
my $chatid = $self->chat->id;
|
||||
|
||||
if (Teapot::Bot::Object::ChatPermissions::canTalk ($self, $chatid)) {
|
||||
return $self->_brain->sendChatAction({chat_id => $chatid});
|
||||
}
|
||||
|
||||
return {'error' => 1};
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
Reference in New Issue
Block a user