Passed
Pull Request — master (#671)
by Vitor
08:37
created

Bot   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 36
c 1
b 0
f 1
dl 0
loc 54
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A cliConfigure() 0 10 1
A send() 0 20 3
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 Christoph Wurst <[email protected]>
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\TwoFactorGateway\Provider\Channel\Telegram\Provider\Drivers;
11
12
use OCA\TwoFactorGateway\Exception\MessageTransmissionException;
13
use OCA\TwoFactorGateway\Provider\Channel\Telegram\Provider\AProvider;
14
use OCA\TwoFactorGateway\Vendor\TelegramBot\Api\BotApi;
0 ignored issues
show
Bug introduced by
The type OCA\TwoFactorGateway\Vendor\TelegramBot\Api\BotApi was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use OCA\TwoFactorGateway\Vendor\TelegramBot\Api\Exception as TelegramSDKException;
0 ignored issues
show
Bug introduced by
The type OCA\TwoFactorGateway\Ven...legramBot\Api\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use OCP\IL10N;
17
use Psr\Log\LoggerInterface;
18
use Symfony\Component\Console\Helper\QuestionHelper;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Question\Question;
22
23
/**
24
 * @method string getBotToken()
25
 * @method static setBotToken(string $botToken)
26
 */
27
class Bot extends AProvider {
28
	public const SCHEMA = [
29
		'name' => 'Telegram Bot',
30
		'instructions' => <<<HTML
31
			<p>In order to receive authentication codes via Telegram, you first
32
			have to start a new chat with the bot set up by your admin.</p>
33
34
			<p>Secondly, you have to obtain your Telegram ID via the
35
			<a href="https://telegram.me/getmyid_bot" target="_blank" rel="noreferrer noopener">ID Bot</a>.</p>
36
37
			<p>Enter this ID to receive your verification code below.</p>
38
			HTML,
39
		'fields' => [
40
			['field' => 'bot_token', 'prompt' => 'Please enter your Telegram bot token:'],
41
		],
42
	];
43
	public function __construct(
44
		private LoggerInterface $logger,
45
		private IL10N $l10n,
46
	) {
47
	}
48
49
	#[\Override]
50
	public function send(string $identifier, string $message, array $extra = []): void {
51
		if (empty($message)) {
52
			$message = $this->l10n->t('`%s` is your Nextcloud verification code.', [$extra['code']]);
53
		}
54
		$this->logger->debug("sending telegram message to $identifier, message: $message");
55
		$botToken = $this->getBotToken();
56
		$this->logger->debug("telegram bot token: $botToken");
57
58
		$api = new BotApi($botToken);
59
60
		$this->logger->debug("sending telegram message to $identifier");
61
		try {
62
			$api->sendMessage($identifier, $message, parseMode: 'markdown');
63
		} catch (TelegramSDKException $e) {
64
			$this->logger->error($e);
65
66
			throw new MessageTransmissionException($e->getMessage());
67
		}
68
		$this->logger->debug("telegram message to chat $identifier sent");
69
	}
70
71
	#[\Override]
72
	public function cliConfigure(InputInterface $input, OutputInterface $output): int {
73
		$helper = new QuestionHelper();
74
		$tokenQuestion = new Question(self::SCHEMA['fields'][0]['prompt'] . ' ');
75
		$token = $helper->ask($input, $output, $tokenQuestion);
76
		$this->setBotToken($token);
77
		$output->writeln("Using $token.");
78
79
		$this->setBotToken($token);
80
		return 0;
81
	}
82
}
83