Romanovich095

SetTelegramBotsWebhook

Sep 10th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.65 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use GuzzleHttp\Client;
  6. use Illuminate\Console\Command;
  7.  
  8. class SetTelegramBotsWebhook extends Command
  9. {
  10.     /**
  11.      * The name and signature of the console command.
  12.      *
  13.      * @var string
  14.      */
  15.     protected $signature = 'tg:wh';
  16.  
  17.     /**
  18.      * The console command description.
  19.      *
  20.      * @var string
  21.      */
  22.     protected $description = 'Set Telegram Bots Webhook';
  23.  
  24.     private $actionUrl = 'https://api.telegram.org/bot%s/%s%s';
  25.  
  26.     /**
  27.      * @var Client
  28.      */
  29.     private $http;
  30.  
  31.     /**
  32.      * Create a new command instance.
  33.      *
  34.      * @return void
  35.      */
  36.     public function __construct()
  37.     {
  38.         parent::__construct();
  39.         $this->http = new Client(['verify' => false]);
  40.     }
  41.  
  42.     /**
  43.      * Execute the console command.
  44.      *
  45.      * @return int
  46.      */
  47.     public function handle()
  48.     {
  49.         \array_map(function ($bot) {
  50.             try {
  51.                 $this->http->get(\sprintf($this->actionUrl, $bot['token'], 'deleteWebhook', ''));
  52.                 $response = $this->http->get(
  53.                     \sprintf(
  54.                         $this->actionUrl,
  55.                         $bot['token'],
  56.                         'setWebhook',
  57.                         '?' . http_build_query([
  58.                             'url' => route($bot['webhook_url'])
  59.                         ])
  60.                     )
  61.                 );
  62.  
  63.                 $this->info($response->getBody()->getContents());
  64.             } catch (\Exception $exception) {
  65.                 $this->warn($exception->getMessage());
  66.             }
  67.         }, config('telegram.bots'));
  68.     }
  69. }
  70.  
Add Comment
Please, Sign In to add comment