Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify telegram setup #232

Merged
merged 1 commit into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/components/TelegramInstructions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
In order to receive authentication codes via Telegram, you first
have to start a new chat with the bot set up by your admin.<br>
Secondly, you have to obtain your Telegram ID via the <a
href="https://telegram.me/get_id_bot"
href="https://telegram.me/getmyid_bot"
target="_blank"
rel="noreferrer noopener">ID Bot</a>.
Enter this ID to receive your verification code below.
Expand Down
44 changes: 11 additions & 33 deletions lib/Service/Gateway/Telegram/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCP\ILogger;
use OCP\IUser;
use Telegram\Bot\Api;
use Telegram\Bot\Exceptions\TelegramSDKException;
use Telegram\Bot\Objects\Update;

class Gateway implements IGateway {
Expand Down Expand Up @@ -72,42 +73,19 @@ public function send(IUser $user, string $identifier, string $message) {
$this->logger->debug("telegram bot token: $botToken");

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

$this->logger->debug("sending telegram message to chat $chatId");
$api->sendMessage([
'chat_id' => $chatId,
'text' => $message,
]);
$this->logger->debug("telegram message to chat $chatId sent");
}

private function getChatId(IUser $user, Api $api, int $userId): int {
$chatId = $this->config->getUserValue($user->getUID(), 'twofactor_gateway', 'telegram_chat_id', null);
$this->logger->debug("sending telegram message to $identifier");
try {
$api->sendMessage([
'chat_id' => $identifier,
'text' => $message,
]);
} catch (TelegramSDKException $e) {
$this->logger->logException($e);

if (!is_null($chatId)) {
$this->logger->debug("using cached telegram chat id $chatId for user $userId");
return (int)$chatId;
throw new SmsTransmissionException($e);
}

$this->logger->debug("trying to get chat id from updates");
$updates = $api->getUpdates();
$this->logger->debug("got " . count($updates) . " updates");
/** @var Update $update */
$update = current(array_filter($updates, function (Update $data) use ($userId) {
if ($data->message->text === "/start" && $data->message->from->id === $userId) {
$this->logger->debug("found `/start` message for user $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', 'telegram_chat_id', $chatId);
$this->logger->debug("found chat id $chatId for user $userId");

return (int)$chatId;
$this->logger->debug("telegram message to chat $identifier sent");
}

/**
Expand Down