Created
March 1, 2018 13:01
-
-
Save Koenvh1/1b652dfd66fe04f0d00dc5f5477faa54 to your computer and use it in GitHub Desktop.
Telegram logic behind http://koenvh.nl/flitsmelder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Script to set the webhook URL (only needs to be run once) | |
require_once (dirname(__FILE__) . "/vendor/autoload.php"); | |
require_once (dirname(__FILE__) . "/app/Telegram.php"); | |
use \unreal4u\TelegramAPI\HttpClientRequestHandler; | |
use \unreal4u\TelegramAPI\TgLog; | |
use \unreal4u\TelegramAPI\Telegram\Methods\SetWebhook; | |
$setWebhook = new SetWebhook(); | |
$setWebhook->url = 'https://koenvh.nl/flitsmelder/telegram'; | |
$loop = \React\EventLoop\Factory::create(); | |
$handler = new HttpClientRequestHandler($loop); | |
$tgLog = new TgLog(TELEGRAM_KEY, $handler); | |
$deferred = new React\Promise\Deferred(); | |
$tgLog->performApiRequest($setWebhook); | |
$loop->run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Class to send a message via Telegram | |
class Telegram | |
{ | |
function sendMessage($id, $text, $location = null) | |
{ | |
$loop = \React\EventLoop\Factory::create(); | |
$handler = new HttpClientRequestHandler($loop); | |
$tgLog = new TgLog(TELEGRAM_KEY, $handler); | |
$sendMessage = new SendMessage(); | |
$sendMessage->disable_web_page_preview = true; | |
$sendMessage->parse_mode = "html"; | |
$sendMessage->chat_id = $id; | |
$sendMessage->text = $text; | |
if(is_array($location)) { | |
$sendLocation = new SendLocation(); | |
$sendLocation->chat_id = $id; | |
$sendLocation->latitude = $location["lat"]; | |
$sendLocation->longitude = $location["lon"]; | |
$sendLocation->disable_notification = true; | |
$tgLog->performApiRequest($sendLocation); | |
$loop->run(); | |
} | |
$tgLog->performApiRequest($sendMessage); | |
$loop->run(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Function to handle incoming Telegram messages | |
public function telegram(ServerRequestInterface $request, ResponseInterface $response, $args) | |
{ | |
$updateData = json_decode(file_get_contents('php://input'), true); | |
$update = new \unreal4u\TelegramAPI\Telegram\Types\Update($updateData); | |
$telegram = new Telegram(); | |
if($update->message->text == "/start") { | |
$flitsUsers = new FlitsUsers(); | |
$id = $update->message->chat->id; | |
$telegram->sendMessage($id, "De code is: <code>$id</code>\n\nStuur <code>/check</code> om handmatig te controleren of er flitsmeldingen op uw route zijn, en <code>/beheer</code> om uw routes te beheren."); | |
if(!$flitsUsers->userExists($id)){ | |
$flitsUsers->addUser($id, uniqid(), "telegram"); | |
} | |
} elseif($update->message->text == "/beheer") { | |
$flitsUsers = new FlitsUsers(); | |
$id = $update->message->chat->id; | |
$token = $flitsUsers->userToken($id); | |
$telegram->sendMessage($update->message->chat->id, "Klik <a href=\"https://koenvh.nl/flitsmelder/beheren/$id/$token\">hier</a> om uw routes te beheren."); | |
} elseif($update->message->text == "/check") { | |
$flitsUsers = new FlitsUsers(); | |
$flitsLogic = new FlitsLogic(); | |
$id = $update->message->chat->id; | |
$radars = $flitsLogic->getRadars(); | |
foreach ($flitsUsers->userRoutes($id) as $route) { | |
$name = $route["name"]; | |
$formulas = $flitsLogic->convertWaypointsToFormulas($flitsUsers->routePoints($id, $name)); | |
$radarsOnRoute = $flitsLogic->getRadarsOnRoute($formulas, $radars); | |
if(count($radarsOnRoute) == 0) { | |
$telegram->sendMessage($update->message->chat->id, "Geen meldingen op de route '$name'"); | |
} else { | |
for ($i = 0; $i < count($radarsOnRoute); $i++) { | |
$telegram->sendMessage($id, $radarsOnRoute[$i]["melding"] . "\n\n" . | |
"Route '$name' | <a href=\"https://koenvh.nl/flitsmelder/beheren/" . $id . "/" . $flitsUsers->userToken($id) . "\">Routes beheren</a> | <a href=\"https://koenvh.nl\">Koenvh</a> | <a href=\"http://flitsservice.nl\">Flitsservice.nl</a>", [ | |
"lat" => $radarsOnRoute[$i]["Latitude"], | |
"lon" => $radarsOnRoute[$i]["Longitude"] | |
]); | |
} | |
} | |
} | |
} | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment