Compare commits
3 Commits
406679ae2e
...
36a200670c
Author | SHA1 | Date | |
---|---|---|---|
|
36a200670c | ||
|
87dcfb587f | ||
|
a42d6e91dd |
@ -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',
|
||||
@ -39,9 +39,7 @@ sub new {
|
||||
$Self->{LogLevel} = $Param{LogLevel} || 'info';
|
||||
$Self->{LogObject} = Log::Log4perl->get_logger('TelegramBot');
|
||||
$Self->{Token} = 'bot5868933096:AAE8Oe-AxU6m_yCWfpqTqwwjERqnRpBGJtE';
|
||||
$Self->{URL} = {
|
||||
Telegram => 'https://api.telegram.org',
|
||||
};
|
||||
$Self->{TelegramURL} = 'https://api.telegram.org';
|
||||
|
||||
# load remembered update ids
|
||||
$Self->{MessageIDs} = YAML::LoadFile("$FindBin::Bin/message_ids.yml");
|
||||
@ -49,6 +47,36 @@ sub new {
|
||||
# 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;
|
||||
}
|
||||
|
||||
@ -59,7 +87,6 @@ sub new {
|
||||
=cut
|
||||
|
||||
sub greet {
|
||||
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
$Self->{LogObject}->info('greet: Initiating greet routine');
|
||||
@ -89,6 +116,7 @@ sub greet {
|
||||
|
||||
=cut
|
||||
|
||||
# TODO rebuild to take custom parameters of various kind
|
||||
sub build {
|
||||
|
||||
my ( $Self, %Param ) = @_;
|
||||
@ -105,7 +133,7 @@ sub build {
|
||||
else {
|
||||
# for now, testing fallback
|
||||
my $ResponseResult = plain POST(
|
||||
join( '/', ( $Self->{URL}{Telegram}, $Self->{Token}, 'sendMessage' ) ),
|
||||
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},
|
||||
@ -118,6 +146,7 @@ sub build {
|
||||
|
||||
}
|
||||
else {
|
||||
# TODO load list of commands from core and plugins
|
||||
$KeyboardData = {
|
||||
'inline_keyboard' => [
|
||||
[
|
||||
@ -141,7 +170,7 @@ sub build {
|
||||
$KeyboardData,
|
||||
);
|
||||
my $ResponseResult = plain POST(
|
||||
join( '/', ( $Self->{URL}{Telegram}, $Self->{Token}, 'sendMessage' ) ),
|
||||
join( '/', ( $Self->{TelegramURL}, $Self->{Token}, 'sendMessage' ) ),
|
||||
{
|
||||
'chat_id' => $Param{Message}->{chat}->{id},
|
||||
'reply_to_message_id' => $Param{Message}->{id},
|
||||
@ -178,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,
|
||||
);
|
||||
@ -217,7 +248,7 @@ sub processMessage {
|
||||
}
|
||||
|
||||
my $ResponseResult = plain POST(
|
||||
join( '/', ( $Self->{URL}{Telegram}, $Self->{Token}, 'sendMessage' ) ),
|
||||
join( '/', ( $Self->{TelegramURL}, $Self->{Token}, 'sendMessage' ) ),
|
||||
{
|
||||
chat_id => $Param{Message}->{message}->{chat}->{id},
|
||||
$ResponseData->%*,
|
||||
@ -229,7 +260,7 @@ sub processMessage {
|
||||
|
||||
# mark message as read
|
||||
my $SeenResult = plain POST(
|
||||
join( '/', ( $Self->{URL}{Telegram}, $Self->{Token}, 'readMessageContents' ) ),
|
||||
join( '/', ( $Self->{TelegramURL}, $Self->{Token}, 'readMessageContents' ) ),
|
||||
{
|
||||
id => $Param{Message}->{message}->{id},
|
||||
}
|
||||
@ -253,7 +284,7 @@ sub fetchMessages {
|
||||
$Self->{LogObject}->info('fetchMessages: Initiating getUpdates');
|
||||
|
||||
my $MessageDataRaw =
|
||||
json GET( join( '/', ( $Self->{URL}{Telegram}, $Self->{Token}, $Method ) ), {} );
|
||||
json GET( join( '/', ( $Self->{TelegramURL}, $Self->{Token}, $Method ) ), {} );
|
||||
$Self->{LogObject}
|
||||
->info( 'fetchMessages: Messages raw are ' . Dumper($MessageDataRaw) );
|
||||
my @Messages = $MessageDataRaw->{result}->@*;
|
||||
|
Loading…
Reference in New Issue
Block a user