PHP code example of telegram-bot-php / core

1. Go to this page and download the library: Download telegram-bot-php/core 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/ */

    

telegram-bot-php / core example snippets



in_id = 123456789;
$bot_token = 'your_bot_token';

\TelegramBot\Telegram::setToken($bot_token);
\TelegramBot\CrashPad::setDebugMode($admin_id);

$result = \TelegramBot\Request::sendMessage([
   'chat_id' => $admin_id,
   'text' => 'text',
]);

echo $result->getRawData(false); // {"ok": true, "result": {...}}


egramBot\Telegram::setToken($bot_token);
$response = \TelegramBot\Request::setWebhook([
   'url' => 'https://your-domain.com/webhook/' . $bot_token,
]);

if ($response->isOk()) {
   echo $response->getDescription();
   exit(0);
}

\TelegramBot\Request::setWebhook([
   'url' => 'https://your-domain.com/webhook/' . $bot_token,
   'certificate' => 'path/to/certificate.pem',
]);

\TelegramBot\Request::deleteWebhook();



use TelegramBot\Entities\Update;
use TelegramBotTest\EchoBot\Plugins\MainPlugin;

class Handler extends \TelegramBot\UpdateHandler {

   public function __process(Update $update): void {
      self::addPlugins([
         MainPlugin::class,
      ]);
   }

}

$updateHandler->filterIncomingUpdates([
   Update::TYPE_MESSAGE,
   Update::TYPE_CALLBACK_QUERY,
]);

$updateHandler->filterIncomingUpdates([
   Update::TYPE_MESSAGE => function (\TelegramBot\Entities\Update $update) {
      return $update->getMessage()->getChat()->getId() === 259760855;
   }
]);



use TelegramBot\Entities\Message;
use TelegramBot\Entities\WebAppData;

class MainPlugin extends \TelegramBot\Plugin {

   public function onMessage(int $update_id, Message $message): \Generator {
      if ($message->getText() === '/start') {
         yield \TelegramBot\Request::sendMessage([
            'chat_id' => $message->getChat()->getId(),
            'text' => 'Hello, ' . $message->getFrom()->getFirstName(),
         ]);
      }

      if ($message->getText() === '/ping') {
         yield \TelegramBot\Request::sendMessage([
            'chat_id' => $message->getChat()->getId(),
            'text' => 'pong',
         ]);
      }
   }

   public function onWebAppData(int $update_id, WebAppData $webAppData): \Generator {
      yield \TelegramBot\Request::sendMessage([
         'chat_id' => $webAppData->getUser()->getId(),
         'text' => 'Hello, ' . $webAppData->getUser()->getFirstName(),
      ]);
   }

}

$commands = new class() extends \TelegramBot\Plugin {

   public function onUpdate(\TelegramBot\Entities\Update $update): \Generator {
      // Write your code here
   }

};

$admin = new class() extends \TelegramBot\Plugin {

   // TODO: Write your code here

};

(new \TelegramBot\UpdateHandler())->addPlugins([$commands, $admin])->resolve();

class SomePlugin extends \TelegramBot\Plugin {

   public function onUpdate(Update $update): \Generator {}

   public function onMessage(int $update_id, Message $message): \Generator {}

   public function onEditedMessage(int $update_id, EditedMessage $editedMessage): \Generator {}

   public function onChannelPost(int $update_id, ChannelPost $channelPost): \Generator {}

   public function onEditedChannelPost(int $update_id, EditedChannelPost $editedChannelPost): \Generator {}

   public function onInlineQuery(int $update_id, InlineQuery $inlineQuery): \Generator {}

   public function onChosenInlineResult(int $update_id, ChosenInlineResult $chosenInlineResult): \Generator {}

   public function onCallbackQuery(int $update_id, CallbackQuery $callbackQuery): \Generator {}

   public function onShippingQuery(int $update_id, ShippingQuery $shippingQuery): \Generator {}

   public function onPreCheckoutQuery(int $update_id, PreCheckoutQuery $preCheckoutQuery): \Generator {}

   public function onPoll(int $update_id, Poll $poll): \Generator {}

   public function onPollAnswer(int $update_id, PollAnswer $pollAnswer): \Generator {}

   public function onWebAppData(int $update_id, WebAppData $webAppData): \Generator {}

}

\TelegramBot\CrashPad::setDebugMode(259760855);

sudo apt-get install curl php-curl
curl -s https://getcomposer.org/installer | php
php composer.phar install

php composer.phar