Completed
Push — master ( 8024bc...f8f0d2 )
by Vitor
14s
created

Bot   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 43.24%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 6
eloc 38
dl 0
loc 59
ccs 16
cts 37
cp 0.4324
rs 10
c 2
b 1
f 1

4 Methods

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