Skip to content

Commit

Permalink
#90 Live location API: added methods for sending quick preformatted A…
Browse files Browse the repository at this point in the history
…PI responses
  • Loading branch information
DJTommek committed May 17, 2022
1 parent b382763 commit e3f7305
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
65 changes: 30 additions & 35 deletions src/libs/Web/ChatLiveLocation/ChatLiveLocationPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace App\Web\ChatLiveLocation;

use App\Config;
use App\TelegramCustomWrapper\Events\Special\LocationEvent;
use App\TelegramUpdateDb;
use App\Utils\General;
use App\Utils\Strict;
use App\Web\MainPresenter;
use Nette\Utils\Json;
use unreal4u\TelegramAPI\Telegram;

class ChatLiveLocationPresenter extends MainPresenter
Expand All @@ -18,42 +16,39 @@ public function action()
{
$result = new \stdClass();
$result->locations = [];
if (Strict::isInt($_GET['telegramId'] ?? null)) {
$chatTelegramId = Strict::intval($_GET['telegramId'] ?? null);
$updates = TelegramUpdateDb::findByChatId($chatTelegramId);
if (Strict::isInt($_GET['telegramId'] ?? false) === false) {
$this->apiError('Invalid or missing Telegram chat ID');
}
$chatTelegramId = Strict::intval($_GET['telegramId']);
$updates = TelegramUpdateDb::findByChatId($chatTelegramId);

foreach ($updates as $update) {
$locationEvent = new LocationEvent($update->getOriginalUpdateObject());
$locations = $locationEvent->getCollection();
if (in_array(General::globalGetToBool('address'), [true, null], true)) { // if not set, default is true
$locations->fillAddresses();
}
if (General::globalGetToBool('datetimezone') === true) {
$locations->fillDatetimeZone();
}
if (General::globalGetToBool('elevation') === true) {
$locations->fillElevations();
}
foreach ($locations as $location) {
$responseLocation = new \stdClass();
$responseLocation->telegram_id = $locationEvent->getFromId();
$responseLocation->telegram_displayname = $locationEvent->getFromDisplayname();
$responseLocation->lat = $location->getLat();
$responseLocation->lon = $location->getLon();
$responseLocation->elevation = $location->getCoordinates()->getElevation();
$responseLocation->address = $location->getAddress();
$responseLocation->timezone = $location->getTimezoneData();
$responseLocation->lastUpdate = $update->getLastUpdate()->getTimestamp();
$result->locations[] = $responseLocation;

foreach ($updates as $update) {
$locationEvent = new LocationEvent($update->getOriginalUpdateObject());
$locations = $locationEvent->getCollection();
if (in_array(General::globalGetToBool('address'), [true, null], true)) { // if not set, default is true
$locations->fillAddresses();
}
if (General::globalGetToBool('datetimezone') === true) {
$locations->fillDatetimeZone();
}
if (General::globalGetToBool('elevation') === true) {
$locations->fillElevations();
}
foreach ($locations as $location) {
$responseLocation = new \stdClass();
// $update->getChatId();
$responseLocation->telegram_id = $locationEvent->getFromId();
$responseLocation->telegram_displayname = $locationEvent->getFromDisplayname();
$responseLocation->lat = $location->getLat();
$responseLocation->lon = $location->getLon();
$responseLocation->elevation = $location->getCoordinates()->getElevation();
$responseLocation->address = $location->getAddress();
$responseLocation->timezone = $location->getTimezoneData();
$responseLocation->lastUpdate = $update->getLastUpdate()->format(Config::DATETIME_FORMAT_ZONE);
$result->locations[] = $responseLocation;
}
}
} else {
die('no valid telegramId');
}
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
die(Json::encode($result, JSON_PRETTY_PRINT));
$this->apiResponse($result);
}
}

18 changes: 18 additions & 0 deletions src/libs/Web/MainPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\User;
use App\Utils\Strict;
use App\Web\Login\LoginFacade;
use Nette\Utils\Json;
use Nette\Utils\Strings;

abstract class MainPresenter
Expand Down Expand Up @@ -99,5 +100,22 @@ public final function getFlashMessages(): \Generator
unset($_SESSION['FLASH_MESSAGES'][$key]);
}
}

public function apiResponse(\stdClass|array $result, string $message = '', bool $error = false): void
{
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
die(Json::encode([
'datetime' => time(),
'error' => $error,
'message' => $message,
'result' => $result,
], JSON_PRETTY_PRINT));
}

public function apiError(string $message = ''): void
{
$this->apiResponse([], $message, true);
}
}

0 comments on commit e3f7305

Please sign in to comment.