52 lines
1.5 KiB
Perl
Executable File
52 lines
1.5 KiB
Perl
Executable File
#!/usr/bin/env perl
|
|
use strict;
|
|
use warnings;
|
|
use FindBin qw($RealBin);
|
|
use lib "$RealBin/../lib";
|
|
|
|
# One-shot helper to register or remove the Telegram webhook.
|
|
# Run once after install, and again whenever the webhook URL changes.
|
|
# Only one webhook can be active per bot token at a time.
|
|
#
|
|
# Usage:
|
|
# mnemosyne-webhook [--config /path] [--delete]
|
|
|
|
use Mnemosyne::Config;
|
|
use Mnemosyne::Telegram;
|
|
|
|
my $config_path = "$RealBin/../config/mnemosyne.conf";
|
|
my $do_delete = 0;
|
|
|
|
while (@ARGV) {
|
|
my $arg = shift @ARGV;
|
|
if ($arg eq '--config' && @ARGV) { $config_path = shift @ARGV }
|
|
elsif ($arg eq '--delete') { $do_delete = 1 }
|
|
else { die "Unknown argument: $arg\n" }
|
|
}
|
|
|
|
die "Config file not found: $config_path\n" unless -f $config_path;
|
|
my $config = Mnemosyne::Config->new($config_path);
|
|
my $token = $config->get('bot', 'token') or die "bot.token missing\n";
|
|
my $secret = $config->get('bot', 'webhook_secret') or die "bot.webhook_secret missing\n";
|
|
my $wh_url = $config->get('bot', 'webhook_url') or die "bot.webhook_url missing\n";
|
|
|
|
my $tg = Mnemosyne::Telegram->new($token);
|
|
|
|
if ($do_delete) {
|
|
my $res = $tg->delete_webhook;
|
|
if ($res->{ok}) {
|
|
print "Webhook deleted.\n";
|
|
} else {
|
|
print "Error: $res->{description}\n";
|
|
exit 1;
|
|
}
|
|
} else {
|
|
my $res = $tg->set_webhook($wh_url, $secret);
|
|
if ($res->{ok}) {
|
|
print "Webhook registered: $wh_url\n";
|
|
} else {
|
|
print "Error: $res->{description}\n";
|
|
exit 1;
|
|
}
|
|
}
|