1. Go to this page and download the library: Download vzgcoders/telegramphp library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Telegram\Events\Event;
use Telegram\Parts\Message;
use Telegram\Telegram;
$telegram = new Telegram(['token' => getenv('TELEGRAM_TOKEN')]);
$telegram->on(Event::MESSAGE, function (Message $message) use ($telegram) {
if ($message->text === 'ping') {
$message->reply('pong');
}
});
$telegram->run();
$telegram = new Telegram([
'token' => getenv('TELEGRAM_TOKEN'),
'base_url' => 'http://localhost:8081',
// The same machine: the server's paths are this process's.
'local_files' => ['C:\telegram-bot-api'],
// Or in Docker: the server's path => the volume it is mounted from.
// 'local_files' => ['/var/lib/telegram-bot-api' => 'D:\telegram-bot-api'],
]);
use Telegram\CommandClient\TelegramCommandClient;
use Telegram\Parts\Message;
$bot = new TelegramCommandClient([
'token' => getenv('TELEGRAM_TOKEN'),
'description' => 'A helpful bot',
]);
$bot->registerCommand('ping', fn () => 'pong', ['description' => 'Checks the bot is alive']);
$bot->registerCommand('echo', fn (Message $message, array $args) => implode(' ', $args), [
'description' => 'Repeats what you say',
'aliases' => ['say'],
]);
$bot->run();
$telegram->chats->get($chatId);
$telegram->chats->fetch($chatId); // getChat, cached on the way back
$telegram->users->fetchMember($chatId, $userId);
use Telegram\Http\Exceptions\ForbiddenException;
use Telegram\Http\Exceptions\HttpException;
$telegram->sendMessage($chatId, 'hello')->then(null, function (HttpException $e) {
if ($e instanceof ForbiddenException) {
// Blocked by the user, or kicked from the chat.
}
$e->getErrorCode(); // 403
$e->getRetryAfter(); // seconds, on a 429
$e->getMigrateToChatId(); // the supergroup a migrated group became
});