Skip to content

Commit

Permalink
Updated laragram:start command to use longPolling method for local de…
Browse files Browse the repository at this point in the history
…velopment
  • Loading branch information
Mirmuxsin committed Jul 20, 2024
1 parent ebe1484 commit e7713e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 67 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ And here we go, you can start your bot now
---
## Usage:

- Local development:
```bash
php artisan laragram:start
```

- Methods:
```php
use Milly\Laragram\Laragram;
Expand Down
91 changes: 24 additions & 67 deletions src/app/Console/Commands/longpolling.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Milly\Laragram\app\Console\Commands;

use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Http\Request;
use Symfony\Component\Process\Process;

class longpolling extends Command
{

/**
* The name and signature of the console command.
*
Expand All @@ -19,89 +22,43 @@ class longpolling extends Command
*
* @var string
*/
protected $description = 'Run telegram bot on longpolling mode';
protected $description = 'Run telegram bot on long-polling mode';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
public function handle(): void
{
$this->info('Telegram bot starting on long-polling mode...');
$token = config('laragram.token');
$this->info('Waiting for updates...');
$offset = 0;

if (empty($token)) {
$this->error('Telegram bot token is required');
exit();
}
while (true) {

if (!$this->existTunnel()) return;
$url = $this->serve();
$updates = \Milly\Laragram\Laragram::getUpdates(
offset: $offset,
);

$this->info('Webhook address: ' . $url.config('laragram.url'));
file_get_contents("https://api.telegram.org/bot" . $token . "/setWebhook?url=" . $url.config('laragram.url'));
$setUrl = \Milly\Laragram\Laragram::setUrl($url);
$this->info('Webhook was set successfully');
$this->call('serve');
}
$request = new Client();

/**
* Check if exist localtunnel server
*
* @return boolean
*/
public function existTunnel()
{
$expression = '/([0-9]\.?)+$/'; // Regular expression. https://regex101.com/r/r8a7eB/1
if (isset($updates['result']) and !empty($updates['result'])) {
$this->info('Received updates: ' . json_encode($updates));
foreach ($updates['result'] as $update) {
$this->info('Processing update: ' . json_encode($update));
$this->newLine();

$process = new Process(['lt', '--version']);
$process->start();
$response = $request->post(config('app.url').config('laragram.url'), [
'form_params' => $update
]);

while ($process->isRunning()) {
sleep(1);
$output = $process->getOutput();

if (!empty($output)) break;
}
$this->info('Response: '.$response->getBody()->getContents());

if (!preg_match($expression, $output)) {
$this->warn("\nLocaltunnel is not found
\rPlease run this command in terminal:");
$this->info("npm install --global localtunnel");
$offset = $update['update_id'] + 1;
}
}

return false;
}

return true;
}

/**
* Start localtunnel server
*
* @var array $url
* @return string
*/
public function serve()
{
$expression = "/(https?:\/{2}[\w+-]+\.\w+\.\w+)$/"; // Regular expression. https://regex101.com/r/2c6sZC/1

$process = new Process(['lt', '--port', '8000']);
$process->start();

while ($process->isRunning()) {
sleep(1);
$output = $process->getOutput();

if (!empty($output)) break;
}

if (!preg_match($expression, $output, $url)) {
$this->error("\nLocaltunnel didn't return url");

exit();
}

return $url[0]; // return webhook payload url
}
}

0 comments on commit e7713e4

Please sign in to comment.