Minor Changes
This commit is contained in:
parent
e3ed0529b9
commit
da6abceed2
1 changed files with 39 additions and 18 deletions
|
@ -8,6 +8,7 @@
|
||||||
use CurlHandle;
|
use CurlHandle;
|
||||||
use Exception;
|
use Exception;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
use LogLib\Log;
|
||||||
use TgBotLib\Abstracts\ChatMemberStatus;
|
use TgBotLib\Abstracts\ChatMemberStatus;
|
||||||
use TgBotLib\Abstracts\EventType;
|
use TgBotLib\Abstracts\EventType;
|
||||||
use TgBotLib\Exceptions\TelegramException;
|
use TgBotLib\Exceptions\TelegramException;
|
||||||
|
@ -52,11 +53,6 @@
|
||||||
*/
|
*/
|
||||||
private $last_update_id;
|
private $last_update_id;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var CurlHandle|null
|
|
||||||
*/
|
|
||||||
private $curl_handle;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var CommandInterface[]
|
* @var CommandInterface[]
|
||||||
*/
|
*/
|
||||||
|
@ -171,13 +167,14 @@
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Log::debug('net.nosial.tgbotlib', sprintf('=> %s(%s)', $method, json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)));
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
print_r($response . PHP_EOL);
|
|
||||||
if ($response === false)
|
if ($response === false)
|
||||||
throw new TelegramException('curl error: ' . curl_error($ch), curl_errno($ch));
|
throw new TelegramException('curl error: ' . curl_error($ch), curl_errno($ch));
|
||||||
|
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
$parsed = json_decode($response, true);
|
$parsed = json_decode($response, true);
|
||||||
|
Log::debug('net.nosial.tgbotlib', sprintf('<= %s', $response));
|
||||||
if($parsed['ok'] === false)
|
if($parsed['ok'] === false)
|
||||||
throw new TelegramException($parsed['description'], $parsed['error_code']);
|
throw new TelegramException($parsed['description'], $parsed['error_code']);
|
||||||
|
|
||||||
|
@ -210,13 +207,14 @@
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Log::debug('net.nosial.tgbotlib', sprintf('=> %s {%s}', $method, json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)));
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
print_r($response);
|
|
||||||
if ($response === false)
|
if ($response === false)
|
||||||
throw new TelegramException('curl error: ' . curl_error($ch), curl_errno($ch));
|
throw new TelegramException('curl error: ' . curl_error($ch), curl_errno($ch));
|
||||||
|
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
$parsed = json_decode($response, true);
|
$parsed = json_decode($response, true);
|
||||||
|
Log::debug('net.nosial.tgbotlib', sprintf('<= %s', $response));
|
||||||
if($parsed['ok'] === false)
|
if($parsed['ok'] === false)
|
||||||
throw new TelegramException($parsed['description'], $parsed['error_code']);
|
throw new TelegramException($parsed['description'], $parsed['error_code']);
|
||||||
|
|
||||||
|
@ -236,6 +234,21 @@
|
||||||
$this->command_handlers[$command] = $handler;
|
$this->command_handlers[$command] = $handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets multiple command handlers at once
|
||||||
|
*
|
||||||
|
* @param array $handlers
|
||||||
|
* @return void
|
||||||
|
* @noinspection PhpUnused
|
||||||
|
*/
|
||||||
|
public function setCommandHandlers(array $handlers): void
|
||||||
|
{
|
||||||
|
foreach($handlers as $command => $handler)
|
||||||
|
{
|
||||||
|
$this->command_handlers[$command] = $handler;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets an event handler for the specified event
|
* Sets an event handler for the specified event
|
||||||
*
|
*
|
||||||
|
@ -246,20 +259,28 @@
|
||||||
*/
|
*/
|
||||||
public function setEventHandler(string $event, EventInterface $handler): void
|
public function setEventHandler(string $event, EventInterface $handler): void
|
||||||
{
|
{
|
||||||
switch($event)
|
if(!in_array($event, EventType::All))
|
||||||
{
|
throw new InvalidArgumentException('Invalid event type: ' . $event);
|
||||||
case EventType::GenericUpdate:
|
|
||||||
case EventType::Message:
|
|
||||||
case EventType::EditedMessage:
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw new InvalidArgumentException('Invalid event type');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->event_handlers[$event] = $handler;
|
$this->event_handlers[$event] = $handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets multiple event handlers at once
|
||||||
|
*
|
||||||
|
* @param array $handlers
|
||||||
|
* @return void
|
||||||
|
* @noinspection PhpUnused
|
||||||
|
*/
|
||||||
|
public function setEventHandlers(array $handlers): void
|
||||||
|
{
|
||||||
|
foreach($handlers as $event => $handler)
|
||||||
|
{
|
||||||
|
if(!in_array($event, EventType::All))
|
||||||
|
throw new InvalidArgumentException('Invalid event type: ' . $event);
|
||||||
|
$this->event_handlers[$event] = $handler;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the command handler for the specified command
|
* Removes the command handler for the specified command
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue