Skip to content

Commit

Permalink
update: sync to Bot API 7.10 and improve optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirHkrg committed Sep 6, 2024
1 parent 05281f3 commit 97f29b7
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 51 deletions.
11 changes: 11 additions & 0 deletions src/Exceptions/InvalidGetUpdateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace LaraGram\Laraquest\Exceptions;

use Throwable;
use UnexpectedValueException;

final class InvalidGetUpdateType extends UnexpectedValueException implements Throwable
{

}
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidUpdateType.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace LaraGram\Exceptions;
namespace LaraGram\Laraquest\Exceptions;

use Throwable;
use UnexpectedValueException;
Expand Down
2 changes: 1 addition & 1 deletion src/Methode.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function sendVideoNote($chat_id, $video_note, $message_thread_id = null,
return $this->endpoint('sendVideoNote', get_defined_vars());
}

public function sendPaidMedia($chat_id, $star_count, $media, $caption = null, $pars_mode = null, $caption_entities = null, $show_caption_above_media = null, $disable_notification = null, $protect_content = null, $reply_parameters = null, $reply_markup = null, $business_connection_id = null): bool|array|string|null
public function sendPaidMedia($chat_id, $star_count, $media, $payload = null, $caption = null, $pars_mode = null, $caption_entities = null, $show_caption_above_media = null, $disable_notification = null, $protect_content = null, $reply_parameters = null, $reply_markup = null, $business_connection_id = null): bool|array|string|null
{
return $this->endpoint('sendPaidMedia', get_defined_vars());
}
Expand Down
97 changes: 50 additions & 47 deletions src/Updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace LaraGram\Laraquest;

use LaraGram\Laraquest\Exceptions\InvalidGetUpdateType;
use LaraGram\Laraquest\Exceptions\InvalidUpdateType;
use LaraGram\Laraquest\Updates\BusinessConnection;
use LaraGram\Laraquest\Updates\BusinessMessagesDeleted;
use LaraGram\Laraquest\Updates\CallbackQuery;
Expand All @@ -14,12 +16,11 @@
use LaraGram\Laraquest\Updates\Message;
use LaraGram\Laraquest\Updates\MessageReactionCountUpdated;
use LaraGram\Laraquest\Updates\MessageReactionUpdated;
use LaraGram\Laraquest\Updates\PaidMediaPurchased;
use LaraGram\Laraquest\Updates\Poll;
use LaraGram\Laraquest\Updates\PollAnswer;
use LaraGram\Laraquest\Updates\PreCheckoutQuery;
use LaraGram\Laraquest\Updates\ShippingQuery;
use LaraGram\Exceptions\InvalidUpdateType;

/**
* @property int $update_id
* @property Message $message
Expand All @@ -44,75 +45,77 @@
* @property ChatJoinRequest $chat_join_request
* @property ChatBoostUpdated $chat_boost
* @property ChatBoostRemoved $removed_chat_boost
* @property PaidMediaPurchased $purchased_paid_media
*/
trait Updates
{
private string $update_type;
private float $polling_sleep_time;
private int $polling_timeout;

public function __construct()
{
if (class_exists("LaraGram\\Config\\Repository")) {
$this->update_type = config()->get('bot.UPDATE_TYPE');
} else {
$this->update_type = $_ENV['UPDATE_TYPE'];
}
$getConfigValue = function ($key, $default, $file) {
return class_exists("LaraGram\\Config\\Repository")
? config()->get("$file.$key") ?? $default
: ($_ENV[$key] ?? $default);
};

$this->update_type = $getConfigValue('UPDATE_TYPE', 'sync', 'bot');
$this->polling_sleep_time = $getConfigValue('POLLING_SLEEP_TIME', 0.5, 'bot');
$this->polling_timeout = $getConfigValue('POLLING_TIMEOUT', 100, 'bot');
}
public function __get($name)
{
if (isset($this->update_type) && $this->update_type == 'global') {
global $data;
$update = json_decode($data['argv'][1]);
} elseif ($this->update_type == 'sync' || !isset($this->update_type)) {
$update = json_decode(file_get_contents('php://input'));
} elseif ($this->update_type == 'openswoole' || $this->update_type == 'swoole') {
global $swoole;
$update = $swoole;
} elseif ($this->update_type == 'polling') {
global $data;
$update = $data;
}
global $data;
global $swoole;
$update = match ($this->update_type){
'sync' => json_decode(file_get_contents('php://input')),
'global' => json_decode($data['argv'][1]),
'openswoole', 'swoole' => $swoole,
'polling' => $data,
default => throw new InvalidGetUpdateType("Unknown get update type")
};

return ($update->{$name}) ?? null;
}

public static function polling(callable $callback): void
{
global $data;

$lastUpdateId = null;
$polling = new Laraquest();
while (true){
$updates = $polling->getUpdates($lastUpdateId + 1, timeout: 100)['result'];
foreach ($updates as $update){
$lastUpdateId = $update['update_id'];
global $data;
$data = json_decode(json_encode($update));
$callback($polling);

try {
while (true){
$updates = $polling->getUpdates($lastUpdateId + 1, timeout: $polling->polling_timeout)['result'];

foreach ($updates as $update){
$lastUpdateId = $update['update_id'];
$data = json_decode(json_encode($update));

$callback($polling);
}

sleep($polling->polling_sleep_time);
}
sleep(0.5);
} catch (\Exception $exception){
file_put_contents('laraquest.log', $exception->getMessage() . PHP_EOL, FILE_APPEND);
}
}

public function getData()
{
if (class_exists("LaraGram\\Config\\Repository")) {
$update_type = config()->get('bot.UPDATE_TYPE');
} else {
$update_type = $_ENV['UPDATE_TYPE'];
}

if (isset($update_type) && $update_type == 'global') {
global $data;
return json_decode($data['argv'][1]);
} elseif ($update_type == 'sync' || !isset($update_type)) {
return json_decode(file_get_contents('php://input'));
} elseif ($update_type == 'openswoole' || $update_type == 'swoole') {
global $swoole;
return $swoole;
} elseif ($this->update_type == 'polling') {
global $data;
return $data;
}

return false;
global $data;
global $swoole;
return match ($this->update_type){
'sync' => json_decode(file_get_contents('php://input')),
'global' => json_decode($data['argv'][1]),
'openswoole', 'swoole' => $swoole,
'polling' => $data,
default => throw new InvalidGetUpdateType("Unknown get update type")
};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Updates/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
* @property string $last_name
* @property True $is_forum
**/
**/
class Chat { }
1 change: 1 addition & 0 deletions src/Updates/ChatBoostSourceGiveaway.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @property string $source
* @property int $giveaway_message_id
* @property User $user
* @property int $prize_star_count
* @property True $is_unclaimed
**/
Expand Down
1 change: 1 addition & 0 deletions src/Updates/Giveaway.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @property True $has_public_winners
* @property string $prize_description
* @property array $country_codes
* @property int $prize_star_count
* @property int $premium_subscription_month_count
**/
Expand Down
1 change: 1 addition & 0 deletions src/Updates/GiveawayCompleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @property int $winner_count
* @property int $unclaimed_prize_count
* @property Message $giveaway_message
* @property bool $is_star_giveaway
**/
class GiveawayCompleted { }
1 change: 1 addition & 0 deletions src/Updates/GiveawayCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @property True $only_new_members
* @property True $has_public_winners
* @property string $prize_description
* @property int $prize_star_count
* @property array $country_codes
* @property int $premium_subscription_month_count
Expand Down
1 change: 1 addition & 0 deletions src/Updates/GiveawayWinners.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @property int $winner_count
* @property array $winners
* @property int $additional_chat_count
* @property int $prize_star_count
* @property int $premium_subscription_month_count
* @property int $unclaimed_prize_count
* @property True $only_new_members
Expand Down
10 changes: 10 additions & 0 deletions src/Updates/PaidMediaPurchased.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace LaraGram\Laraquest\Updates;

/**
* @property User $from
* @property string $paid_media_payload
**/
class PaidMediaPurchased { }
3 changes: 2 additions & 1 deletion src/Updates/TransactionPartnerUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* @property string $type
* @property User $user
* @property string $invoice_payload
* @property array $paidMedia
* @property array $paid_media
* @property string $paid_media_payload
**/
class TransactionPartnerUser { }

0 comments on commit 97f29b7

Please sign in to comment.