Maybedone?

This commit is contained in:
Gwyn 2026-03-04 14:13:31 -05:00
parent 8b4f86ec4a
commit 10c04fbaae
2 changed files with 8 additions and 39 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
.secrets.pm
*.secrets.pm
.env

View file

@ -1,43 +1,30 @@
#!/usr/bin/env perl #!/usr/bin/env perl
use strict; use strict;
use warnings; use warnings;
use POE; use POE;
use POE::Component::IRC; use POE::Component::IRC;
use WWW::Telegram::BotAPI; use WWW::Telegram::BotAPI;
# -------------------------------------------------
# Import secrets
# -------------------------------------------------
require './.secrets.pm'; require './.secrets.pm';
my $IRC_SERVER = '127.0.0.1'; my $IRC_SERVER = '127.0.0.1';
my $IRC_PORT = 6667; my $IRC_PORT = 6667;
my $IRC_NICK = 'Jerry'; my $IRC_NICK = 'Jerry';
my $IRC_CHANNEL = $Secrets::IRC_CHANNEL; my $IRC_CHANNEL = $Secrets::IRC_CHANNEL;
my $NICKSERV_PASSWORD = $Secrets::NICKSERV_PASSWORD; my $NICKSERV_PASSWORD = $Secrets::NICKSERV_PASSWORD;
# -------------------------------------------------
# Telegram Setup
# -------------------------------------------------
my $telegram = WWW::Telegram::BotAPI->new( my $telegram = WWW::Telegram::BotAPI->new(
token => $Secrets::TELEGRAM_TOKEN token => $Secrets::TELEGRAM_TOKEN
); );
my $allowed_user = $Secrets::TELEGRAM_USER_ID; my $myTele = $Secrets::TELEGRAM_USER_ID;
# Keep track of last update to avoid duplicates # We don't wanna double post the last thing we saw
my $last_update_id = 0; my $last_update_id = 0;
# -------------------------------------------------
# IRC Setup
# -------------------------------------------------
my $irc = POE::Component::IRC->spawn( my $irc = POE::Component::IRC->spawn(
Nick => $IRC_NICK, Nick => $IRC_NICK,
Server => $IRC_SERVER, Server => $IRC_SERVER,
Port => $IRC_PORT, Port => $IRC_PORT,
Ircname => 'Telegram Bridge Bot', Ircname => 'Jerry',
) or die "Cannot spawn IRC component\n"; ) or die "Cannot spawn IRC component\n";
POE::Session->create( POE::Session->create(
@ -57,16 +44,10 @@ POE::Session->create(
$poe_kernel->run(); $poe_kernel->run();
exit 0; exit 0;
# -------------------------------------------------
# POE Handlers
# -------------------------------------------------
sub _start { sub _start {
my ($kernel) = $_[KERNEL]; my ($kernel) = $_[KERNEL];
$irc->yield(register => 'all'); $irc->yield(register => 'all');
$irc->yield(connect => {}); $irc->yield(connect => {});
# Poll Telegram every 2 seconds # Poll Telegram every 2 seconds
$kernel->delay(poll_telegram => 2); $kernel->delay(poll_telegram => 2);
} }
@ -81,16 +62,13 @@ sub irc_public {
my ($who, $where, $what) = @_[ARG0, ARG1, ARG2]; my ($who, $where, $what) = @_[ARG0, ARG1, ARG2];
my $nick = (split /!/, $who)[0]; my $nick = (split /!/, $who)[0];
my $channel = $where->[0]; my $channel = $where->[0];
return unless $channel eq $IRC_CHANNEL; return unless $channel eq $IRC_CHANNEL;
send_telegram("[$nick] $what"); send_telegram("[$nick] $what");
} }
sub irc_msg { sub irc_msg {
my ($who, $what) = @_[ARG0, ARG2]; my ($who, $what) = @_[ARG0, ARG2];
my $nick = (split /!/, $who)[0]; my $nick = (split /!/, $who)[0];
send_telegram("[PM from $nick] $what"); send_telegram("[PM from $nick] $what");
} }
@ -98,43 +76,31 @@ sub irc_disconnected {
$irc->yield(connect => {}); $irc->yield(connect => {});
} }
# -------------------------------------------------
# Telegram Polling
# -------------------------------------------------
sub poll_telegram { sub poll_telegram {
my ($kernel) = $_[KERNEL]; my ($kernel) = $_[KERNEL];
my $updates = eval { $telegram->getUpdates({ offset => $last_update_id }) }; my $updates = eval { $telegram->getUpdates({ offset => $last_update_id }) };
if ($@) { if ($@) {
warn "Telegram poll failed: $@"; warn "Telegram poll failed: $@";
} elsif ($updates && $updates->{ok}) { } elsif ($updates && $updates->{ok}) {
for my $update (@{ $updates->{result} }) { for my $update (@{ $updates->{result} }) {
$last_update_id = $update->{update_id} + 1; $last_update_id = $update->{update_id} + 1;
next unless $update->{message}; next unless $update->{message};
my $msg = $update->{message}; my $msg = $update->{message};
# Only allow the specific user # Only allow the specific user
next unless $msg->{from}{id} == $allowed_user; next unless $msg->{from}{id} == $myTele;
my $text = $msg->{text} // ''; my $text = $msg->{text} // '';
next unless $text; next unless $text;
$irc->yield(privmsg => $IRC_CHANNEL => $text); $irc->yield(privmsg => $IRC_CHANNEL => $text);
} }
} }
$kernel->delay(poll_telegram => 2); $kernel->delay(poll_telegram => 2);
} }
# -------------------------------------------------
# Send message to Telegram
# -------------------------------------------------
sub send_telegram { sub send_telegram {
my ($text) = @_; my ($text) = @_;
eval { eval {
$telegram->sendMessage({ $telegram->sendMessage({
chat_id => $allowed_user, chat_id => $myTele,
text => $text, text => $text,
}); });
}; };