formapro / telegram-bot
PHP Telegram Bot
0.1.14
2019-04-15 11:32 UTC
Requires
- php: ^7.1.3
- formapro/values: ^0.5.1
- guzzlehttp/guzzle: ^6.3
README
如你所期望的Telegram机器人。
示例
SetWebhook
<?php use Formapro\TelegramBot\Bot; use Formapro\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 Formapro\TelegramBot\Bot; use function GuzzleHttp\Psr7\str; $bot = new Bot('telegramToken'); $response = $bot->getWebhookInfo(); echo str($response);
SendMessage
<?php use Formapro\TelegramBot\Bot; use Formapro\TelegramBot\Update; use Formapro\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
注意:如果你要发送包含多个图片的gif,请使用sendDocument
<?php use Formapro\TelegramBot\Bot; use Formapro\TelegramBot\Update; use Formapro\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
注意: 通过URL发送目前仅适用于gif、pdf和zip文件。(来自文档)
<?php use Formapro\TelegramBot\Bot; use Formapro\TelegramBot\Update; use Formapro\TelegramBot\SendDocument; use Formapro\TelegramBot\FileUrl; use Formapro\TelegramBot\FileId; use Formapro\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 Formapro\TelegramBot\Bot; use Formapro\TelegramBot\Update; use Formapro\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 Formapro\TelegramBot\Bot; use Formapro\TelegramBot\Update; use Formapro\TelegramBot\SendMessage; use Formapro\TelegramBot\KeyboardButton; use Formapro\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);
请求联系人
<?php use Formapro\TelegramBot\Bot; use Formapro\TelegramBot\Update; use Formapro\TelegramBot\SendMessage; use Formapro\TelegramBot\KeyboardButton; use Formapro\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 Formapro\TelegramBot\InlineKeyboardButton; use Formapro\TelegramBot\InlineKeyboardMarkup; use Formapro\TelegramBot\SendMessage; use Formapro\TelegramBot\Bot; use Formapro\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 Formapro\TelegramBot\InlineKeyboardButton; use Formapro\TelegramBot\InlineKeyboardMarkup; use Formapro\TelegramBot\SendMessage; use Formapro\TelegramBot\Bot; use Formapro\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 Formapro\TelegramBot\AnswerCallbackQuery; use Formapro\TelegramBot\Bot; use Formapro\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())); }
由Forma-Pro开发
Forma-Pro是一家全栈开发公司,其兴趣也扩展到开源开发。作为一支强大的专业团队,我们致力于通过开发电子商务、docker & 微服务架构领域的前沿解决方案来帮助社区,这些领域我们已经积累了多年的经验。我们的主要专长是基于Symfony框架的解决方案,但我们始终关注能够以最佳方式完成我们工作的技术。我们致力于创造革命性的解决方案,改变架构和可扩展性方面的开发方式。
如果您对我们开源开发有任何疑问,特别是关于这个产品或任何其他事项,请随时联系opensource@forma-pro.com
许可证
它是在MIT许可证下发布的。