Completed
Push — master ( ea0713...bd13d1 )
by Vitor
15s
created

Bot::send()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 20
ccs 0
cts 13
cp 0
rs 9.7998
cc 3
nc 4
nop 3
crap 12
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
		'id' => 'telegram_bot',
30
		'name' => 'Telegram Bot',
31
		'allow_markdown' => true,
32
		'instructions' => <<<HTML
33
			<p>In order to receive authentication codes via Telegram, you first
34
			have to start a new chat with the bot set up by your admin.</p>
35
36
			<p>Secondly, you have to obtain your Telegram ID via the
37
			<a href="https://telegram.me/getmyid_bot" target="_blank" rel="noreferrer noopener">ID Bot</a>.</p>
38
39
			<p>Enter this ID to receive your verification code below.</p>
40
			HTML,
41
		'fields' => [
42
			['field' => 'bot_token', 'prompt' => 'Please enter your Telegram bot token:'],
43
		],
44
	];
45
	public function __construct(
46
		private LoggerInterface $logger,
47
		private IL10N $l10n,
48
	) {
49
	}
50
51
	#[\Override]
52
	public function send(string $identifier, string $message, array $extra = []): void {
53
		if (empty($message)) {
54
			$message = $this->l10n->t('`%s` is your Nextcloud verification code.', [$extra['code']]);
55
		}
56
		$this->logger->debug("sending telegram message to $identifier, message: $message");
57
		$botToken = $this->getBotToken();
58
		$this->logger->debug("telegram bot token: $botToken");
59
60
		$api = new BotApi($botToken);
61
62
		$this->logger->debug("sending telegram message to $identifier");
63
		try {
64
			$api->sendMessage($identifier, $message, parseMode: 'markdown');
65
		} catch (TelegramSDKException $e) {
66
			$this->logger->error($e);
67
68
			throw new MessageTransmissionException($e->getMessage());
69
		}
70
		$this->logger->debug("telegram message to chat $identifier sent");
71
	}
72
73
	#[\Override]
74
	public function cliConfigure(InputInterface $input, OutputInterface $output): int {
75
		$helper = new QuestionHelper();
76
		$tokenQuestion = new Question(self::SCHEMA['fields'][0]['prompt'] . ' ');
77
		$token = $helper->ask($input, $output, $tokenQuestion);
78
		$this->setBotToken($token);
79
		$output->writeln("Using $token.");
80
		return 0;
81
	}
82
}
83