legolabs/telegram-bot
Composer 安装命令:
composer require legolabs/telegram-bot
包简介
PHP Telegram Bot
README 文档
README
Telegram bot as it should be.
Examples
SetWebhook
<?php use Legolabs\TelegramBot\Bot; use Legolabs\TelegramBot\SetWebhook; use function GuzzleHttp\Psr7\str; $bot = new Bot('telegramToken'); $setWebhook = new SetWebhook('https://your.app/telegram-updates-hook'); // uncomment if use use self-signed certificate // $setWebhook->setCertificate(file_get_contents('/path/to/self-signed-certifcate.pem')); $response = $bot->setWebhook($setWebhook); echo str($response);
GetWebhookInfo
<?php use Legolabs\TelegramBot\Bot; use function GuzzleHttp\Psr7\str; $bot = new Bot('telegramToken'); $response = $bot->getWebhookInfo(); echo str($response);
SendMessage
<?php use Legolabs\TelegramBot\Bot; use Legolabs\TelegramBot\Update; use Legolabs\TelegramBot\SendMessage; $requestBody = file_get_contents('php://input'); $data = json_decode($requestBody, true); $update = Update::create($data); $bot = new Bot('telegramToken'); $bot->sendMessage(new SendMessage( $update->getMessage()->getChat()->getId(), 'Hi there! What can I do?' ));
SendPhoto
Note: if you want to send gif with many pictures inside -
use sendDocument
<?php use Legolabs\TelegramBot\Bot; use Legolabs\TelegramBot\Update; use Legolabs\TelegramBot\SendMessage; $requestBody = file_get_contents('php://input'); $data = json_decode($requestBody, true); $update = Update::create($data); // You can pass URI of image or a path to file $picture = '/path/to/picture'; // OR https://some-server.com/some.jpg $sendPhoto = new SendPhoto( $update->getMessage()->getChat()->getId(), file_get_contents($picture) // or just $picture if it's url ); // also you can set `caption` to image $sendPhoto->setCaption('Some caption under the picture'); $bot = new Bot('telegramToken'); $bot->sendPhoto($sendPhoto);
SendDocument
Note: sending by URL will currently only work for gif, pdf and zip files. (from documentation)
<?php use Legolabs\TelegramBot\Bot; use Legolabs\TelegramBot\Update; use Legolabs\TelegramBot\SendDocument; use Legolabs\TelegramBot\FileUrl; use Legolabs\TelegramBot\FileId; use Legolabs\TelegramBot\InputFile; $requestBody = file_get_contents('php://input'); $data = json_decode($requestBody, true); $update = Update::create($data); // You can pass URI of image or a path to file //$document = new FileUrl('https://some-server.com/some.pdf'); // You can pass an ID of already stored file on Telegram side. //$document = new FileId('123'); // You can pass local file. $document = new InputFile('test.txt', 'Hi there!'); $sendDocument = SendDocument::withInputFile( $update->getMessage()->getChat()->getId(), $document ); // also you can set `caption` to image $sendDocument->setCaption('Some caption under the document'); $bot = new Bot('telegramToken'); $bot->sendDocument($sendDocument);
SendInvoice
<?php use Legolabs\TelegramBot\Bot; use Legolabs\TelegramBot\Update; use Legolabs\TelegramBot\SendInvoice; $requestBody = file_get_contents('php://input'); $data = json_decode($requestBody, true); $update = Update::create($data); $payload = []; // any params which you need to proccess in the transaction $providerToken = 'something:like:this'; // Token have to be taken from botFather $sendInvoice = new SendInvoice( $update->getMessage()->getChat()->getId(), 'Your title', 'Your description of invoice', json_encode($payload), $providerToken, '12314czasdq', // unique id 'UAH', [new LabeledPrice('PriceLabel_1', 3001)] // amount; here 30.01 UAH ); $bot = new Bot('telegramToken'); $bot->sendInvoice($sendInvoice);
ReplyKeyboardMarkup
<?php use Legolabs\TelegramBot\Bot; use Legolabs\TelegramBot\Update; use Legolabs\TelegramBot\SendMessage; use Legolabs\TelegramBot\KeyboardButton; use Legolabs\TelegramBot\ReplyKeyboardMarkup; $requestBody = file_get_contents('php://input'); $data = json_decode($requestBody, true); $update = Update::create($data); $fooButton = new KeyboardButton('foo'); $barButton = new KeyboardButton('bar'); $bazButton = new KeyboardButton('bar'); $keyboard = new ReplyKeyboardMarkup([[$fooButton], [$barButton, $bazButton]]); $sendMessage = new SendMessage($update->getMessage()->getChat()->getId(), 'Choose an option.'); $sendMessage->setReplyMarkup($keyboard); $bot = new Bot('telegramToken'); $bot->sendMessage($sendMessage);
Request Contacts:
<?php use Legolabs\TelegramBot\Bot; use Legolabs\TelegramBot\Update; use Legolabs\TelegramBot\SendMessage; use Legolabs\TelegramBot\KeyboardButton; use Legolabs\TelegramBot\ReplyKeyboardMarkup; $requestBody = file_get_contents('php://input'); $data = json_decode($requestBody, true); $update = Update::create($data); $button = new KeyboardButton('Share my contacts'); $button->setRequestContact(true); $keyboard = new ReplyKeyboardMarkup([[$button]]); $keyboard->setOneTimeKeyboard(true); $sendMessage = new SendMessage($update->getMessage()->getChat()->getId(), 'Please, share your contact info with us.'); $sendMessage->setReplyMarkup($keyboard); $bot = new Bot('telegramToken'); $bot->sendMessage($sendMessage);
InlineKeyboardButton
Url:
<?php use Legolabs\TelegramBot\InlineKeyboardButton; use Legolabs\TelegramBot\InlineKeyboardMarkup; use Legolabs\TelegramBot\SendMessage; use Legolabs\TelegramBot\Bot; use Legolabs\TelegramBot\Update; $requestBody = file_get_contents('php://input'); $data = json_decode($requestBody, true); $update = Update::create($data); $button = InlineKeyboardButton::withUrl('inline button', 'https://your.app/link'); $keyboard = new InlineKeyboardMarkup([[$button]]); $sendMessage = new SendMessage($update->getMessage()->getChat()->getId(), 'Click on inline button.'); $sendMessage->setReplyMarkup($keyboard); $bot = new Bot('telegramToken'); $bot->sendMessage($sendMessage);
CallbackQuery:
<?php use Legolabs\TelegramBot\InlineKeyboardButton; use Legolabs\TelegramBot\InlineKeyboardMarkup; use Legolabs\TelegramBot\SendMessage; use Legolabs\TelegramBot\Bot; use Legolabs\TelegramBot\Update; $requestBody = file_get_contents('php://input'); $data = json_decode($requestBody, true); $update = Update::create($data); $button = InlineKeyboardButton::withCallbackData('inline button', 'some_data'); $keyboard = new InlineKeyboardMarkup([[$button]]); $sendMessage = new SendMessage($update->getMessage()->getChat()->getId(), 'Click on inline button.'); $sendMessage->setReplyMarkup($keyboard); $bot = new Bot('telegramToken'); $bot->sendMessage($sendMessage);
AnswerCallbackQuery
<?php use Legolabs\TelegramBot\AnswerCallbackQuery; use Legolabs\TelegramBot\Bot; use Legolabs\TelegramBot\Update; $requestBody = file_get_contents('php://input'); $data = json_decode($requestBody, true); $update = Update::create($data); if ($callbackQuery = $update->getCallbackQuery()) { $bot = new Bot('telegramToken'); $bot->answerCallbackQuery(new AnswerCallbackQuery($callbackQuery->getId())); }
Developed by Forma-Pro
Forma-Pro is a full stack development company which interests also spread to open source development. Being a team of strong professionals we have an aim an ability to help community by developing cutting edge solutions in the areas of e-commerce, docker & microservice oriented architecture where we have accumulated a huge many-years experience. Our main specialization is Symfony framework based solution, but we are always looking to the technologies that allow us to do our job the best way. We are committed to creating solutions that revolutionize the way how things are developed in aspects of architecture & scalability.
If you have any questions and inquires about our open source development, this product particularly or any other matter feel free to contact at opensource@forma-pro.com
License
It is released under the MIT License.
legolabs/telegram-bot 适用场景与选型建议
legolabs/telegram-bot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.14k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 12 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「client」 「bot」 「telegram」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 legolabs/telegram-bot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 legolabs/telegram-bot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 legolabs/telegram-bot 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dialogflow php sdk
Laravel 5.6 Monolog custom telegram channel
PHP Wrapper for Telegram Bot API
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
统计信息
- 总下载量: 2.14k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-27