项目作者: tje3d

项目描述 :
PHP Telegram Bot API, Supports Laravel. You'll love it.
高级语言: PHP
项目地址: git://github.com/tje3d/Telegram.git
创建时间: 2017-03-10T20:17:47Z
项目社区:https://github.com/tje3d/Telegram

开源协议:MIT License

下载


Telegram Bot API

PHP Telegram Bot API, Supports Laravel.

Installation

composer require tje3d/telegram

Laravel Only

Service Provider

Tje3d\Telegram\Laravel\TelegramServiceProvider::class

Facade

'Bot' => Tje3d\Telegram\Laravel\Facades\Bot::class

Publish Assets

artisan vendor:publish --tag=telegram

Will make a config file named telegram (config/telegram.php)

Examples

✔️ Initialize a Bot

  1. $bot = new \Tje3d\Telegram\Bot($token);
  2. $info = $bot->getMe();
  3. print_r($info);

✔️ Set Web Hook

  1. $bot->setWebhook('https://sld.tld');

✔️ Get Updates

  1. $response = $bot->getUpdates();
  2. ...
  3. $response = $bot->getUpdates($offset=0, $limit=100, $timeout=10); // Long pull

✔️ Send text message (known as sendMessage)

  1. $bot->sendMethod(
  2. (new \Tje3d\Telegram\Methods\Text())
  3. ->text('test')
  4. ->chat_id($chatId)
  5. );

⭐️ Or pass configuration as array

  1. $bot->sendMethod(
  2. (new Tje3d\Telegram\Methods\Text(['text' => 'hi', 'chat_id' => $chatId]))
  3. );

✔️ Keyboard

⭐️ Reply Keyboard

  1. $bot->sendMethod(
  2. (new Tje3d\Telegram\Methods\Text)
  3. ->text('My Sample Text')
  4. ->chat_id($chatId)
  5. ->reply_markup(
  6. (new Tje3d\Telegram\Markups\ReplayKeyboardMarkup)
  7. ->row(function($handler){
  8. $handler->addButton(['text' => 'btn1']);
  9. $handler->addButton(['text' => 'my special button ⭐️']);
  10. })
  11. ->row(function($handler){
  12. $handler->addButton(['text' => 'WOW']);
  13. })
  14. ->row(function($handler){
  15. $handler->addButton(['text' => 'Hey this is third line!']);
  16. })
  17. ->row(function($handler){
  18. $handler->addButton(['text' => '1']);
  19. $handler->addButton(['text' => '2']);
  20. $handler->addButton(['text' => '3']);
  21. $handler->addButton(['text' => '4']);
  22. })
  23. )
  24. );

image

⭐️ Inline Keyboard

  1. $bot->sendMethod(
  2. (new Tje3d\Telegram\Methods\Text)
  3. ->text('My Sample Text')
  4. ->chat_id($testChatId)
  5. ->reply_markup(
  6. (new Tje3d\Telegram\Markups\InlineKeyboardMarkup)
  7. ->row(function($handler){
  8. $handler->addButton(['text' => 'btn1', 'url' => 'http://sld.tld']);
  9. $handler->addButton(['text' => 'my special button ⭐️', 'url' => 'http://sld.tld']);
  10. })
  11. ->row(function($handler){
  12. $handler->addButton(['text' => 'WOW', 'callback_data' => 'doSomethingSpecial']);
  13. })
  14. )
  15. );

image

✔️ Photo, Audio, Video, Document …

  1. $bot->sendMethod(
  2. (new \Tje3d\Telegram\Methods\Photo)
  3. ->chat_id($chatId)
  4. ->photo(realpath('pic.png'))
  5. );
  6. ...
  7. $bot->sendMethod(
  8. (new \Tje3d\Telegram\Methods\Video)
  9. ->chat_id($chatId)
  10. ->video(realpath('video.mp4'))
  11. ->duration(10) // optional
  12. ->width(320) // optional
  13. ->height(320) // optional
  14. );
  15. ...
  16. $bot->sendMethod(
  17. (new \Tje3d\Telegram\Methods\Audio)
  18. ->chat_id($chatId)
  19. ->audio(realpath('video.mp3'))
  20. ->duration(30) // optional
  21. ->performer('tje3d') // optional
  22. ->title('Great music') // optional
  23. );
  24. ...

✔️ ChatAction

  1. $bot->sendMethod(
  2. (new \Tje3d\Telegram\Methods\ChatAction)
  3. ->chat_id($testChatId)
  4. ->typing() // Could be: upload_photo, record_video, upload_video, record_audio, upload_audio, upload_document, find_location
  5. );

Exceptions

Throw’s Tje3d\Telegram\Exceptions\TelegramResponseException if sendMethod failed.

  1. try {
  2. $bot = new \Tje3d\Telegram\Bot($token);
  3. $response = $bot->sendMethod(
  4. (new \Tje3d\Telegram\Methods\Text())
  5. ->text($text)
  6. ->chat_id($chatId)
  7. );
  8. } catch (TelegramResponseException $e) {
  9. print_r($e->response());
  10. }

Contact me

You can contact me via Telegram or Email.