Split files into smaller classed. Add enable_bot.php file to enable bot daily.
This commit is contained in:
56
index.php
56
index.php
@@ -1,51 +1,20 @@
|
||||
<?php
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Kplaczek\FoodsiBot\FoodsiService;
|
||||
use Kplaczek\FoodsiBot\HomeassistantService;
|
||||
|
||||
require_once 'vendor/autoload.php';
|
||||
date_default_timezone_set('Europe/Warsaw');
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
|
||||
$dotenv->load();
|
||||
$client = new Client();
|
||||
function send_notifications(Client $client, string $receiver, string $title, string $body): void
|
||||
{
|
||||
$client->post(sprintf('%s/api/services/notify/%s', $_ENV['HOMEASSISTANT_URL'], $receiver), [
|
||||
'headers' => ['Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN']],
|
||||
'json' => ['message' => $body, 'title' => $title]
|
||||
]);
|
||||
$homeassistant = new HomeassistantService($client);
|
||||
if ($homeassistant->getEntityInfo('input_boolean.foodsi_bot_enabled')['state'] === 'off') {
|
||||
return;
|
||||
}
|
||||
|
||||
function update_homeassistent_entity(Client $client, array $package_info): void
|
||||
{
|
||||
$client->post(sprintf('%s/api/states/sensor.foodsi_3kromki_large', $_ENV['HOMEASSISTANT_URL']), [
|
||||
'headers' => ['Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN']],
|
||||
'json' => ['state' => $package_info['current_quantity']]
|
||||
]);
|
||||
}
|
||||
|
||||
function get_foodsie_package_info(Client $client): array
|
||||
{
|
||||
$response = $client->post('https://api.foodsi.pl/api/v2/auth/sign_in', [
|
||||
'json' => [
|
||||
'email' => $_ENV['FOODSI_EMAIL'],
|
||||
'password' => $_ENV['FOODSI_PASSWORD'],
|
||||
],
|
||||
]);
|
||||
$response = $client->get(
|
||||
'https://api.foodsi.pl/api/v3/user/offers?filter[venue_name][]=Trzy kromki chleba&filter[active]=true&filter[current_quantity][gt]=-1&filter[id]=6052730',
|
||||
[
|
||||
'headers' => [
|
||||
'Access-Token' => $response->getHeader('Access-Token'),
|
||||
'Client' => $response->getHeader('Client'),
|
||||
'Uid' => $response->getHeader('Uid'),
|
||||
],
|
||||
],
|
||||
);
|
||||
$data = json_decode($response->getBody(), true);
|
||||
|
||||
return $data['data'][0]['attributes'];
|
||||
}
|
||||
|
||||
$packageInfo = get_foodsie_package_info($client);
|
||||
$foodsi = new FoodsiService($client);
|
||||
$packageInfo = $foodsi->getPackageInfo();
|
||||
$receivers = explode(',', $_ENV['HOMEASSISTANT_NOTIFICATION_CLIENTS']);
|
||||
$notificationSent = false;
|
||||
foreach ($receivers as $receiver) {
|
||||
@@ -54,11 +23,16 @@ foreach ($receivers as $receiver) {
|
||||
}
|
||||
$lastNotificationDate = file_get_contents('last_notification_date.txt');
|
||||
if ($packageInfo['current_quantity'] > 0 && $lastNotificationDate !== date('Y-m-d')) {
|
||||
send_notifications($client, $receiver, 'Dostępna paczka w 3 kromki chleba', sprintf('W trzech kromkach dostępne są paczki. Ilość: %s', $packageInfo['current_quantity']));
|
||||
$homeassistant->sendNotification(
|
||||
$receiver,
|
||||
'Dostępna paczka w 3 kromki chleba',
|
||||
sprintf('W trzech kromkach dostępne są paczki. Ilość: %s', $packageInfo['current_quantity']),
|
||||
);
|
||||
$notificationSent = true;
|
||||
}
|
||||
}
|
||||
if ($notificationSent) {
|
||||
file_put_contents('last_notification_date.txt', date('Y-m-d'));
|
||||
$homeassistant->updateEntity('input_boolean.foodsi_bot_enabled', 'off');
|
||||
}
|
||||
update_homeassistent_entity($client, $packageInfo);
|
||||
$homeassistant->updateEntity('sensor.foodsi_3kromki_large', $packageInfo['current_quantity'], ['update' => date('Y-m-d H:i:s')]);
|
||||
|
||||
Reference in New Issue
Block a user