1

Make bot more human-like by delaying answers

- Greet newcommers with notable randomized delay + send "typing", pretending we are real human :)
- Make 1-2 second delay in group chat before answer.
This commit is contained in:
eleksir
2020-10-10 14:17:25 +03:00
parent 0d0b8bb4c2
commit 9abb221dac
2 changed files with 30 additions and 1 deletions

View File

@@ -158,6 +158,17 @@ sub __on_msg {
$send_args->{text} = "Дратути, [$fullname](tg://user?id=$userid). Представьтес, пожалуйста, и расскажите, что вас сюда привело."; $send_args->{text} = "Дратути, [$fullname](tg://user?id=$userid). Представьтес, пожалуйста, и расскажите, что вас сюда привело.";
} }
# let's emulate real human and delay answer
sleep (int ( rand (3) + 1));
for (my $i = 0; $i <= (4 + int (rand (3))); $i++) {
sendChatAction($chatid);
sleep(3);
sleep 3 unless ($i);
}
sleep ( 3 + int ( rand (2)));
Telegram::Bot::Brain::sendMessage ($self, $send_args); Telegram::Bot::Brain::sendMessage ($self, $send_args);
return; return;
} }
@@ -360,6 +371,7 @@ sub __on_msg {
$reply = randomCommonPhrase(); $reply = randomCommonPhrase();
} }
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);
} else { } else {

View File

@@ -13,7 +13,7 @@ use Telegram::Bot::Brain;
# module shit # module shit
use vars qw/$VERSION/; use vars qw/$VERSION/;
use Exporter qw(import); use Exporter qw(import);
our @EXPORT = qw(getChat getChatMember visavi); our @EXPORT = qw(getChat getChatMember sendChatAction visavi);
$VERSION = "1.0"; $VERSION = "1.0";
@@ -52,6 +52,23 @@ sub getChatMember {
return $api_response; 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 Telegram::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 = '';