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

Add debug logging to telegram gateway #126

Merged
merged 1 commit into from Oct 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/Service/Gateway/Telegram/Gateway.php
Expand Up @@ -30,6 +30,7 @@
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IUser;
use Telegram\Bot\Api;
use Telegram\Bot\Objects\Update;
Expand All @@ -45,12 +46,17 @@ class Gateway implements IGateway {
/** @var IConfig */
private $config;

/** @var ILogger */
private $logger;

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

/**
Expand All @@ -61,28 +67,36 @@ public function __construct(IClientService $clientService,
* @throws SmsTransmissionException
*/
public function send(IUser $user, string $identifier, string $message) {
$this->logger->debug("sending telegram message to $identifier, message: $message");
$botToken = $this->gatewayConfig->getBotToken();
$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);

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

$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;
Expand All @@ -91,6 +105,7 @@ private function getChatId(IUser $user, Api $api, int $userId): int {

$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;
}
Expand Down