PHP Classes

PHP Telegram Bot: Send and receive messages to Telegram users

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 62%Total: 1,363 All time: 2,851 This week: 67Up
Version License PHP version Categories
telegram-php 1.0.26GNU General Publi...5PHP 5, Web services, Chat
Description 

Author

This package can send and receive messages to Telegram users.

It can connect to Telegram as a given user and send messages to a given user.

It can also register callback functions when the user receives a message matching some incoming message text pattern.

Picture of David Girón
  Performance   Level  
Name: David Girón <contact>
Classes: 4 packages by
Country: Spain Spain
Age: 28
All time rank: 160738 in Spain Spain
Week rank: 287 Up8 in Spain Spain Up
Innovation award
Innovation award
Nominee: 2x

Documentation

Telegram-PHP

Another library to use Telegram bots with PHP.

  • Include the src/Autoloader.php file.
  • Create a Telegram\Bot object.
  • Create a Telegram\Receiver object using the $bot.
$bot = new Telegram\Bot("11111111:AAAAAAAAAAzzzzzzzzzzzzzzzzzzz", "MyUserBot", "The Name of Bot");
$tg  = new Telegram\Receiver($bot);

You can create as many Bots and Receivers or Senders as you want. Using Receiver includes a Sender.

Usage

Once the page is loaded (manually or via webhook), you can send or reply the requests.

To send a message to a user or group chat:

$tg->send
  ->chat("123456")
  ->text("Hello world!")
->send();

To reply a user command:

if($tg->text_command("start")){
  $tg->send
    ->text("Hi!")
  ->send();
}

To reply a user message:

if($tg->text_has("are you alive")){
  $tg->send
    ->text("Yes!")
  ->send();
}

NEW: To parse a string:

if($tg->text_regex("I'm {N:age}") and $tg->words() <= 4){
  $num = $tg->input->age;
  $str = "So old...";
  if($num < 18){ $str = "You're young!"; }
  $tg->send
    ->text($str)
  ->send();
}elseif($tg->text_regex("My name's {name}")){
  $tg->send
    ->text("Nice to meet you, " .$tg->input->name ."!")
  ->send();
}

Send an Inline Keyboard and parse it:

if($tg->callback == "but 1"){
  $tg->answer_if_callback(""); // Stop loading button.
  $tg->send
    ->message(TRUE)
    ->chat(TRUE)
    ->text("You pressed the first button!")
  ->edit("text");
}elseif($tg->callback == "but 2"){
  $tg->answer_if_callback("You pressed the second button!", TRUE);
  // Display an alert and stop loading button.
}

if($tg->text_has("matrix") and $tg->words() <= 5){
  $tg->send
    ->text("Red or blue. You choose.")
    ->inline_keyboard()
      ->row()
        ->button("Red",  "but 1")
        ->button("Blue", "but 2")
      ->end_row()
    ->show()
  ->send();
}

Examples


  Files folder image Files (26)  
File Role Description
Files folder imagesrc (9 files, 3 directories)
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (26)  /  src  
File Role Description
Files folder imageElements (12 files)
Files folder imageKeyboards (2 files)
Files folder imagePayments (1 file)
  Accessible without login Plain text file Autoloader.php Aux. Auxiliary script
  Plain text file Bot.php Class Class source
  Plain text file Chat.php Class Class source
  Accessible without login Plain text file Emojis.php Aux. Auxiliary script
  Plain text file Message.php Class Class source
  Plain text file Receiver.php Class Class source
  Plain text file Sender.php Class Class source
  Plain text file Sticker.php Class Class source
  Plain text file User.php Class Class source

  Files folder image Files (26)  /  src  /  Elements  
File Role Description
  Plain text file Audio.php Class Class source
  Plain text file Base.php Class Class source
  Plain text file ChatMember.php Class Class source
  Plain text file Document.php Class Class source
  Plain text file Location.php Class Class source
  Plain text file MessageEntity.php Class Class source
  Plain text file PhotoSize.php Class Class source
  Plain text file Sticker.php Class Class source
  Plain text file Venue.php Class Class source
  Plain text file Video.php Class Class source
  Plain text file VideoNote.php Class Class source
  Plain text file Voice.php Class Class source

  Files folder image Files (26)  /  src  /  Keyboards  
File Role Description
  Plain text file InlineKeyboard.php Class Class source
  Plain text file Keyboard.php Class Class source

  Files folder image Files (26)  /  src  /  Payments  
File Role Description
  Plain text file Stripe.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:1,363
This week:0
All time:2,851
This week:67Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:87%StarStarStarStarStar
Documentation:79%StarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:62%StarStarStarStar
Rank:948