<?php
namespace Teleguru\Client\Console\Command;
use File\File;
use Socket\Socket;
use Socket\ServerSocket;
use Teleguru\Client\Client;
use Teleguru\Client\Utils\Log;
use Symfony\Component\Dotenv\Dotenv;
use Teleguru\Client\Auth\Session\Session;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Undocumented class
*/
class ServeCommand extends Command
{
protected $rootDir;
public function __construct($rootDir)
{
parent::__construct();
$this->rootDir = $rootDir;
}
public function configure()
{
$this
->setName("serve");
}
public function execute(InputInterface $in, OutputInterface $out)
{
Log::d("Run server socket..");
$server = new ServerSocket(4001);
$dotenv = new Dotenv();
$dotenv->load($this->rootDir."/.env");
$client = new Client([
"config" => [
"api_id" => getenv("API_ID"),
"api_hash" => getenv("API_HASH")
]
]);
$client->setRootDir($this->rootDir);
$client->boot();
$client->setSession(Session::factory( $this->rootDir."/temp/session.dat" ));
// $client->authorize();
$server->run(function(Socket $socket) use($client)
{
try
{
$body = $socket->read(1024000);
if (0 == \strlen($body))
return true;
$data = \json_decode($body, true);
switch(isset($data['method']) && is_string($data['method']) ? $data['method'] : null)
{
case "resolveUsername":
{
$content = $client->resolveUsername($data['username']);
//\var_dump($content);
$socket->write([
\json_encode($content)
]);
}
break;
case "sendCode":
{
$content = $client->__sendMessage(
$data['username'],
"Код подтверждения: " . $data['code'] . ". Никому не давайте код!"
. "\n\nЭтот код используется для аутнификации вашей учетной записи в сервисе teleguru."
. "\n\nЕсли вы не запрашивали код, проигнорируйте сообщение."
);
// \var_dump($content);
}
break;
default:
{
}
}
} catch(\Exception $e) {
}
$client->getSession()->update();
return true;
});
}
}