Compare commits
No commits in common. "main" and "issue-#1-plugin_base" have entirely different histories.
main
...
issue-#1-p
126
TelegramBot.pm
126
TelegramBot.pm
@ -47,29 +47,18 @@ sub new {
|
||||
# whitelist
|
||||
$Self->{Whitelist} = $Param{Whitelist};
|
||||
|
||||
# default command list
|
||||
$Self->{CommandList} = {
|
||||
'greet' => \&greet,
|
||||
};
|
||||
|
||||
# default query trigger
|
||||
$Self->{QueryTrigger} = {
|
||||
'startQuery' => 1,
|
||||
};
|
||||
|
||||
$Self->{Data} = {};
|
||||
|
||||
# load plugins
|
||||
# include plugins
|
||||
my @Plugins = glob("$FindBin::Bin/Plugins/*");
|
||||
|
||||
# iterate over plugins found
|
||||
if ( @Plugins ) {
|
||||
use File::Basename;
|
||||
PLUGIN:
|
||||
for my $PluginPath ( @Plugins ) {
|
||||
my $PluginName = basename($PluginPath);
|
||||
next PLUGIN unless require("$FindBin::Bin/Plugins/${PluginName}/Core.pm");
|
||||
|
||||
my $FullPath = "Plugins::${PluginName}::Core";
|
||||
my $PluginObject = $FullPath->new;
|
||||
if( !$PluginObject ) {
|
||||
@ -85,22 +74,6 @@ sub new {
|
||||
|
||||
$Self->{CommandList}{$PluginCommand} = $PluginCommandList->{$PluginCommand};
|
||||
}
|
||||
|
||||
# import plugin query trigger
|
||||
my $PluginQueryTriggerList = $PluginObject->getQueryTrigger();
|
||||
QUERYTRIGGER:
|
||||
for my $PluginQueryTrigger ( keys $PluginQueryTriggerList->%* ) {
|
||||
next QUERYTRIGGER if $Self->{QueryTrigger}{$PluginQueryTrigger};
|
||||
|
||||
$Self->{QueryTrigger}{$PluginQueryTrigger} = $PluginQueryTriggerList->{$PluginQueryTrigger};
|
||||
}
|
||||
|
||||
# import module data
|
||||
my $PluginData = $PluginObject->getData();
|
||||
$Self->{Data} = {
|
||||
$Self->{Data}->%*,
|
||||
$PluginData->%*,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -129,9 +102,9 @@ sub greet {
|
||||
$Self->{LogObject}->debug( 'Random Greet is ' . $Greet );
|
||||
my $ReturnContent =
|
||||
$Greet . ', '
|
||||
. ( $Param{Message}{chat}{first_name}
|
||||
? $Param{Message}{chat}{first_name}
|
||||
: $Param{Message}{chat}{username} )
|
||||
. ( $Param{Message}->{chat}->{first_name}
|
||||
? $Param{Message}->{chat}->{first_name}
|
||||
: $Param{Message}->{chat}->{username} )
|
||||
. '!';
|
||||
$Self->{LogObject}->debug( 'ReturnContent is ' . $ReturnContent );
|
||||
return { text => $ReturnContent, };
|
||||
@ -145,19 +118,15 @@ sub greet {
|
||||
|
||||
# TODO rebuild to take custom parameters of various kind
|
||||
sub build {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
my ( $Self, %Param ) = @_;
|
||||
use Data::Dumper;
|
||||
use JSON;
|
||||
use LWP::Simple::REST qw(POST plain);
|
||||
|
||||
my $KeyboardData;
|
||||
my $KeyboardMessage;
|
||||
if ( $Param{KeyboardData} ) {
|
||||
$KeyboardData = $Param{KeyboardData};
|
||||
$KeyboardMessage = $Param{KeyboardMessage} // '';
|
||||
}
|
||||
elsif ( $Param{QueryStep} ) {
|
||||
if ( $Param{QueryStep} ) {
|
||||
if ( $Param{QueryStep} eq 'hist' ) {
|
||||
# show next selection
|
||||
}
|
||||
@ -166,8 +135,8 @@ sub build {
|
||||
my $ResponseResult = plain POST(
|
||||
join( '/', ( $Self->{TelegramURL}, $Self->{Token}, 'sendMessage' ) ),
|
||||
{
|
||||
'chat_id' => $Param{Message}{callback_query}{from}{id},
|
||||
'reply_to_message_id' => $Param{Message}{callback_query}{message}{message_id},
|
||||
'chat_id' => $Param{Message}->{callback_query}->{from}->{id},
|
||||
'reply_to_message_id' => $Param{Message}->{callback_query}->{message}->{message_id},
|
||||
'text' => "Kommando $Param{QueryStep} erkannt",
|
||||
}
|
||||
);
|
||||
@ -177,16 +146,17 @@ sub build {
|
||||
|
||||
}
|
||||
else {
|
||||
# TODO load list of commands from core and plugins
|
||||
$KeyboardData = {
|
||||
'inline_keyboard' => [
|
||||
[
|
||||
{
|
||||
'text' => 'Grüßen',
|
||||
'callback_data' => 'greet',
|
||||
'callback_data' => 'greet'
|
||||
},
|
||||
{
|
||||
'text' => 'Statistik',
|
||||
'callback_data' => 'statistics',
|
||||
'callback_data' => 'statistics'
|
||||
},
|
||||
],
|
||||
],
|
||||
@ -194,20 +164,18 @@ sub build {
|
||||
'single_use' => 1,
|
||||
'placeholder' => 'test',
|
||||
};
|
||||
$KeyboardMessage = "Hallo $Param{Message}{message}{chat}{first_name}, über die folgenden Fragen kannst du auswählen, welche Interaktion du ausführen möchtest. Was möchtest du tun?";
|
||||
$KeyboardMessage = "Hallo $Param{Message}->{chat}->{first_name}, über die folgenden Fragen kannst du auswählen, welche Interaktion du ausführen möchtest. Was möchtest du tun?";
|
||||
}
|
||||
|
||||
# keyboard encoding
|
||||
my $EncodedKeyboard = JSON::to_json(
|
||||
my $EncodedKeyboard = JSON::encode_json(
|
||||
$KeyboardData,
|
||||
);
|
||||
my $ResponseResult = plain POST(
|
||||
join( '/', ( $Self->{TelegramURL}, $Self->{Token}, 'sendMessage' ) ),
|
||||
{
|
||||
'chat_id' => $Param{Message}{message}{chat}{id} || $Param{Message}{chat}{id},
|
||||
'reply_to_message_id' => $Param{Message}{message}{message_id} || $Param{Message}{message_id},
|
||||
'chat_id' => $Param{Message}->{chat}->{id},
|
||||
'reply_to_message_id' => $Param{Message}->{id},
|
||||
'text' => $KeyboardMessage,
|
||||
'reply_markup' => $EncodedKeyboard,
|
||||
'reply_markup' => $EncodedKeyboard,
|
||||
}
|
||||
);
|
||||
$Self->{LogObject}->info( 'build: Sending result is ' . $ResponseResult );
|
||||
@ -223,8 +191,8 @@ sub build {
|
||||
=cut
|
||||
|
||||
sub processMessage {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
my ( $Self, %Param ) = @_;
|
||||
use Data::Dumper;
|
||||
use LWP::Simple::REST qw(POST plain json);
|
||||
|
||||
@ -237,15 +205,13 @@ sub processMessage {
|
||||
$Self->{LogObject}
|
||||
->info( 'processMessage: Message is ' . Dumper( $Param{Message} ) );
|
||||
|
||||
my $ResponseData = {};
|
||||
my $CommandList = join('|', keys $Self->{CommandList}->%*);
|
||||
my $QueryTriggerList = join('|', keys $Self->{QueryTrigger}->%*);
|
||||
|
||||
# command branch
|
||||
if ( defined $Param{Message}{message} && $Param{Message}{message}{text} && $Param{Message}{message}{text} =~
|
||||
my $ResponseData = {};
|
||||
my $CommandList = join('|', keys $Self->{CommandList}->%*);
|
||||
if ( defined $Param{Message}->{message} && $Param{Message}->{message}->{text} =~
|
||||
/\/(?<command>$CommandList)\s?(?<arguments>.*)?/ )
|
||||
{
|
||||
my $Message = $Param{Message}{message};
|
||||
my $Message = $Param{Message}->{message};
|
||||
my $Command = $+{command};
|
||||
my $ArgumentsString = $+{arguments};
|
||||
$ResponseData = $Self->{CommandList}{$Command}(
|
||||
@ -257,40 +223,25 @@ sub processMessage {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# query trigger branch
|
||||
elsif ( defined $Param{Message}{message} && $Param{Message}{message}{text} && $Param{Message}{message}{text} =~ /\/(?<trigger>$QueryTriggerList)/ ) {
|
||||
my $Subname = $+{trigger};
|
||||
$Self->$Subname(
|
||||
elsif ( defined $Param{Message}->{callback_query} ) {
|
||||
$Self->build(
|
||||
Message => $Param{Message},
|
||||
QueryStep => $Param{Message}->{callback_query}->{data},
|
||||
);
|
||||
}
|
||||
|
||||
# query result branch
|
||||
elsif ( defined $Param{Message}{callback_query} ) {
|
||||
my $ArgumentsString = '';
|
||||
$ResponseData = $Self->{CommandList}{$Param{Message}{callback_query}{data}}(
|
||||
$Self,
|
||||
Message => $Param{Message}{callback_query}{message},
|
||||
Arguments => $ArgumentsString,
|
||||
);
|
||||
if ( !keys $ResponseData->%* ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$Self->{LogObject}
|
||||
->debug( 'Command not recognized. Data: ' . $Param{Message}{message}{text} );
|
||||
if ( 0 && $Self->{Whitelist}{$Param{Message}{message}{from}{id}} eq 'Sarah'
|
||||
|| $Self->{Whitelist}{$Param{Message}{message}{from}{id}} eq 'Stefan' ) {
|
||||
# $ResponseData = $Self->replyLoveQuote( Message => $Param{Message}{message} );
|
||||
->debug( 'Command not recognized. Data: ' . $Param{Message}->{message}->{text} );
|
||||
if ( $Self->{Whitelist}->{$Param{Message}->{message}->{from}->{id}} eq 'Sarah'
|
||||
|| $Self->{Whitelist}->{$Param{Message}->{message}->{from}->{id}} eq 'Stefan' ) {
|
||||
$ResponseData = $Self->replyLoveQuote( Message => $Param{Message}->{message} );
|
||||
}
|
||||
else {
|
||||
$ResponseData->{text} =
|
||||
"I'm sorry, "
|
||||
. ( $Param{Message}{message}{chat}{first_name}
|
||||
? $Param{Message}{message}{chat}{first_name}
|
||||
: $Param{Message}{message}{chat}{username} )
|
||||
. ( $Param{Message}->{message}->{chat}->{first_name}
|
||||
? $Param{Message}->{message}->{chat}->{first_name}
|
||||
: $Param{Message}->{message}->{chat}->{username} )
|
||||
. ", I couldn't understand your request. Currently I can process the commands:\n\n\t\/greet\n\t\/statistics driver standings\n\t\/statistics constructor standings";
|
||||
}
|
||||
|
||||
@ -299,7 +250,7 @@ sub processMessage {
|
||||
my $ResponseResult = plain POST(
|
||||
join( '/', ( $Self->{TelegramURL}, $Self->{Token}, 'sendMessage' ) ),
|
||||
{
|
||||
chat_id => $Param{Message}{message}{chat}{id} || $Param{Message}{callback_query}{message}{chat}{id},
|
||||
chat_id => $Param{Message}->{message}->{chat}->{id},
|
||||
$ResponseData->%*,
|
||||
}
|
||||
);
|
||||
@ -311,7 +262,7 @@ sub processMessage {
|
||||
my $SeenResult = plain POST(
|
||||
join( '/', ( $Self->{TelegramURL}, $Self->{Token}, 'readMessageContents' ) ),
|
||||
{
|
||||
id => $Param{Message}{message}{id},
|
||||
id => $Param{Message}->{message}->{id},
|
||||
}
|
||||
);
|
||||
|
||||
@ -324,6 +275,7 @@ sub processMessage {
|
||||
=cut
|
||||
|
||||
sub fetchMessages {
|
||||
|
||||
my ( $Self, %Param ) = @_;
|
||||
use Data::Dumper;
|
||||
use LWP::Simple::REST qw(GET json);
|
||||
@ -341,22 +293,20 @@ sub fetchMessages {
|
||||
|
||||
MESSAGE:
|
||||
for my $Message (@Messages) {
|
||||
my $MessageID = $Message->{message}{message_id} || $Message->{callback_query}{message}{message_id};
|
||||
my $UserID = $Message->{message}{from}{id} || $Message->{callback_query}{message}{chat}{id};
|
||||
if ( !$Self->{Whitelist}{$UserID} ) {
|
||||
if ( !$Self->{Whitelist}{$Message->{message}{from}{id}} ) {
|
||||
$Self->{LogObject}
|
||||
->info( 'fetchMessages: User not whitelisted, skipping message ' . Dumper($MessageDataRaw) );
|
||||
|
||||
$Self->{MessageIDs}{$MessageID} = 1;
|
||||
$Self->{MessageIDs}{$Message->{message}{message_id}} = 1;
|
||||
next MESSAGE;
|
||||
}
|
||||
if ($Self->{MessageIDs}{$MessageID}) {
|
||||
if ($Self->{MessageIDs}{$Message->{message}{message_id}}) {
|
||||
$Self->{LogObject}->info('fetchMessages: Skipping known message_id');
|
||||
next MESSAGE;
|
||||
}
|
||||
else {
|
||||
$Self->{LogObject}->info('fetchMessages: Calling processMessage');
|
||||
$Self->{MessageIDs}{$MessageID} = 1;
|
||||
$Self->{MessageIDs}{$Message->{message}{message_id}} = 1;
|
||||
$Self->processMessage( Message => $Message, );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user