Compare commits

..

No commits in common. "36a200670c2030527632dcaf8e69c59b4015b78c" and "a42d6e91ddb9ce5f471eb9f497691f59f6945c6c" have entirely different histories.

View File

@ -1,4 +1,4 @@
# important for switch feature # Important for switch feature
use v5.32; use v5.32;
use utf8; use utf8;
@ -17,10 +17,10 @@ use JSON;
use Log::Log4perl; use Log::Log4perl;
use YAML; use YAML;
# package name # Package name
package TelegramBot; package TelegramBot;
# constants and initalisations # Constants and initalisations
Log::Log4perl->init("$FindBin::Bin/log.conf"); Log::Log4perl->init("$FindBin::Bin/log.conf");
my $CACHE = Cache::FastMmap->new( my $CACHE = Cache::FastMmap->new(
share_file => '/tmp/telegram_bot', share_file => '/tmp/telegram_bot',
@ -47,36 +47,6 @@ sub new {
# whitelist # whitelist
$Self->{Whitelist} = $Param{Whitelist}; $Self->{Whitelist} = $Param{Whitelist};
$Self->{CommandList} = {
'greet' => \&greet,
};
# include plugins
my @Plugins = glob("$FindBin::Bin/Plugins/*");
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 ) {
$Self->{LogObject}->error("Couldn't load plugin $PluginName");
next PLUGIN;
}
# import plugin command list
my $PluginCommandList = $PluginObject->getCommandList();
COMMAND:
for my $PluginCommand ( keys $PluginCommandList->%* ) {
next COMMAND if $Self->{CommandList}{$PluginCommand};
$Self->{CommandList}{$PluginCommand} = $PluginCommandList->{$PluginCommand};
}
}
}
return $Self; return $Self;
} }
@ -87,6 +57,7 @@ sub new {
=cut =cut
sub greet { sub greet {
my ( $Self, %Param ) = @_; my ( $Self, %Param ) = @_;
$Self->{LogObject}->info('greet: Initiating greet routine'); $Self->{LogObject}->info('greet: Initiating greet routine');
@ -116,7 +87,6 @@ sub greet {
=cut =cut
# TODO rebuild to take custom parameters of various kind
sub build { sub build {
my ( $Self, %Param ) = @_; my ( $Self, %Param ) = @_;
@ -146,7 +116,6 @@ sub build {
} }
else { else {
# TODO load list of commands from core and plugins
$KeyboardData = { $KeyboardData = {
'inline_keyboard' => [ 'inline_keyboard' => [
[ [
@ -207,15 +176,13 @@ sub processMessage {
my $ResponseData = {}; my $ResponseData = {};
my $CommandList = join('|', keys $Self->{CommandList}->%*);
if ( defined $Param{Message}->{message} && $Param{Message}->{message}->{text} =~ if ( defined $Param{Message}->{message} && $Param{Message}->{message}->{text} =~
/\/(?<command>$CommandList)\s?(?<arguments>.*)?/ ) /\/(?<command>greet|statistics|build)\s?(?<arguments>.*)?/ )
{ {
my $Message = $Param{Message}->{message}; my $Message = $Param{Message}->{message};
my $Command = $+{command}; my $Command = $+{command};
my $ArgumentsString = $+{arguments}; my $ArgumentsString = $+{arguments};
$ResponseData = $Self->{CommandList}{$Command}( $ResponseData = $Self->$Command(
$Self,
Message => $Message, Message => $Message,
Arguments => $ArgumentsString, Arguments => $ArgumentsString,
); );