Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/nutgram.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ nutgram:
# The Telegram bot token
token: '%env(string:TELEGRAM_TOKEN)%'

# If true, the webhook mode validates the incoming IP range is from a Telegram server
safeMode: false
# Specify webhook secret for increased security
#webhook_secret: '%env(string:WEBHOOK_TOKEN)%'

# If the nutgram bundle should automatically load the routes from config/telegram.php
routes: true
Expand Down
9 changes: 7 additions & 2 deletions src/Console/WebhookSetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

#[AsCommand(
name: 'nutgram:hook:set',
Expand All @@ -19,10 +20,13 @@ class WebhookSetCommand extends Command
{
private Nutgram $bot;

public function __construct(Nutgram $bot, string $name = null)
private ParameterBagInterface $parameters;

public function __construct(Nutgram $bot, ParameterBagInterface $parameters, string $name = null)
{
parent::__construct($name);
$this->bot = $bot;
$this->parameters = $parameters;
}

protected function configure(): void
Expand Down Expand Up @@ -50,7 +54,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$max_connections = (int)$max_connections;
}

$this->bot->setWebhook($url, ip_address: $ip_address, max_connections: $max_connections);
$secret = $this->parameters->get('nutgram.config')['webhook_secret'];
$this->bot->setWebhook($url, ip_address: $ip_address, max_connections: $max_connections, secret_token: $secret);

$io->info("Bot webhook set with url: $url");

Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$treeBuilder->getRootNode()
->children()
->scalarNode('token')->end()
->booleanNode('safeMode')->end()
->scalarNode('webhook_secret')->defaultNull()->end()
->booleanNode('routes')->end()
->arrayNode('config')
->children()
Expand Down
5 changes: 3 additions & 2 deletions src/DependencyInjection/Factory/NutgramFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public function createNutgram(
} else {
$webhook = Webhook::class;

if ($config['safe_mode'] ?? false) {
$webhook = new $webhook(fn() => $requestStack->getCurrentRequest()?->getClientIp());
if ($config['webhook_secret']) {
$webhook = new Webhook(secretToken: $config['webhook_secret']);
$webhook->setSafeMode(true);
}

$bot->setRunningMode($webhook);
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/test_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ framework:

nutgram:
token: '%env(string:TELEGRAM_TOKEN)%'
safeMode: false
routes: true
webhook_secret: 'VerySecret'
config:
botId: 123
apiUrl: 'BlaBla'
Expand Down
4 changes: 3 additions & 1 deletion tests/Functional/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SergiX44\NutgramBundle\Console\WebhookRemoveCommand;
use SergiX44\NutgramBundle\Console\WebhookSetCommand;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

it('call the logout command', function () {
/** @var \SergiX44\Nutgram\Testing\FakeNutgram $instance */
Expand Down Expand Up @@ -72,8 +73,9 @@
it('calls the set webhook', function () {
/** @var \SergiX44\Nutgram\Testing\FakeNutgram $instance */
$instance = static::getContainer()->get(Nutgram::class);
$parameters = static::getContainer()->get(ParameterBagInterface::class);

$commandTester = new CommandTester(new WebhookSetCommand($instance));
$commandTester = new CommandTester(new WebhookSetCommand($instance, $parameters));
$commandTester->execute(['url' => 'http://foo.bar']);
$commandTester->assertCommandIsSuccessful();

Expand Down