Teapot lib: Implement ability to delete message in chat
This commit is contained in:
@@ -254,6 +254,44 @@ sub forwardMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# TODO: Check that message is no older than 48 hrs
|
||||||
|
sub deleteMessage {
|
||||||
|
my $self = shift;
|
||||||
|
my $args = shift || {};
|
||||||
|
my $send_args = {};
|
||||||
|
|
||||||
|
unless ($args->{chat_id}) {
|
||||||
|
cluck 'No chat_id supplied in deleteMessage()';
|
||||||
|
return {'error' => 1};
|
||||||
|
}
|
||||||
|
|
||||||
|
$send_args->{chat_id} = $args->{chat_id};
|
||||||
|
|
||||||
|
unless ($args->{message_id}) {
|
||||||
|
cluck 'No message_id supplied in deleteMessage()';
|
||||||
|
return {'error' => 1};
|
||||||
|
}
|
||||||
|
|
||||||
|
$send_args->{message_id} = $args->{message_id};
|
||||||
|
|
||||||
|
my $token = $self->token || croak 'No token supplied to deleteMessage()?';
|
||||||
|
|
||||||
|
if (Teapot::Bot::Object::ChatMember::canDelete($self, $chatid)) {
|
||||||
|
my $url = "https://api.telegram.org/bot${token}/deleteMessage";
|
||||||
|
my $api_response = $self->_post_request($url, $send_args);
|
||||||
|
|
||||||
|
if ($api_response) {
|
||||||
|
return Teapot::Bot::Object::Message->create_from_hash($api_response, $self);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return {'error' => 1};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return {'error' => 1};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
sub sendPhoto {
|
sub sendPhoto {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
@@ -571,6 +609,14 @@ Returns a L<Teapot::Bot::Object::Message> object.
|
|||||||
|
|
||||||
On error returns hash reference with error set to 1
|
On error returns hash reference with error set to 1
|
||||||
|
|
||||||
|
=head2 deleteMessage
|
||||||
|
|
||||||
|
See L<https://core.telegram.org/bots/api#deletemessage>.
|
||||||
|
|
||||||
|
Returns a L<Teapot::Bot::Object::Message> object.
|
||||||
|
|
||||||
|
On error returns hash reference with error set to 1
|
||||||
|
|
||||||
=head2 sendPhoto
|
=head2 sendPhoto
|
||||||
|
|
||||||
See L<https://core.telegram.org/bots/api#sendphoto>.
|
See L<https://core.telegram.org/bots/api#sendphoto>.
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package Teapot::Bot::Object::ChatMember;
|
|||||||
|
|
||||||
use Mojo::Base 'Teapot::Bot::Object::Base';
|
use Mojo::Base 'Teapot::Bot::Object::Base';
|
||||||
use Teapot::Bot::Object::User;
|
use Teapot::Bot::Object::User;
|
||||||
|
use Carp qw(cluck);
|
||||||
|
|
||||||
$Teapot::Bot::Object::ChatMember::VERSION = '0.022';
|
$Teapot::Bot::Object::ChatMember::VERSION = '0.022';
|
||||||
|
|
||||||
@@ -38,6 +39,59 @@ sub fields {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
sub canDeleteMessage {
|
||||||
|
my $self = shift;
|
||||||
|
my $chatid = shift;
|
||||||
|
|
||||||
|
my $can_delete = 0;
|
||||||
|
|
||||||
|
if ($chatid < 0) {
|
||||||
|
# group chat
|
||||||
|
my $chatobj = $self->_brain->getChat ({ 'chat_id' => $chatid });
|
||||||
|
|
||||||
|
# on api error, keep silence
|
||||||
|
unless ($chatobj) {
|
||||||
|
cluck "Unable to get chat info for $chatid from telegram API";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
my $myObj = $self->_brain->getMe ();
|
||||||
|
|
||||||
|
# on api error, keep silence
|
||||||
|
unless ($chatobj) {
|
||||||
|
cluck "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) {
|
||||||
|
cluck 'Unable to get chat info for bot itself from telegram API';
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
# bot can delete msg if it is group and it is admin there.
|
||||||
|
if ($chatobj->{'type'} eq 'group') {
|
||||||
|
if ($me->{'status'} eq 'administrator') {
|
||||||
|
$can_delete = 1;
|
||||||
|
}
|
||||||
|
# in supergroup bot must be granted with
|
||||||
|
} elsif ($chatobj->{'type'} eq 'group') || ($chatobj->{'type'} eq 'channel')
|
||||||
|
if ($me->{'can_delete_messages'}) {
|
||||||
|
$can_delete = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
# 1 on 1 chat with user
|
||||||
|
$can_delete = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $can_delete;
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
||||||
__END__
|
__END__
|
||||||
@@ -60,6 +114,14 @@ The base class for Telegram 'ChatMember' type objects.
|
|||||||
See L<https://core.telegram.org/bots/api#chatmember> for details of the
|
See L<https://core.telegram.org/bots/api#chatmember> for details of the
|
||||||
attributes available for L<Teapot::Bot::Object::ChatMember> objects.
|
attributes available for L<Teapot::Bot::Object::ChatMember> objects.
|
||||||
|
|
||||||
|
=head1 METHODS
|
||||||
|
|
||||||
|
=head2 canDeleteMessage
|
||||||
|
|
||||||
|
A convenience method to check if bot can delete message in given conversation.
|
||||||
|
|
||||||
|
Returns true if the bot can delete the message in given conversation, otherwise false.
|
||||||
|
|
||||||
=head1 AUTHOR
|
=head1 AUTHOR
|
||||||
|
|
||||||
Sergei Fedosov <eleksir@gmail.com>
|
Sergei Fedosov <eleksir@gmail.com>
|
||||||
|
|||||||
Reference in New Issue
Block a user