PHP code example of lex19 / telegram

1. Go to this page and download the library: Download lex19/telegram library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lex19 / telegram example snippets


// routes/web.php

use lex19\Telegram\Facades\Telegram;
use App\Telegram\Receptions\MainReception;

Route::any('hook', function(){
    return Telegram::hook(MainReception::class);
});


// AnyClass.php

use lex19\Telegram\Facades\Telegram;

Telegram::send([
    "chat_id" => 123456,
    "text" => "hello stranger!"
]);


// app/Telegram/Commands/CommandName.php

namespace App\Telegram\Commands;

use lex19\Telegram\Command;

class CommandName extends Command
{

  public function handle()
  {
    return $this->telegram->send('hello');
  }

}

// app/Telegram/Receptions/MainReception.php

namespace App\Telegram\Receptions;

use lex19\Telegram\BaseReception;

class MainReception extends BaseReception
{

  protected $commands = [
    'say hello' => \App\Telegram\Commands\CommandName::class
  ];

}


// app/Telegram/Receptions/MainReception.php

namespace App\Telegram\Receptions;

use lex19\Telegram\BaseReception;
use App\Telegram\Commands\CommandName;

class MainReception extends BaseReception
{

  protected $commands = [
    'say hello' => CommandName::class,
    'say hello myFunc' => [CommandName::class, 'myFunc'], // you use not just 'handle' method
  ];


    public function init()
    {
        $this->add('dynamic hello', [CommandName::class, 'dynamicHello']);
        $this->add([
            'dynamic hello 1' => [CommandName::class, 'dynamicHello1'],
            'dynamic hello 2' => [CommandName::class, 'dynamicHello2'],
        ]);
        $command = new CommandName;
        $this->add('dynamic hello 3', [$command, 'dynamicHello3']);
    }

}


// app/Telegram/Commands/CommandName.php

namespace App\Telegram\Commands;

use lex19\Telegram\Command;

class CommandName extends Command
{

  public function handle()
  {
    $this->setNext([CommandName::class, 'next']); // any next message will trigger this command

    return $this->telegram->send('hello');
  }


  public function next()
  {
    $text = $this->telegram->text;
    return $this->telegram->send("I don't care what you wrote there, but here it is: $text");
  }

}


bash
php artisan telegram:install
bash
php artisan telegram:command CommandName