Telegram BOT in Perl

 

 

Implementazione in Perl delle API Telegram, con una pseudo autenticazione direttamente via chat.

#!/usr/bin/perl
use Data::Dumper;
use WWW::Telegram::BotAPI;
use syntax 'junction';
use autobox;
use File::Touch;

####################

my $authPassword = "password";

####################
# Dax 2017
# Note
# http://search.cpan.org/dist/WWW-Telegram-BotAPI/lib/WWW/Telegram/BotAPI.pm
# http://www.fileformat.info/info/emoji/list.htm
####################

my $api                 = WWW::Telegram::BotAPI->new (  token => 'my-token' );
my ($offset, $updates)  = 0;
my @autorizedIds        = ();
my $authStorage         = "telegramBotAuths";
my @doneMessages        = ('Done.', 'Fatto!');
my @helloMessages       = ('Ciao', 'Hei', chr(0x263a) );

sub ARRAY::contains { any( @{$_[0]} ) eq $_[1] };
sub ARRAY::getRandom { return @{$_[0]}[rand @{$_[0]}] };


#
#disable buffer
#
$|= 1;

#
# check auth storage
#
if( ! -e $authStorage ) {
        touch($authStorage);
}

#
# load auth storage
#
open my $f,"<:encoding(utf8)", $authStorage or die "
 Cannot open $authStorage $!
";
while( my $line = <$f> ){
        $line =~ s/
//g;
        $line =~ s/
//g;
        push(@autorizedIds, $line);
}

#
# main infinite loop
#
while (true) {

        $updates = $api->getUpdates ({ timeout => 30, $offset ? (offset => $offset) : () });
        unless ($updates and ref $updates eq "HASH" and $updates->{ok}) {
                warn "WARNING: getUpdates returned a false value - trying again...";
                next;
        }

        for my $u (@{$updates->{result}}) {
                $offset = $u->{update_id} + 1 if $u->{update_id} >= $offset;

                if (my $text = $u->{message}{text}) {
                        print Dumper($u);
                        #
                        # check auth
                        #
                        if( !@autorizedIds->contains( $u->{message}{from}{id} ) ){
                                #
                                # get auth
                                #
                                if( $text eq $authPassword ){
                                        $api->sendMessage ({ chat_id => $u->{message}{chat}{id}, text => $u->{message}{from}{first_name} .", you are welcome! ".chr(0x1F389) });
                                        push( @autorizedIds, $u->{message}{chat}{id} );
                                        #
                                        # save into storage the new item
                                        #
                                        open( my $fh, '>>', $authStorage ) or die "Error when write in $authStorage
";
                                        say $fh $u->{message}{from}{id};
                                        close $fh;
                                        next;
                                }

                                $api->sendMessage ({ chat_id => $u->{message}{chat}{id}, text => "You must first authenticate to chat with me ".chr(0x1F601) });
                                next;
                        }

                        #print $u->{message}{from}{first_name} . " write: $text
";

                        if(lc($text) eq "ciao"){
                                $api->sendMessage ({ chat_id => $u->{message}{chat}{id}, text => @helloMessages->getRandom() });
                                next;
                        }
                        if( lc($text) eq "uptime" ){
                                $api->sendMessage ({ chat_id => $u->{message}{chat}{id}, text => `uptime` });
                        }
                        
                }
        }
}

 

DaxTech 2024