Skip to content

Commit

Permalink
Add logging, Improve examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Muaath5 committed Dec 7, 2021
1 parent 1a3c5dd commit 3193b94
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ You can see a full example [here](https://muaath5.github.io/SimpleBotAPI/FullExa
- [x] Auto-store for bot users
- [x] Add JSON Storage can be used by bot
- [x] Method that posts to all bot users
- [ ] Add logging (Done partially)
- [ ] Add DB Storage can be used by bot
- [ ] Document missed things

Expand Down
1 change: 0 additions & 1 deletion docs/MessageWithKeyboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,4 @@ $Bot->SendMessage([

[Go to next document?](https://muaath5.github.io/SimpleBotAPI/CallbackQueries)
===
**This was all of out documentation :)**
Anything missed, unclear, Or not working? Contact @Muaath_5!
4 changes: 2 additions & 2 deletions examples/ContactMeBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
{
if ($_REQUEST['token'] == getenv('BOT_TOKEN'))
{
$Bot = new TelegramBot(getenv('BOT_TOKEN'), new BotSettings(new ContactMeBot(getenv('MESSAGES_CHAT_ID'), [126008640])));
$Bot = new TelegramBot(getenv('BOT_TOKEN'), new ContactMeBot(getenv('MESSAGES_CHAT_ID'), [126008640]), new BotSettings());

# Process Webhook Update
$Bot->OnWebhookUpdate(file_get_contents('php://input'));
$Bot->OnWebhookUpdate();
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/EchoBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
# Check authentication
if ($_REQUEST['token'] == getenv('BOT_TOKEN'))
{
$Bot = new TelegramBot(getenv('BOT_TOKEN'), new BotSettings(new EchoBot()));
$Bot = new TelegramBot(getenv('BOT_TOKEN'), new EchoBot(), new BotSettings());

# Process Webhook Update
$Bot->OnWebhookUpdate(file_get_contents('php://input'));
$Bot->OnWebhookUpdate();
}

class EchoBot extends UpdatesHandler
Expand Down
2 changes: 0 additions & 2 deletions examples/FAQBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use SimpleBotAPI\TelegramBot;
use SimpleBotAPI\UpdatesHandler;

use function PHPSTORM_META\type;

# The webhook url should be:
# https://mywebsite.com/echo-bot.php?token={bot_token}
#
Expand Down
Empty file added examples/InlineQueriesBot.php
Empty file.
4 changes: 2 additions & 2 deletions examples/WelcomeBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
# Check authentication
if ($_REQUEST['token'] == getenv('BOT_TOKEN'))
{
$Bot = new TelegramBot(getenv('BOT_TOKEN'), new BotSettings(new WelcomeBot(getenv('LOGS_CHAT_ID'))));
$Bot = new TelegramBot(getenv('BOT_TOKEN'), new WelcomeBot(getenv('LOGS_CHAT_ID'), new BotSettings()));

# Process Webhook Update
$Bot->OnWebhookUpdate(file_get_contents('php://input'));
$Bot->OnWebhookUpdate();
}

# Note: When you send setWebhook method, Take care that chat_member updates should be allowed
Expand Down
39 changes: 23 additions & 16 deletions src/TelegramBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,54 +127,54 @@ protected function OnUpdate(\stdClass $update) : bool
# Calling update handler
switch ($update)
{
case property_exists($update, 'message') && !array_search('message', $this->Settings->AllowedUpdates):
case property_exists($update, 'message') && array_search('message', $this->Settings->AllowedUpdates) !== false:
if ($this->Settings->AutoSaveBotUsers && !array_search($update->message->from->id, $this->Settings->BotUsers))
{
array_push($this->Settings->BotUsers, $update->message->from->id);
}
return $this->UpdatesHandler->MessageHandler($update->message);

case property_exists($update, 'edited_message') && !array_search('edited_message', $this->Settings->AllowedUpdates):
case property_exists($update, 'edited_message') && (array_search('edited_message', $this->Settings->AllowedUpdates) !== false):
return $this->UpdatesHandler->EditedMessageHandler($update->edited_message);


case property_exists($update, 'channel_post') && !array_search('channel_post', $this->Settings->AllowedUpdates):
case property_exists($update, 'channel_post') && (array_search('channel_post', $this->Settings->AllowedUpdates) !== false):
return $this->UpdatesHandler->ChannelPostHandler($update->channel_post);

case property_exists($update, 'edited_channel_post') && !array_search('edited_channel_post', $this->Settings->AllowedUpdates):
case property_exists($update, 'edited_channel_post') && (array_search('edited_channel_post', $this->Settings->AllowedUpdates) !== false):
return $this->UpdatesHandler->EditedChannelPostHandler($update->edited_channel_post);


case property_exists($update, 'inline_query') && !array_search('inline_query', $this->Settings->AllowedUpdates):
case property_exists($update, 'inline_query') && (array_search('inline_query', $this->Settings->AllowedUpdates) !== false):
return $this->UpdatesHandler->InlineQueryHandler($update->inline_query);

case property_exists($update, 'chosen_inline_query') && !array_search('chosen_inline_query', $this->Settings->AllowedUpdates):
case property_exists($update, 'chosen_inline_query') && (array_search('chosen_inline_query', $this->Settings->AllowedUpdates) !== false):
return $this->UpdatesHandler->ChosenInlineQueryHandler($update->chosen_inline_query);


case property_exists($update, 'callback_query') && !array_search('callback_query', $this->Settings->AllowedUpdates):
case property_exists($update, 'callback_query') && (array_search('callback_query', $this->Settings->AllowedUpdates) !== false):
return $this->UpdatesHandler->CallbackQueryHandler($update->callback_query);


case property_exists($update, 'my_chat_member') && !array_search('my_chat_member', $this->Settings->AllowedUpdates):
case property_exists($update, 'my_chat_member') && (array_search('my_chat_member', $this->Settings->AllowedUpdates) !== false):
return $this->UpdatesHandler->MyChatMemberHandler($update->my_chat_member);

case property_exists($update, 'chat_member') && !array_search('chat_member', $this->Settings->AllowedUpdates):
case property_exists($update, 'chat_member') && (array_search('chat_member', $this->Settings->AllowedUpdates) !== false):
return $this->UpdatesHandler->ChatMemberHandler($update->chat_member);

case property_exists($update, 'chat_join_request') && !array_search('chat_join_request', $this->Settings->AllowedUpdates):
case property_exists($update, 'chat_join_request') && (array_search('chat_join_request', $this->Settings->AllowedUpdates) !== false):
return $this->UpdatesHandler->ChatJoinRequestHandler($update->chat_join_request);


case property_exists($update, 'shipping_query') && !array_search('shipping_query', $this->Settings->AllowedUpdates):
case property_exists($update, 'shipping_query') && (array_search('shipping_query', $this->Settings->AllowedUpdates) !== false):
return $this->UpdatesHandler->ShippingQueryHandler($update->shipping_query);

case property_exists($update, 'pre_checkout_query') && !array_search('pre_checkout_query', $this->Settings->AllowedUpdates):
case property_exists($update, 'pre_checkout_query') && (array_search('pre_checkout_query', $this->Settings->AllowedUpdates) !== false):
return $this->UpdatesHandler->PreCheckoutQueryHandler($update->pre_checkout_query);

default:
# This means Library version is out-dated, Or it's a faked update
error_log('Update type not allowed!');
error_log("ERROR: Update type is not allowed or unknown!");
return false;
}
}
Expand All @@ -194,7 +194,7 @@ public function OnWebhookUpdate() : bool
if ($_GET['token_hash'] != hash($this->HashingMethod, $this->Token))
{
# Fake update
error_log("Unauthorized, Received token_hash={$_GET['token_hash']}");
error_log("ERROR[401]: Received invalid token_hash={$_GET['token_hash']}");
http_response_code(401);
return false;
}
Expand Down Expand Up @@ -232,6 +232,8 @@ public function ReceiveUpdates(int $limit = 100, int $offset = 0) : bool
# A function to send messages to all bot users
public function BroudcastMessage(string $text, array $reply_markup = [], string $parse_mode = 'HTML', bool $disable_notification = false)
{
$usersCnt = count($this->Settings->BotUsers);
error_log("INFO: Broadcasting to {$usersCnt} users");
foreach ($this->Settings->BotUsers as $userId)
{
$this->SendMessage([
Expand Down Expand Up @@ -267,16 +269,19 @@ public function WebhookResponse(string $method, array $params = []) : void
header('Content-Type: application/json');
header('Content-Length: ' . strlen($payload));

error_log("INFO: Webhook responsed method {$method}");
http_response_code(200);

echo $payload;
}

public function SetBotWebhook(string $host, int $max_connections = 40, bool $auth = true)
{
$newWebhookUrl = $host . ($auth ? '?token_hash=' . hash($this->HashingMethod, $this->Token) : '');
error_log("INFO: Webhook was set to {$newWebhookUrl}");
$this->Settings->CheckUpdates = $auth;
return $this->SetWebhook([
'url' => $host . ($auth ? '?token_hash=' . hash($this->HashingMethod, $this->Token) : ''),
'url' => $newWebhookUrl,
'max_connections' => $max_connections,
'allowed_updates' => json_encode($this->Settings->AllowedUpdates)
]);
Expand Down Expand Up @@ -308,9 +313,11 @@ public function __call(string $method, array $params) : mixed
# Flood error
if (property_exists($object->parameters, 'retry_after'))
{
$secondsToWait = $object->parameters->retry_after * 1000000;
error_log("WARNING: Flood error, Retry after {$secondsToWait}");
if ($this->Settings->AutoHandleFloodException)
{
usleep($object->parameters->retry_after * 1000000 + 1000000);
usleep($secondsToWait + 1000000);
# Recall same method
$this->$method($params[0] ?? []);
}
Expand Down
Empty file added test/InlineQueriesTest.php
Empty file.

0 comments on commit 3193b94

Please sign in to comment.