Guest User

Untitled

a guest
Jan 16th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. <?php
  2. namespace AppHttpControllers;
  3. use TelegramBotLaravelFacadesTelegram;
  4.  
  5. class TelegramController extends Controller
  6. {
  7. public function index()
  8. {
  9. $telegram = new Api(config('telegram.bot_token'));
  10. dd($response = $telegram->setWebhook([
  11. 'url' => 'https://telebot.dev/<token>',
  12. 'certificate' => '/my.pem'
  13. ]));
  14. }
  15. }
  16.  
  17. <?php
  18.  
  19. namespace TelegramBotCommands;
  20.  
  21. /**
  22. * Class HelpCommand.
  23. */
  24. class HelpCommand extends Command
  25. {
  26. /**
  27. * @var string Command Name
  28. */
  29. protected $name = 'help';
  30.  
  31. /**
  32. * @var string Command Description
  33. */
  34. protected $description = 'Help command, Get a list of commands';
  35.  
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function handle($arguments)
  40. {
  41. $commands = $this->telegram->getCommands();
  42.  
  43. $text = '';
  44. foreach ($commands as $name => $handler) {
  45. $text .= sprintf('/%s - %s'.PHP_EOL, $name, $handler->getDescription());
  46. }
  47.  
  48. $this->replyWithMessage(compact('text'));
  49. }
  50. }
  51.  
  52. $updates = Telegram::getUpdates(); // array of Update
  53.  
  54. // Бот отвечает сообщением, которое получил добавляя префикс "echo: "
  55. public function webHookAction()
  56. {
  57. if (config('app.env') == 'local') {
  58. $updates = Telegram::getUpdates();
  59. } else {
  60. $updates = [Telegram::getWebhookUpdates()];
  61. }
  62.  
  63. /** @var Update[] $updates */
  64. foreach ($updates as $update) {
  65. $fromChatId = $update->getMessage()->getChat()->getId();
  66. $receivedMessage = $update->getMessage()->getText();
  67.  
  68. Telegram::sendMessage([
  69. 'chat_id' => $fromChatId,
  70. 'parse_mode' => 'HTML',
  71. 'text' => 'echo: <i>' . $receivedMessage . "</i>",
  72. ]);
  73. }
  74. }
  75.  
  76. Route::post('/' . config('telegram.bot_token'), function (Request $request) {
  77. $update = Telegram::commandsHandler(true);
  78. return 'ok';
  79. });
  80.  
  81. class StartCommand extends Command {
  82.  
  83. protected $name = "start";
  84.  
  85. /**
  86. * @var string Command Description
  87. */
  88. protected $description = "Команда для начала общения с ботом";
  89.  
  90. /**
  91. * @inheritdoc
  92. */
  93. public function handle($arguments)
  94. {
  95. $this->replyWithMessage(['text' => 'Привет я Бот, вот список команд которые вам доступны:']);
  96.  
  97. // Посылаем статус что печатаем
  98. $this->replyWithChatAction(['action' => Actions::TYPING]);
  99.  
  100. $commands = $this->getTelegram()->getCommands();
  101.  
  102. // Строим список команд
  103. $response = '';
  104. foreach ($commands as $name => $command) {
  105. $response .= sprintf('/%s - %s' . PHP_EOL, $name, $command->getDescription());
  106. }
  107.  
  108. //Посылаем ответ
  109. $this->replyWithMessage(['text' => $response]);
  110. }
  111. }
Add Comment
Please, Sign In to add comment