2017 © Pedro Peláez
 

project telegrambot-php

Framework for Telegram Bot API

image

lukasss93/telegrambot-php

Framework for Telegram Bot API

  • Saturday, July 28, 2018
  • by Lukasss93
  • Repository
  • 1 Watchers
  • 3 Stars
  • 95 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 149 Forks
  • 0 Open issues
  • 30 Versions
  • 22 % Grown

The README.md

⚠️ This package is abandoned and no longer maintained.
Please use the nutgram/nutgram package instead.


, (*1)


TelegramBot-PHP

API PHP CURL, (*2)

Latest Stable Version Total Downloads License, (*3)

A very simple PHP Telegram Bot API for sending messages., (*4)

Requirements

  • PHP ≥ 7.2
  • Curl for PHP must be enabled.
  • Telegram API Key, you can get one simply with @BotFather with simple commands right after creating your bot.

For the WebHook: * An SSL certificate (Telegram API requires this). You can use Cloudflare's Free Flexible SSL which crypts the web traffic from end user to their proxies if you're using CloudFlare DNS.
Since the August 29 update you can use a self-signed ssl certificate., (*5)

For the GetUpdates: * Some way to execute the script in order to serve messages (for example cronjob), (*6)

Installation

You can install this library with composer:, (*7)

composer require lukasss93/telegrambot-php, (*8)

Configuration (WebHook)

Navigate to https://api.telegram.org/bot(TOKEN)/setWebhook?url=https://yoursite.com/your_update.php Or use the Telegram class setWebhook method., (*9)

Informations

This simple framework is object-based, all methods return a Telegram Object contained in TelegramBot/Types namespace., (*10)

Examples

use TelegramBot\TelegramBot;

$bot = new TelegramBot($token);
$update=$bot->getWebhookUpdate();
$bot->sendMessage([
    'chat_id' => $update->message->chat->id,
    'text' => 'Hello world!'
]);

If you want to get some specific parameter from the Telegram webhook, simply call parameter name in the object:, (*11)

use TelegramBot\TelegramBot;

$bot = new TelegramBot($token);
$update=$bot->getWebhookUpdate();
$text=$update->message->text;

To upload a Photo or some other files, you need to load it with CurlFile:, (*12)

// Load a local file to upload. If is already on Telegram's Servers just pass the resource id
$img = curl_file_create('test.png','image/png');
$bot->sendPhoto([
    'chat_id' => $chat_id, 
    'photo' => $img
]);

To download a file on the Telegram's servers, (*13)

$file = $bot->getFile($file_id);
$bot->downloadFile($file->file_path, "./my_downloaded_file_on_local_server.png");

If you want to use getUpdates instead of the WebHook you need a for cycle., (*14)

$updates=$bot->getUpdated();
for ($i = 0; $i < count($updates); $i++) {
    //single update
    $update=$updates[$i];
    if($update->message->getCommand()=='/start'){
        //working!
    }
}

Message Object Methods

Message object mainly provide 2 methods: * getCommand(), (*15)

```php
//$update->message->text => '/test@ExampleBot my args'
$command=$update->message->getCommand();
//$command => '/text'
```

* getArgs(bool $asString=false), (*16)

```php
//$update->message->text => '/test@ExampleBot my args'
$args_array=$update->message->getArgs();
//$args_array[0] => 'my'
//$args_array[1] => 'args'
$args_string=$update->message->getArgs(true);
//$args_string => 'my args'
```

Build keyboard parameters

Telegram's bots can have two different kind of keyboards: Inline and Reply. The InlineKeyboard is linked to a particular message, while the ReplyKeyboard is linked to the whole chat. They are both an array of array of buttons, which rapresent the rows and columns. For instance you can arrange a ReplyKeyboard using this code:, (*17)

$buttons = [ 
    //First row
    [
        $bot->buildKeyboardButton("Button 1"),
        $bot->buildKeyboardButton("Button 2")
    ], 
    //Second row 
    [
        $bot->buildKeyboardButton("Button 3"),
        $bot->buildKeyboardButton("Button 4"),
        $bot->buildKeyboardButton("Button 5")], 
    //Third row
    [
        $bot->buildKeyboardButton("Button 6")]
    ];

$bot->sendMessage([
    'chat_id' => $chat_id, 
    'reply_markup' => $bot->buildKeyBoard($buttons), 
    'text' => 'This is a Keyboard Test'
]);

When a user click on the button, the button text is send back to the bot. For an InlineKeyboard it's pretty much the same (but you need to provide a valid URL or a Callback data), (*18)

$buttons = [ 
    //First row
    [
        $bot->buildInlineKeyBoardButton("Button 1", $url="http://link1.com"), 
        $bot->buildInlineKeyBoardButton("Button 2", $url="http://link2.com")
    ], 
    //Second row 
    [
        $bot->buildInlineKeyBoardButton("Button 3", $url="http://link3.com"),
        $bot->buildInlineKeyBoardButton("Button 4", $url="http://link4.com"),
        $bot->buildInlineKeyBoardButton("Button 5", $url="http://link5.com")
    ], 
    //Third row
    [
        $bot->buildInlineKeyBoardButton("Button 6", $url="http://link6.com")
    ]
];

$bot->sendMessage([
    'chat_id' => $chat_id, 
    'reply_markup' => $bot->buildInlineKeyBoard($buttons), 
    'text' => 'This is a Keyboard Test'
]);

This is the list of all the helper functions to make keyboards easily:, (*19)

$bot->buildKeyBoard(array $options, $onetime=true, $resize=true, $selective=true);

Send a custom keyboard. $option is an array of array KeyboardButton.
Check ReplyKeyBoardMarkUp for more info., (*20)

$bot->buildInlineKeyBoard(array $inline_keyboard);

Send a custom keyboard. $inline_keyboard is an array of array InlineKeyboardButton.
Check InlineKeyboardMarkup for more info., (*21)

$bot->buildInlineKeyBoardButton($text, $url, $callback_data, $switch_inline_query);

Create an InlineKeyboardButton.
Check InlineKeyBoardButton for more info., (*22)

$bot->buildKeyBoardButton($text, $url, $request_contact, $request_location);

Create a KeyboardButton.
Check KeyBoardButton for more info., (*23)

$bot->buildKeyBoardHide($selective=true);

Hide a custom keyboard.
Check ReplyKeyBoarHide for more info., (*24)

$bot->buildForceReply($selective=true);

Show a Reply interface to the user.
Check ForceReply for more info., (*25)

Contact me

You can contact me via Telegram but if you have an issue please open one., (*26)

To-Do list

  • Optional predictive parameters in methods
  • Optimize keyboards
  • Chat conversations
  • Commands listener with callback + events

Changelog

All notable changes to this project will be documented here., (*27)

The Versions

28/07 2018

dev-master

9999999-dev

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

28/07 2018

1.6.9

1.6.9.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

10/04 2018

1.6.8

1.6.8.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

14/02 2018

1.6.7

1.6.7.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

14/02 2018

1.6.6

1.6.6.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

20/11 2017

1.6.5

1.6.5.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

11/10 2017

1.6.4

1.6.4.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

21/09 2017

1.6.3

1.6.3.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

19/09 2017

dev-dev

dev-dev

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

17/09 2017

1.6.2

1.6.2.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

17/09 2017

1.6.1

1.6.1.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

23/08 2017

1.6.0

1.6.0.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

20/08 2017

1.5.2

1.5.2.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

19/08 2017

1.5.1

1.5.1.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

14/08 2017

1.5.0

1.5.0.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

13/08 2017

1.4.4

1.4.4.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

12/08 2017

1.4.3

1.4.3.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

12/08 2017

1.4.2

1.4.2.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

09/08 2017

1.4.1

1.4.1.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

21/07 2017

1.4.0

1.4.0.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

30/06 2017

1.3.0

1.3.0.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

12/06 2017

1.2.3

1.2.3.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

05/06 2017

1.2.2

1.2.2.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

04/06 2017

1.2.1

1.2.1.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

04/06 2017

1.2.0

1.2.0.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

29/05 2017

1.1.1

1.1.1.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

20/05 2017

1.1.0

1.1.0.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

16/05 2017

1.0.2

1.0.2.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

24/01 2017

1.0.1

1.0.1.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires

 

24/01 2017

1.0.0

1.0.0.0

Framework for Telegram Bot API

  Sources   Download

MIT

The Requires