Completed
Push — master ( 636672...b19edb )
by Christoph
12s
created

Gateway::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Christoph Wurst <[email protected]>
7
 * @author André Fondse <[email protected]>
8
 *
9
 * Nextcloud - Two-factor Gateway for Telegram
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\TwoFactorGateway\Service\Gateway\Telegram;
26
27
use Exception;
28
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
29
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
30
use OCP\Http\Client\IClient;
31
use OCP\Http\Client\IClientService;
32
use OCP\IConfig;
33
use OCP\IL10N;
34
use OCP\IUser;
35
use Telegram\Bot\Api;
36
use Telegram\Bot\Objects\Update;
37
38
class Gateway implements IGateway {
39
40
	/** @var IClient */
41
	private $client;
42
43
	/** @var IConfig */
44
	private $config;
45
46
	/** @var IL10N */
47
	private $l10n;
48
49
	public function __construct(IClientService $clientService,
50
								IConfig $config,
51
								IL10N $l10n) {
52
		$this->client = $clientService->newClient();
53
		$this->config = $config;
54
		$this->l10n = $l10n;
55
	}
56
57
	/**
58
	 * @param IUser $user
59
	 * @param string $idenfier
60
	 * @param string $message
61
	 *
62
	 * @throws \Telegram\Bot\Exceptions\TelegramSDKException
63
	 */
64
	public function send(IUser $user, string $idenfier, string $message) {
65
		$token = $this->config->getAppValue('twofactor_gateway', 'telegram_bot_token', null);
66
		// TODO: token missing handling
67
68
		$api = new Api($token);
69
		$chatId = $this->getChatId($user, $api, (int)$idenfier);
70
71
		$api->sendMessage([
72
			'chat_id' => $chatId,
73
			'text' => $message,
74
		]);
75
	}
76
77
	private function getChatId(IUser $user, Api $api, int $userId): int {
78
		$chatId = $this->config->getUserValue($user->getUID(), 'twofactor_gateway', 'telegram_chat_id', null);
79
80
		if (!is_null($chatId)) {
0 ignored issues
show
introduced by
The condition is_null($chatId) is always false.
Loading history...
81
			return (int)$chatId;
82
		}
83
84
		$updates = $api->getUpdates();
85
		/** @var Update $update */
86
		$update = current(array_filter($updates, function (Update $data) use ($userId) {
87
			if ($data->message->text === "/start" && $data->message->from->id === $userId) {
88
				return true;
89
			}
90
			return false;
91
		}));
92
		// TODO: handle missing `/start` message and `$update` null values
93
94
		$chatId = $update->message->chat->id;
95
		$this->config->setUserValue($user->getUID(), 'twofactor_gateway', 'chat_id', $chatId);
96
97
		return (int)$chatId;
98
	}
99
100
	/**
101
	 * Get a short description of this gateway's name so that users know how
102
	 * their messages are delivered, e.g. "Telegram"
103
	 *
104
	 * @return string
105
	 */
106
	public function getShortName(): string {
107
		return 'Telegram';
108
	}
109
110
	/**
111
	 * @return string
112
	 */
113
	public function getProviderDescription(): string {
114
		return $this->l10n->t('Authenticate via Telegram');
115
	}
116
}
117