Enabled importing of plugin commands.
This commit is contained in:
		| @@ -1,4 +1,4 @@ | ||||
| # Important for switch feature | ||||
| # important for switch feature | ||||
| use v5.32; | ||||
|  | ||||
| use utf8; | ||||
| @@ -17,10 +17,10 @@ use JSON; | ||||
| use Log::Log4perl; | ||||
| use YAML; | ||||
|  | ||||
| # Package name | ||||
| # package name | ||||
| package TelegramBot; | ||||
|  | ||||
| # Constants and initalisations | ||||
| # constants and initalisations | ||||
| Log::Log4perl->init("$FindBin::Bin/log.conf"); | ||||
| my $CACHE = Cache::FastMmap->new( | ||||
|     share_file => '/tmp/telegram_bot', | ||||
| @@ -47,6 +47,10 @@ sub new { | ||||
|     # whitelist | ||||
|     $Self->{Whitelist} = $Param{Whitelist}; | ||||
|  | ||||
|     $Self->{CommandList} = { | ||||
|         'greet' => \&greet, | ||||
|     }; | ||||
|  | ||||
|     # include plugins | ||||
|     my @Plugins = glob("$FindBin::Bin/Plugins/*"); | ||||
|     if ( @Plugins ) { | ||||
| @@ -54,11 +58,22 @@ sub new { | ||||
|         PLUGIN: | ||||
|         for my $PluginPath ( @Plugins ) { | ||||
|             my $PluginName = basename($PluginPath); | ||||
| 		my $PluginObject = require("$FindBin::Bin/Plugins/${PluginName}/Core.pm"); | ||||
|             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}; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @@ -72,7 +87,6 @@ sub new { | ||||
| =cut | ||||
|  | ||||
| sub greet { | ||||
|  | ||||
|     my ( $Self, %Param ) = @_; | ||||
|  | ||||
|     $Self->{LogObject}->info('greet: Initiating greet routine'); | ||||
| @@ -102,6 +116,7 @@ sub greet { | ||||
|  | ||||
| =cut | ||||
|  | ||||
| # TODO rebuild to take custom parameters of various kind | ||||
| sub build { | ||||
|  | ||||
|     my ( $Self, %Param ) = @_; | ||||
| @@ -131,6 +146,7 @@ sub build { | ||||
|  | ||||
|     } | ||||
|     else { | ||||
|         # TODO load list of commands from core and plugins | ||||
|         $KeyboardData = { | ||||
|             'inline_keyboard' => [ | ||||
|                 [ | ||||
| @@ -191,13 +207,15 @@ sub processMessage { | ||||
|  | ||||
|  | ||||
|     my $ResponseData = {}; | ||||
|     my $CommandList  = join('|', keys $Self->{CommandList}->%*); | ||||
|     if ( defined $Param{Message}->{message} && $Param{Message}->{message}->{text} =~ | ||||
|         /\/(?<command>greet|statistics|build)\s?(?<arguments>.*)?/ ) | ||||
|         /\/(?<command>$CommandList)\s?(?<arguments>.*)?/ ) | ||||
|     { | ||||
|         my $Message = $Param{Message}->{message}; | ||||
|         my $Command         = $+{command}; | ||||
|         my $ArgumentsString = $+{arguments}; | ||||
|         $ResponseData = $Self->$Command( | ||||
|         $ResponseData = $Self->{CommandList}{$Command}( | ||||
|             $Self, | ||||
|             Message   => $Message, | ||||
|             Arguments => $ArgumentsString, | ||||
|         ); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user