Passed
Pull Request — master (#686)
by Vitor
04:21
created

Bot::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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