sagirba/telegram-lite

Telegram Bot Lite class

1.1 2019-09-08 07:38 UTC

This package is auto-updated.

Last update: 2023-12-08 17:18:23 UTC


README

Very litle simple class for webhook telegram bot https://core.telegram.org/api

Send text or photo on user demand

Works on PHP 5.6+ with cURL

Before use this class you must have SSL Certificate and set webhook from the link:

https://api.telegram.org:443/botYOUR_BOT_TOKEN/setwebhook?url=https://example.com/example.php

Response will be:

{"ok":true,"result":true,"description":"Webhook was set"}

Install

composer require sagirba/telegram-lite

Example

/**
 * Config
 */
$config = [
    'token' => 'YOUR_TOKEN_BOT',
    'url' => 'https://api.telegram.org/bot',
    'upload_directory' => 'upload_telegram/'
];

use Sagirba\Telegram\TelegramLite;

$request = file_get_contents('php://input');
$request = json_decode($request, TRUE);

if (!$request) {

    /**
     * Some Error output (request is not valid JSON)
     */

} elseif (!isset($request['update_id']) || !isset($request['message'])) {

    /**
     * Some Error output (request has not message)
     */

} else {

    /**
     * Create object
     */
    $bot = new TelegramLite($config);

    /**
     * ... and do something
     */

    if ($request['message']['text'] === '/start') {
        $chat_id = $request['message']['from']['id'];
        $message = 'Yahoo!';
        $photo_url = 'http://cs6.pikabu.ru/post_img/2014/08/07/8/1407413976_1098743416.jpg';
        $caption_url = 'Image From Internet';
        $photo_server = 'image.jpg';
        $caption_server = 'Image From Server';

        /**
         * Send simple text
         */
        $bot->sendMessage($chat_id, $message);    //Send simple text

        /**
         * Send photo from internet
         */
        $bot->sendPhoto($chat_id, $caption_url, $photo_url, true);

        /**
         * Send photo from our server
         */
        $bot->sendPhoto($chat_id, $caption_server, $photo_server);
    }

}