PHP Classes

File: examples/functions.php

Recommend this page to a friend!
  Classes of Mateo   PHP Telegram Bot Handler   examples/functions.php   Download  
File: examples/functions.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Telegram Bot Handler
Create a Telegram bot to respond to certain events
Author: By
Last change:
Date: 1 year ago
Size: 1,372 bytes
 

Contents

Class file image Download
<?php

use Mateodioev\Bots\Telegram\{Methods, Buttons};
use
Mateodioev\TgHandler\Commands;

function
sendHello(Methods $bot, Commands $cmd): stdClass {
  return
$bot->sendMessage($cmd->getChatId(), 'Hello World!');
}

function
sendHelloWithButton(Methods $bot, Commands $cmd): stdClass {
 
$bot->AddOpt([
   
'reply_markup' => (string) Buttons::create()
      ->
addCeil(['text' => 'Open this link', 'url' => 'https://google.com'])
  ]);

  return
sendHello($bot, $cmd);
}

function
onText(Commands $cmd) {
  echo
'Function executed on text messages.' . PHP_EOL;
  echo
'User id: ' . $cmd->getUserId() . ' | Chat id: ' . $cmd->getChatId() . PHP_EOL;
  echo
'Text: ' . $cmd->getText() . PHP_EOL . PHP_EOL;
}

function
runBot(Commands $cmd) {
  try {
   
$cmd->Run(function (Commands $cmd) {
      echo
'Command result: ' . var_export($cmd->getFnResult(), true) . PHP_EOL;
    });
  } catch (\
Throwable $th) {
    echo
'Error: ' . $th->getMessage() . PHP_EOL;
  }
}

function
noWebhook(Methods $bot, Commands $cmd) {
 
$payload = ['offset' => 0];
 
// Infinite loop
 
while (true) {
   
$updates = $bot->getUpdates($payload);

    if (!
$updates->ok) {
      echo
'Error: ' . $updates->description . PHP_EOL;
      break;
    }

    foreach (
$updates->result as $up) {
     
$payload['offset'] = $up->update_id + 1;
     
$cmd->setUpdate($up);
     
runBot($cmd);
    }
  }

}