PHP code example of kyle2142 / phpbot

1. Go to this page and download the library: Download kyle2142/phpbot 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/ */

    

kyle2142 / phpbot example snippets



$bot = new kyle2142\PHPBot('12345678:ABCDEF123456890abcdef'); //replace with your token
print_r($bot->api->getMe()); //dump bot info
$me = 98765432; //replace with your ID
$bot->sendMessage($me, "Hello World!");





$content = file_get_contents('php://input');
$update = json_decode($content, true);
//do stuff with $update:
if(isset($update['message']['text']) and $update['message']['text'] === "Hello!"){
    $msg_id = $update['message']['message_id'];
    $chat_id = $update['message']['chat']['id'];
    $name = $update['message']['from']['first_name'];
    $bot->sendMessage($chat_id, "Hello $name!", ['reply_to_message_id'=>$msg_id]);
}

$params = array('chat_id'=>511048636, 'from_chat_id'=>'@durov', 'message_id'=>79);
$bot->api->forwardMessage($params); //direct api method

$bot->editMessage(/*chat id*/ 511048646, /*msg id*/ 21, "New text!");

$params = [511048646, 21, "New text!"];
$bot->editMessage(...$params);

php > var_dump($bot->editMessage(343859930, 172, "New text!"));

object(stdClass)#4 (6) {
  ["message_id"]=>
  int(172)
  ["from"]=>
  object(stdClass)#5 (4) {
    ["id"]=>
    int(511048636)
    ["is_bot"]=>
    bool(true)
    ["first_name"]=>
    string(15) "Kyle's test bot"
    ["username"]=>
    string(14) "kyle_s_testbot"
  }
  ["chat"]=>
  object(stdClass)#6 (4) {
    ["id"]=>
    int(343859930)
    ["first_name"]=>
    string(4) "Kyle"
    ["username"]=>
    string(6) "Kyle_S"
    ["type"]=>
    string(7) "private"
  }
  ["date"]=>
  int(1528881693)
  ["edit_date"]=>
  int(1528881742)
  ["text"]=>
  string(9) "New text!"
}

php > var_dump($bot->deleteMessage(343859930, 172));

bool(true)

$result = $bot->sendmessage('@mychannel', "New stuff at example.com!");

$params = ['chat_id'=>343859930, 'from_chat_id' => $result->chat->id, 'message_id' => $result->message_id];
$bot->api->forwardMessage($params); //put params as variable due to line length

php > var_dump($bot->deleteMessage(343859930, 172));

PHP Warning:  Uncaught TelegramException: 400 (Bad Request: message to delete not found)
Trace:
//trace omitted

try{
    $bot->deleteMessage(343859930, 172);
}catch(\kyle2142\TelegramExeption $exception){
    echo "Error code was {$exception->getCode()}\n
    Telegram says '{$exception->getMessage()}' ";
}

Error code was 400
Telegram says 'Bad Request: message to delete not found'

try{
    $bot->editMessage(343859930, 173, "beep");
}catch(\kyle2142\TelegramFloodWait $exception){
    echo "We need to wait {$exception->getRetryAfter()} seconds";
}