Introduce console kernel class and handle cli update requests

parent a2f6ee78
......@@ -45,7 +45,7 @@ class ChatsCommand extends AdminCommand
/**
* Command execute method
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
......
......@@ -326,7 +326,7 @@ class CleanupCommand extends AdminCommand
/**
* Execution if MySQL is required but not available
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
*/
public function executeNoDb()
{
......@@ -345,7 +345,7 @@ class CleanupCommand extends AdminCommand
/**
* Command execute method
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
......
......@@ -12,7 +12,7 @@ namespace Longman\TelegramBot\Commands\AdminCommands;
use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Http\ServerResponse;
use Longman\TelegramBot\Http\Response;
use Longman\TelegramBot\Http\Client;
/**
......@@ -48,7 +48,7 @@ class SendtoallCommand extends AdminCommand
/**
* Execute command
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
......@@ -59,7 +59,7 @@ class SendtoallCommand extends AdminCommand
return $this->replyToChat('Usage: ' . $this->getUsage());
}
/** @var ServerResponse[] $results */
/** @var Response[] $results */
$results = Client::sendToActiveChats(
'sendMessage', //callback function to execute (see Request.php methods)
['text' => $text], //Param to evaluate the request
......
......@@ -54,7 +54,7 @@ class SendtochannelCommand extends AdminCommand
/**
* Command execute method
*
* @return \Longman\TelegramBot\Http\ServerResponse|mixed
* @return \Longman\TelegramBot\Http\Response|mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
......@@ -275,7 +275,7 @@ class SendtochannelCommand extends AdminCommand
* @param \Longman\TelegramBot\Entities\Message $message
* @param array $data
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
protected function sendBack(Message $message, array $data)
......
......@@ -52,7 +52,7 @@ class WhoisCommand extends AdminCommand
/**
* Command execute method
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
......
......@@ -143,7 +143,7 @@ abstract class Command
/**
* Pre-execute command
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function preExecute()
......@@ -176,7 +176,7 @@ abstract class Command
/**
* Execute command
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
abstract public function execute();
......@@ -184,7 +184,7 @@ abstract class Command
/**
* Execution if MySQL is required but not available
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function executeNoDb()
......@@ -393,7 +393,7 @@ abstract class Command
* @param string $text
* @param array $data
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
*/
public function replyToChat($text, array $data = [])
{
......@@ -413,7 +413,7 @@ abstract class Command
* @param string $text
* @param array $data
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
*/
public function replyToUser($text, array $data = [])
{
......
......@@ -20,7 +20,7 @@ abstract class SystemCommand extends Command
* Although system commands should just work and return a successful ServerResponse,
* each system command can override this method to add custom functionality.
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
*/
public function execute()
{
......
......@@ -42,7 +42,7 @@ class GenericmessageCommand extends SystemCommand
/**
* Execution if MySQL is required but not available
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
*/
public function executeNoDb()
{
......@@ -53,7 +53,7 @@ class GenericmessageCommand extends SystemCommand
/**
* Execute command
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
......
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Console;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Http\Client;
use Longman\TelegramBot\Http\Request;
use Longman\TelegramBot\Http\Response;
use Longman\TelegramBot\Telegram;
class Kernel
{
/**
* The application implementation.
*
* @var \Longman\TelegramBot\Telegram
*/
protected $app;
/**
* Create a new HTTP kernel instance.
*
* @param \Longman\TelegramBot\Telegram $app
*
* @return void
*/
public function __construct(Telegram $app)
{
$this->app = $app;
}
/**
* Handle an incoming HTTP request.
*
* @param \Longman\TelegramBot\Http\Request $request
*
* @param null $limit
* @param null $timeout
* @return \Longman\TelegramBot\Http\Response
*
*/
public function handle(Request $request, $limit = null, $timeout = null)
{
if (! DB::isDbConnected() && ! $this->app->getupdates_without_database) {
return new Response(
[
'ok' => false,
'description' => 'getUpdates needs MySQL connection! (This can be overridden - see documentation)',
],
$this->bot_username
);
}
$offset = 0;
//Take custom input into account.
if ($custom_input = $this->app->getCustomInput()) {
$response = new Response(json_decode($custom_input, true), $this->app->getBotUsername());
} else {
if (DB::isDbConnected()) {
//Get last update id from the database
$last_update = DB::selectTelegramUpdate(1);
$last_update = reset($last_update);
$this->app->last_update_id = isset($last_update['id']) ? $last_update['id'] : null;
}
if ($this->app->last_update_id !== null) {
$offset = $this->app->last_update_id + 1; //As explained in the telegram bot API documentation
}
$response = Client::getUpdates(
[
'offset' => $offset,
'limit' => $limit,
'timeout' => $timeout,
]
);
}
if ($response->isOk()) {
$results = $response->getResult();
//Process all updates
/** @var Update $result */
foreach ($results as $result) {
$this->app->processUpdate($result);
}
if (! DB::isDbConnected() && ! $custom_input && $this->app->last_update_id !== null && $offset === 0) {
//Mark update(s) as read after handling
Client::getUpdates(
[
'offset' => $this->app->last_update_id + 1,
'limit' => 1,
'timeout' => $timeout,
]
);
}
}
return $response;
}
}
......@@ -48,7 +48,7 @@ class PreCheckoutQuery extends Entity
* @param bool $ok
* @param array $data
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
*/
public function answer($ok, array $data = [])
{
......
......@@ -45,7 +45,7 @@ class ShippingQuery extends Entity
* @param bool $ok
* @param array $data
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
*/
public function answer($ok, array $data = [])
{
......
This diff is collapsed.
......@@ -37,9 +37,9 @@ class Kernel
/**
* Handle an incoming HTTP request.
*
* @param \Longman\TelegramBot\Request2 $request
* @param \Longman\TelegramBot\Http\Request $request
*
* @return \Longman\TelegramBot\Response
* @return \Longman\TelegramBot\Http\Response
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
......
......@@ -24,7 +24,7 @@ use Longman\TelegramBot\Entities\WebhookInfo;
*
* @todo method ResponseParameters getParameters() Field which can help to automatically handle the error
*/
class ServerResponse
class Response
{
/**
* ServerResponse constructor.
......
......@@ -16,12 +16,12 @@ defined('TB_BASE_COMMANDS_PATH') || define('TB_BASE_COMMANDS_PATH', TB_BASE_PATH
use Exception;
use Illuminate\Container\Container;
use Longman\TelegramBot\Commands\Command;
use Longman\TelegramBot\Console\Kernel as ConsoleKernel;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Http\Kernel;
use Longman\TelegramBot\Http\Client;
use Longman\TelegramBot\Http\Kernel;
use Longman\TelegramBot\Http\Request;
use Longman\TelegramBot\Http\ServerResponse;
use PDO;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
......@@ -130,7 +130,7 @@ class Telegram extends Container
/**
* ServerResponse of the last Command execution
*
* @var \Longman\TelegramBot\Http\ServerResponse
* @var \Longman\TelegramBot\Http\Response
*/
protected $last_command_response;
......@@ -153,7 +153,7 @@ class Telegram extends Container
*
* @var bool
*/
protected $getupdates_without_database = false;
public $getupdates_without_database = false;
/**
* Last update ID
......@@ -161,7 +161,7 @@ class Telegram extends Container
*
* @var integer
*/
protected $last_update_id = null;
public $last_update_id = null;
/**
* Telegram constructor.
......@@ -340,7 +340,7 @@ class Telegram extends Container
/**
* Get the ServerResponse of the last Command execution
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
*/
public function getLastCommandResponse()
{
......@@ -353,7 +353,7 @@ class Telegram extends Container
* @param int|null $limit
* @param int|null $timeout
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function handleGetUpdates($limit = null, $timeout = null)
......@@ -362,63 +362,10 @@ class Telegram extends Container
throw new TelegramException('Bot Username is not defined!');
}
if (! DB::isDbConnected() && ! $this->getupdates_without_database) {
return new ServerResponse(
[
'ok' => false,
'description' => 'getUpdates needs MySQL connection! (This can be overridden - see documentation)',
],
$this->bot_username
);
}
$offset = 0;
//Take custom input into account.
if ($custom_input = $this->getCustomInput()) {
$response = new ServerResponse(json_decode($custom_input, true), $this->bot_username);
} else {
if (DB::isDbConnected()) {
//Get last update id from the database
$last_update = DB::selectTelegramUpdate(1);
$last_update = reset($last_update);
$this->last_update_id = isset($last_update['id']) ? $last_update['id'] : null;
}
if ($this->last_update_id !== null) {
$offset = $this->last_update_id + 1; //As explained in the telegram bot API documentation
}
/** @var \Longman\TelegramBot\Console\Kernel $kernel */
$kernel = $this->make(ConsoleKernel::class);
$response = Client::getUpdates(
[
'offset' => $offset,
'limit' => $limit,
'timeout' => $timeout,
]
);
}
if ($response->isOk()) {
$results = $response->getResult();
//Process all updates
/** @var Update $result */
foreach ($results as $result) {
$this->processUpdate($result);
}
if (! DB::isDbConnected() && ! $custom_input && $this->last_update_id !== null && $offset === 0) {
//Mark update(s) as read after handling
Client::getUpdates(
[
'offset' => $this->last_update_id + 1,
'limit' => 1,
'timeout' => $timeout,
]
);
}
}
$response = $kernel->handle(Request::capture(), $limit, $timeout);
return $response;
}
......@@ -461,7 +408,7 @@ class Telegram extends Container
*
* @param \Longman\TelegramBot\Entities\Update $update
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function processUpdate(Update $update)
......@@ -833,7 +780,7 @@ class Telegram extends Container
*/
public function getVersion()
{
return $this->version;
return self::VERSION;
}
/**
......@@ -842,7 +789,7 @@ class Telegram extends Container
* @param string $url
* @param array $data Optional parameters.
*
* @return \Longman\TelegramBot\Http\ServerResponse
* @return \Longman\TelegramBot\Http\Response
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function setWebhook($url, array $data = [])
......
......@@ -13,7 +13,7 @@
namespace Longman\TelegramBot\Tests\Unit;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Http\ServerResponse;
use Longman\TelegramBot\Http\Response;
use Longman\TelegramBot\Http\Client;
/**
......@@ -42,7 +42,7 @@ class ServerResponseTest extends TestCase
public function testSendMessageOk()
{
$result = $this->sendMessageOk();
$server = new ServerResponse(json_decode($result, true), 'testbot');
$server = new Response(json_decode($result, true), 'testbot');
$server_result = $server->getResult();
self::assertTrue($server->isOk());
......@@ -76,7 +76,7 @@ class ServerResponseTest extends TestCase
public function testSendMessageFail()
{
$result = $this->sendMessageFail();
$server = new ServerResponse(json_decode($result, true), 'testbot');
$server = new Response(json_decode($result, true), 'testbot');
self::assertFalse($server->isOk());
self::assertNull($server->getResult());
......@@ -92,7 +92,7 @@ class ServerResponseTest extends TestCase
public function testSetWebhookOk()
{
$result = $this->setWebhookOk();
$server = new ServerResponse(json_decode($result, true), 'testbot');
$server = new Response(json_decode($result, true), 'testbot');
self::assertTrue($server->isOk());
self::assertTrue($server->getResult());
......@@ -112,7 +112,7 @@ class ServerResponseTest extends TestCase
public function testSetWebhookFail()
{
$result = $this->setWebhookFail();
$server = new ServerResponse(json_decode($result, true), 'testbot');
$server = new Response(json_decode($result, true), 'testbot');
self::assertFalse($server->isOk());
self::assertNull($server->getResult());
......@@ -172,7 +172,7 @@ class ServerResponseTest extends TestCase
public function testGetUpdatesArray()
{
$result = $this->getUpdatesArray();
$server = new ServerResponse(json_decode($result, true), 'testbot');
$server = new Response(json_decode($result, true), 'testbot');
self::assertCount(4, $server->getResult());
self::assertInstanceOf('\Longman\TelegramBot\Entities\Update', $server->getResult()[0]);
......@@ -186,7 +186,7 @@ class ServerResponseTest extends TestCase
public function testGetUpdatesEmpty()
{
$result = $this->getUpdatesEmpty();
$server = new ServerResponse(json_decode($result, true), 'testbot');
$server = new Response(json_decode($result, true), 'testbot');
self::assertEmpty($server->getResult());
}
......@@ -221,7 +221,7 @@ class ServerResponseTest extends TestCase
public function testGetUserProfilePhotos()
{
$result = $this->getUserProfilePhotos();
$server = new ServerResponse(json_decode($result, true), 'testbot');
$server = new Response(json_decode($result, true), 'testbot');
$server_result = $server->getResult();
$photos = $server_result->getPhotos();
......@@ -251,7 +251,7 @@ class ServerResponseTest extends TestCase
public function testGetFile()
{
$result = $this->getFile();
$server = new ServerResponse(json_decode($result, true), 'testbot');
$server = new Response(json_decode($result, true), 'testbot');
self::assertInstanceOf('\Longman\TelegramBot\Entities\File', $server->getResult());
}
......@@ -261,7 +261,7 @@ class ServerResponseTest extends TestCase
//setWebhook ok
$fake_response = Client::generateGeneralFakeServerResponse();
$server = new ServerResponse($fake_response, 'testbot');
$server = new Response($fake_response, 'testbot');
self::assertTrue($server->isOk());
self::assertTrue($server->getResult());
......@@ -271,7 +271,7 @@ class ServerResponseTest extends TestCase
//sendMessage ok
$fake_response = Client::generateGeneralFakeServerResponse(['chat_id' => 123456789, 'text' => 'hello']);
$server = new ServerResponse($fake_response, 'testbot');
$server = new Response($fake_response, 'testbot');
/** @var Message $server_result */
$server_result = $server->getResult();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment