From f4c57e53db461ed7d4de2696dc701bcc7f45ff68 Mon Sep 17 00:00:00 2001 From: Rodger Castle Date: Thu, 4 Jun 2026 21:23:24 -0400 Subject: [PATCH] Fixed a couple message routing errors --- README.md | 7 +++++++ lib/Mnemosyne/Telegram.pm | 6 +++++- lib/Mnemosyne/Webhook.pm | 24 +++++++++++++++++------- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4de1a96..4574356 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,13 @@ Send `/today` to your bot. You should receive a digest (or an "all clear" messag ## AlmaLinux / RHEL-specific notes +**IPv6 not routed (common on Linode/VPS)** — if the bot logs "Connect timeout" when trying to reach `api.telegram.org`, IPv6 is configured on the interface but not actually routed. Fix by telling the system resolver to prefer IPv4: + +```bash +echo 'precedence ::ffff:0:0/96 100' | sudo tee -a /etc/gai.conf +sudo systemctl restart mnemosyne-bot +``` + **SELinux** — if nginx cannot connect to the local bot port, allow it: ```bash diff --git a/lib/Mnemosyne/Telegram.pm b/lib/Mnemosyne/Telegram.pm index 52c0845..1c7b9b4 100644 --- a/lib/Mnemosyne/Telegram.pm +++ b/lib/Mnemosyne/Telegram.pm @@ -206,7 +206,11 @@ sub _post { my $tx = $self->{_ua}->post( $url => { 'Content-Type' => 'application/json' } => encode_json($body) ); - my $res = $tx->result; + my $res = eval { $tx->result }; + if ($@) { + warn "Telegram connection error ($method): $@\n"; + return {}; + } unless ($res->is_success) { warn "Telegram API error ($method): " . $res->body . "\n"; } diff --git a/lib/Mnemosyne/Webhook.pm b/lib/Mnemosyne/Webhook.pm index 2933f76..f04231f 100644 --- a/lib/Mnemosyne/Webhook.pm +++ b/lib/Mnemosyne/Webhook.pm @@ -45,15 +45,22 @@ sub _handle_message { my ($msg, $chat_id, $db, $config, $telegram) = @_; my $text = $msg->{text} // ''; - # Wizard in progress — text input feeds into the current step - if (exists $SESSIONS{$chat_id} && $SESSIONS{$chat_id}{step} ne 'done') { - _wizard_text($text, $chat_id, $msg, $db, $config, $telegram); - return; - } - # Strip bot username suffix from commands (e.g. /help@MnemosyneBot) $text =~ s{^(/\w+)@\w+}{$1}; + # Wizard in progress — /cancel always escapes; other commands are blocked + if (exists $SESSIONS{$chat_id} && $SESSIONS{$chat_id}{step} ne 'done') { + if ($text =~ /^\/cancel/i) { + _cmd_cancel($chat_id, $telegram); + } elsif ($text =~ /^\//) { + $telegram->send_message($chat_id, + "A task wizard is in progress. Use /cancel to abort it first."); + } else { + _wizard_text($text, $chat_id, $msg, $db, $config, $telegram); + } + return; + } + my ($cmd, $args) = $text =~ /^(\/\w+)\s*(.*)/s ? ($1, $2) : ('', $text); if ($cmd eq '/today' || $cmd eq '/glance') { _cmd_today ($chat_id, $db, $config, $telegram) } @@ -417,7 +424,10 @@ sub _wizard_text { _wizard_confirm($chat_id, $wmid, $sess, $telegram); } else { - $telegram->send_message($chat_id, "Unexpected input. /cancel to abort."); + # Button-only steps (await_class, await_weekday, etc.) land here if + # the user types instead of tapping. Prompt them back to the buttons. + $telegram->send_message($chat_id, + "Please use the buttons above to continue, or /cancel to abort."); } }