Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TelegramGateway.php « Gateway « Service « lib - github.com/nextcloud/twofactor_gateway.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b667867fcb02df7f41526a45602720db56cf4c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

declare(strict_types=1);

/**
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author André Fondse <andre@hetnetwerk.org>
 *
 * Nextcloud - Two-factor Gateway for Telegram
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace OCA\TwoFactorGateway\Service\Gateway;

use Exception;
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
use OCA\TwoFactorGateway\Service\IGateway;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IUser;
use Telegram\Bot\Api;
use Telegram\Bot\Objects\Update;

class TelegramGateway implements IGateway {

	/** @var IClient */
	private $client;

	/** @var IConfig */
	private $config;

	public function __construct(IClientService $clientService, IConfig $config) {
		$this->client = $clientService->newClient();
		$this->config = $config;
	}

	/**
	 * @param IUser $user
	 * @param string $idenfier
	 * @param string $message
	 * @throws \Telegram\Bot\Exceptions\TelegramSDKException
	 */
	public function send(IUser $user, string $idenfier, string $message) {
		$token = $this->config->getAppValue('twofactor_gateway', 'telegram_bot_token', null);
		// TODO: token missing handling

		$api = new Api($token);
		$chatId = $this->getChatId($user, $api, (int)$idenfier);

		$api->sendMessage([
			'chat_id' => $chatId,
			'text' => $message,
		]);
	}

	private function getChatId(IUser $user, Api $api, int $userId): int {
		$chatId = $this->config->getUserValue($user->getUID(), 'twofactor_gateway', 'telegram_chat_id', null);

		if (!is_null($chatId)) {
			return (int)$chatId;
		}

		$updates = $api->getUpdates();
		/** @var Update $update */
		$update = current(array_filter($updates, function (Update $data) use ($userId) {
			if ($data->message->text === "/start" && $data->message->from->id === $userId) {
				return true;
			}
			return false;
		}));
		// TODO: handle missing `/start` message and `$update` null values

		$chatId = $update->message->chat->id;
		$this->config->setUserValue($user->getUID(), 'twofactor_gateway', 'chat_id', $chatId);

		return (int)$chatId;
	}

	/**
	 * Get a short description of this gateway's name so that users know how
	 * their messages are delivered, e.g. "Telegram"
	 *
	 * @return string
	 */
	public function getShortName(): string {
		return "Telegram";
	}
}