Passed
Pull Request — master (#640)
by Vitor
03:39
created

Gateway::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 18
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 4
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;
11
12
use OCA\TwoFactorGateway\Exception\MessageTransmissionException;
13
use OCA\TwoFactorGateway\Provider\Gateway\AGateway;
14
use OCP\IAppConfig;
15
use OCP\IL10N;
16
use OCP\IUser;
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
use TelegramBot\Api\BotApi;
23
use TelegramBot\Api\Exception as TelegramSDKException;
24
25
/**
26
 * @method string getBotToken()
27
 * @method static setBotToken(string $botToken)
28
 */
29
class Gateway extends AGateway {
30
	public const SCHEMA = [
31
		'name' => 'Telegram',
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
		public IAppConfig $appConfig,
47
		private LoggerInterface $logger,
48
		private IL10N $l10n,
49
	) {
50
		parent::__construct($appConfig);
51
	}
52
53
	#[\Override]
54
	public function send(IUser $user, string $identifier, string $message, array $extra = []): void {
55
		$message = $this->l10n->t('`%s` is your Nextcloud verification code.', [$extra['code']]);
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
81
		$this->setBotToken($token);
82
		return 0;
83
	}
84
}
85