Telegram бот и регистрация URL для веб-хука

Для регистрации нового бота используется телеграмм-бот: @BotFather

Все команды данного бота:

/newbot — create a new bot
/mybots — edit your bots [beta]

Edit Bots
/setname — change a bot’s name
/setdescription — change bot description
/setabouttext — change bot about info
/setuserpic — change bot profile photo
/setcommands — change the list of commands
/deletebot — delete a bot

Bot Settings
/token — generate authorization token
/revoke — revoke bot access token
/setinline — toggle inline mode (https://core.telegram.org/bots/inline)
/setinlinegeo — toggle inline location requests (https://core.telegram.org/bots/inline#location-based-results)
/setinlinefeedback — change inline feedback (https://core.telegram.org/bots/inline#collecting-feedback) settings
/setjoingroups — can your bot be added to groups?
/setprivacy — toggle privacy mode (https://core.telegram.org/bots#privacy-mode) in groups

Games
/mygames — edit your games (https://core.telegram.org/bots/games) [beta]
/newgame — create a new game (https://core.telegram.org/bots/games)
/listgames — get a list of your games
/editgame — edit a game
/deletegame — delete an existing game

 

Описание API по телеграмм боту: https://tlgrm.ru/docs/bots/api

 

Для добавления адреса для веб-хуков:

https://api.telegram.org/bot<API-KEY>/setwebhook?url=<URL>

Пример:

https://api.telegram.org/bot1260943904:ktr3f49m6jrmrz68scbthw/setwebhook?url=https://site.net/bot_telegram/

 

Библиотека на PHP «PHP Telegram Bot Api»

https://github.com/TelegramBot/Api

Установка через Composer:

$ composer require telegram-bot/api

Send message:

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN');

$bot->sendMessage($chatId, $messageText);

Send document:

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN');

$document = new \CURLFile('document.txt');

$bot->sendDocument($chatId, $document);

Send message with reply keyboard:

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN');

$keyboard = new \TelegramBot\Api\Types\ReplyKeyboardMarkup(array(array("one", "two", "three")), true); // true for one-time keyboard

$bot->sendMessage($chatId, $messageText, null, false, null, $keyboard);

Send message with inline keyboard:

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN');

$keyboard = new \TelegramBot\Api\Types\Inline\InlineKeyboardMarkup(
            [
                [
                    ['text' => 'link', 'url' => 'https://core.telegram.org']
                ]
            ]
        );
        
$bot->sendMessage($chatId, $messageText, null, false, null, $keyboard);

Send media group:

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN');

$media = new \TelegramBot\Api\Types\InputMedia\ArrayOfInputMedia();
$media->addItem(new TelegramBot\Api\Types\InputMedia\InputMediaPhoto('https://avatars3.githubusercontent.com/u/9335727'));
$media->addItem(new TelegramBot\Api\Types\InputMedia\InputMediaPhoto('https://avatars3.githubusercontent.com/u/9335727'));
// Same for video
// $media->addItem(new TelegramBot\Api\Types\InputMedia\InputMediaVideo('http://clips.vorwaerts-gmbh.de/VfE_html5.mp4'));
$bot->sendMediaGroup($chatId, $media);

Client

require_once "vendor/autoload.php";

try {
    $bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN');
    // or initialize with botan.io tracker api key
    // $bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN', 'YOUR_BOTAN_TRACKER_API_KEY');
    

    //Handle /ping command
    $bot->command('ping', function ($message) use ($bot) {
        $bot->sendMessage($message->getChat()->getId(), 'pong!');
    });
    
    //Handle text messages
    $bot->on(function (\TelegramBot\Api\Types\Update $update) use ($bot) {
        $message = $update->getMessage();
        $id = $message->getChat()->getId();
        $bot->sendMessage($id, 'Your message: ' . $message->getText());
    }, function () {
        return true;
    });
    
    $bot->run();

} catch (\TelegramBot\Api\Exception $e) {
    $e->getMessage();
}

Скачать GitHub вариант от 29.01.2021