Updated method \TgBotLib > Bot > handleGetUpdates() to handle exceptions

This commit is contained in:
Netkas 2023-08-09 20:02:59 -04:00
parent 8a4dfddae6
commit fbc472d8de
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
6 changed files with 21 additions and 27 deletions

6
.idea/php.xml generated
View file

@ -14,12 +14,12 @@
<include_path>
<path value="/usr/share/php" />
<path value="/etc/ncc" />
<path value="/var/ncc/packages/net.nosial.tamerlib=1.0.0" />
<path value="/var/ncc/packages/net.nosial.loglib=1.0.1" />
<path value="/var/ncc/packages/net.nosial.tamerlib=2.1.5" />
<path value="/var/ncc/packages/net.nosial.loglib=1.0.2" />
<path value="/var/ncc/packages/net.nosial.tempfile=1.1.0" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="8.0">
<component name="PhpProjectSharedConfiguration" php_language_level="8.1">
<option name="suggestChangeDefaultLanguageLevel" value="false" />
</component>
<component name="PhpStanOptionsConfiguration">

View file

@ -62,6 +62,7 @@ input objects for methods that require input objects.
* Updated class type to `final class` in `\TgBotLib\Enums > StickerType`
* Updated class type to `final class` in `\TgBotLib\Enums > ThumbnailMimeType`
* Updated class type to `final class` in `\TgBotLib\Enums > UpdateEventType`
* Updated method `\TgBotLib > Bot > handleGetUpdates()` to handle exceptions
## [6.6.0] - 2023-04-10

View file

@ -505,14 +505,26 @@
* @return void
* @throws TelegramException
*/
public function handleGetUpdates(bool $run_forever = false): void
public function handleGetUpdates(bool $run_forever=false): void
{
do
{
$updates = $this->getUpdates();
foreach($updates as $update)
{
$this->handleUpdate($update);
try
{
$this->handleUpdate($update);
}
catch(Exception $e)
{
Log::error('net.nosial.tgbotlib', sprintf('Unhandled exception while handling update: %s', $e->getMessage()), $e);
if(!$run_forever)
{
throw new TelegramException('Unhandled exception while handling update', 0, $e);
}
}
}
} while($run_forever);
}

View file

@ -18,9 +18,9 @@
public function handle(Bot $bot, Update $update): void
{
// Usage: /hash <text>
$data = str_replace('/hash ', '', $update->getMessage()->getText());
$data = str_replace('/hash ', '', $update->getMessage()?->getText());
$bot->sendMessage(
$update->getMessage()->getChat()->getId(), md5($data)
$update->getMessage()?->getChat()?->getId(), md5($data)
);
}
}

View file

@ -19,7 +19,7 @@
{
// reply to the incoming message
$bot->sendMessage(
$update->getMessage()->getChat()->getId(), 'Hello, ' . $update->getMessage()->getFrom()->getFirstName()
$update->getMessage()?->getChat()?->getId(), 'Hello, ' . $update->getMessage()?->getFrom()?->getFirstName()
);
}
}

View file

@ -1,19 +0,0 @@
<?php
require __DIR__ . DIRECTORY_SEPARATOR . 'autoload.php';
import('net.nosial.tamerlib');
$bot = new TgBotLib\Bot(getenv('BOT_TOKEN'));
TamerLib\Tamer::init(\TamerLib\Abstracts\ProtocolType::Gearman, ['127.0.0.1:4730']);
TamerLib\Tamer::addWorker(__DIR__ . DIRECTORY_SEPARATOR . 'worker.php', 10);
var_dump('Starting workers');
TamerLib\Tamer::startWorkers();
while(true)
{
foreach($bot->getUpdates() as $update)
{
TamerLib\Tamer::do(TamerLib\Objects\Task::create('handle_update', json_encode($update->toArray())));
}
}