From 9abb221daca74ea4aa634dd09bfe96f921c557c9 Mon Sep 17 00:00:00 2001 From: eleksir Date: Sat, 10 Oct 2020 14:17:25 +0300 Subject: [PATCH] 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. --- lib/telegrambot.pm | 12 ++++++++++++ lib/telegramlib.pm | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/telegrambot.pm b/lib/telegrambot.pm index 60d6544..ce48785 100644 --- a/lib/telegrambot.pm +++ b/lib/telegrambot.pm @@ -158,6 +158,17 @@ sub __on_msg { $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); return; } @@ -360,6 +371,7 @@ sub __on_msg { $reply = randomCommonPhrase(); } + 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); } else { diff --git a/lib/telegramlib.pm b/lib/telegramlib.pm index 529b148..e1cc501 100644 --- a/lib/telegramlib.pm +++ b/lib/telegramlib.pm @@ -13,7 +13,7 @@ use Telegram::Bot::Brain; # module shit use vars qw/$VERSION/; use Exporter qw(import); -our @EXPORT = qw(getChat getChatMember visavi); +our @EXPORT = qw(getChat getChatMember sendChatAction visavi); $VERSION = "1.0"; @@ -52,6 +52,23 @@ sub getChatMember { 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 (@) { my ($userid, $username, $fullname) = @_; my $name = '';