Split files into smaller classed. Add enable_bot.php file to enable bot daily.
This commit is contained in:
11
enable_bot.php
Normal file
11
enable_bot.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Kplaczek\FoodsiBot\HomeassistantService;
|
||||
|
||||
require_once 'vendor/autoload.php';
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
|
||||
$dotenv->load();
|
||||
$client = new Client();
|
||||
$homeassistant = new HomeassistantService($client);
|
||||
$homeassistant->updateEntity('input_boolean.foodsi_bot_enabled', 'on');
|
||||
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')]);
|
||||
|
||||
52
src/FoodsiService.php
Normal file
52
src/FoodsiService.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Kplaczek\FoodsiBot;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
readonly class FoodsiService
|
||||
{
|
||||
private string $accessToken;
|
||||
private string $client;
|
||||
private string $uid;
|
||||
|
||||
public function __construct(private Client $httpClient)
|
||||
{
|
||||
}
|
||||
|
||||
public function getPackageInfo(): array
|
||||
{
|
||||
$this->login();
|
||||
$response = $this->httpClient->get(
|
||||
'https://api.foodsi.pl/api/v3/user/offers?filter[venue_name][]=Trzy kromki chleba&filter[active]=true&filter[current_quantity][gt]=-1&filter[original_price]=35',
|
||||
[
|
||||
'headers' => [
|
||||
'Access-Token' => $this->accessToken,
|
||||
'Client' => $this->client,
|
||||
'Uid' => $this->uid,
|
||||
],
|
||||
],
|
||||
);
|
||||
$data = json_decode($response->getBody(), true);
|
||||
if (isset($data['data'][0])) {
|
||||
return $data['data'][0]['attributes'];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
private function login(): void
|
||||
{
|
||||
if (empty($this->accessToken)) {
|
||||
$response = $this->httpClient->post('https://api.foodsi.pl/api/v2/auth/sign_in', [
|
||||
'json' => [
|
||||
'email' => $_ENV['FOODSI_EMAIL'],
|
||||
'password' => $_ENV['FOODSI_PASSWORD'],
|
||||
],
|
||||
]);
|
||||
$this->accessToken = $response->getHeaderLine('Access-Token');
|
||||
$this->client = $response->getHeaderLine('Client');
|
||||
$this->uid = $response->getHeaderLine('Uid');
|
||||
}
|
||||
}
|
||||
}
|
||||
43
src/HomeassistantService.php
Normal file
43
src/HomeassistantService.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Kplaczek\FoodsiBot;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
readonly class HomeassistantService
|
||||
{
|
||||
public function __construct(private Client $client)
|
||||
{
|
||||
}
|
||||
|
||||
public function getEntityInfo(string $entityId): array
|
||||
{
|
||||
$response = $this->client->get(sprintf('%s/api/states/%s', $_ENV['HOMEASSISTANT_URL'], $entityId), [
|
||||
'headers' => [
|
||||
'Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN'],
|
||||
],
|
||||
]);
|
||||
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
public function updateEntity(string $entityId, string $state, array $attributes = []): void
|
||||
{
|
||||
$this->client->post(
|
||||
sprintf('%s/api/states/%s', $_ENV['HOMEASSISTANT_URL'], $entityId), [
|
||||
'headers' => ['Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN']],
|
||||
'json' => ['state' => $state, 'attributes' => $attributes],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
function sendNotification(string $receiver, string $title, string $body): void
|
||||
{
|
||||
$this->client->post(sprintf('%s/api/services/notify/%s', $_ENV['HOMEASSISTANT_URL'], $receiver), [
|
||||
'headers' => [
|
||||
'Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN'],
|
||||
],
|
||||
'json' => ['message' => $body, 'title' => $title]
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user