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