PHP Classes

PHP Telegram Bot API Package: Build a bot that can interact with 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
Not yet rated by the usersTotal: 110 All time: 9,644 This week: 129Up
Version License PHP version Categories
telegram-bot-api 1.0.0MIT/X Consortium ...5PHP 5, Web services, Logging, Chat
Description 

Author

This package can build a bot that can interact with Telegram users.

It can send Web requests to the Web server of the Telegram API to perform several types of operations that developers can use to implement a Telegram bot.

Currently, it can:

- Send generic Telegram API requests

- Define text patterns that the bot will be able to detect to trigger specific response messages

- Define keyboard buttons that can appear to respond to specific telegram messages that the users can send

- Log chatbot messages

Innovation Award
PHP Programming Innovation award nominee
July 2022
Number 8
Telegram is a well-known chat application that provides an API to build powerful bot applications. Those bot applications can interact with users automatically.

Some Telegram features allow users to answer questions using buttons or typing text using only specific keyboard characters.

This package takes advantage of the Telegram features to implement more user-friendly chatbot applications written in PHP quickly.

Manuel Lemos
Picture of Mateo
  Performance   Level  
Name: Mateo <contact>
Classes: 23 packages by
Country: Peru Peru
Age: ???
All time rank: 20474 in Peru Peru
Week rank: 180 Up1 in Peru Peru Up
Innovation award
Innovation award
Nominee: 10x

Winner: 3x

Documentation

Telegram bot api

Install

composer require mateodioev/tgbot

git clone https://github.com/Mateodioev/tgbot

First step

require __DIR__ . 'path/to/vendor/autoload.php';

Api

Make request to telegram api

use Mateodioev\Bots\Telegram\Api;
$api = new Api($bot_token, $endpoint);
$api->request('methodName', $params=[]);

$enpoint Is optional parameter, can be telegram bot api url or custom bot api

Core

Add magic methods __call and __callStatic

use Mateodioev\Bots\Telegram\Core;
$core = new Core($bot_token);
// https://core.telegram.org/bots/api#available-methods
$core->sendMessage($params);

If you want to use static methods you need some env vars

$_ENV['BOT_TOKEN']; // Bot token
$_ENV['BOT_API_LINK']; // (optional) Telegram bot api link or custom server

Inline

Methods to create results to answer inline query

use Mateodioev\Bots\Telegram\{Inline, Methods};
$methods = new Methods($token);
$inline = new Inline;

$result = $methods->answerInlineQuery($inline_id, [
  // https://core.telegram.org/bots/api#inlinequeryresult
  $inline->Article([
    'title' => 'The world ends in 2030!!',
    'input_message_content' => $inline->InputMessageContent('This is fake news...'),
  ]),
  $inline->Article([
    'title' => 'How to create fake news?',
    'input_message_content' => $inline->InputMessageContent("Too easy ..."),
    'reply_markup' => (string) Buttons::create()->addCeil(['text' => 'learn more here', 'url' => 'https://fake.news'])
  ])
]);

Methods

Default methods

use Mateodioev\Bots\Telegram\Methods;
$methods = new Methods($token);
$methods->method_name($params);

Buttons

Create keyboards

use Mateodioev\Bots\Telegram\Buttons;
$button = Buttons::create()
->addCeil(['text' => 'Button 1', 'callback_data' => 'test'])
->addCeil(['text' => "I'm link", 'url' => 'https://t.me/'])
->AddLine()->addCeil(['text' => 'Button in new line', 'url' => 'https://t.me']);

echo $button; // Return JSON object string

TelegramLogger

Activate php log and send to telegram channel

use Mateodioev\Bots\Telegram\TelegramLogger as Logger;

Logger::Activate($dir); // Directory where you want a file with the php logs to be stored
Loger::Warning($message); // send to telegram channel

__callStatic needs a $_ENV[CHANNEL_LOG] with the value of a chat_id where logs are send


  Files folder image Files (8)  
File Role Description
Files folder imagesrc (6 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (8)  /  src  
File Role Description
  Plain text file Api.php Class Class source
  Plain text file Buttons.php Class Class source
  Plain text file Core.php Class Class source
  Plain text file Inline.php Class Class source
  Plain text file Methods.php Class Class source
  Plain text file TelegramLogger.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:110
This week:0
All time:9,644
This week:129Up