TelegramController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Http\Controllers\Guest;
  3. use App\Services\TelegramService;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. class TelegramController extends Controller
  7. {
  8. protected $msg;
  9. protected $commands = [];
  10. public function __construct(Request $request)
  11. {
  12. if ($request->input('access_token') !== md5(config('v2board.telegram_bot_token'))) {
  13. abort(401);
  14. }
  15. }
  16. public function webhook(Request $request)
  17. {
  18. $this->msg = $this->getMessage($request->input());
  19. if (!$this->msg) return;
  20. $this->handle();
  21. }
  22. public function handle()
  23. {
  24. $msg = $this->msg;
  25. $commandName = explode('@', $msg->command);
  26. // To reduce request, only commands contains @ will get the bot name
  27. if (count($commandName) == 2) {
  28. $botName = $this->getBotName();
  29. if ($commandName[1] === $botName){
  30. $msg->command = $commandName[0];
  31. }
  32. }
  33. try {
  34. foreach (glob(base_path('app//Plugins//Telegram//Commands') . '/*.php') as $file) {
  35. $command = basename($file, '.php');
  36. $class = '\\App\\Plugins\\Telegram\\Commands\\' . $command;
  37. if (!class_exists($class)) continue;
  38. $instance = new $class();
  39. if ($msg->message_type === 'message') {
  40. if (!isset($instance->command)) continue;
  41. if ($msg->command !== $instance->command) continue;
  42. $instance->handle($msg);
  43. return;
  44. }
  45. if ($msg->message_type === 'reply_message') {
  46. if (!isset($instance->regex)) continue;
  47. if (!preg_match($instance->regex, $msg->reply_text, $match)) continue;
  48. $instance->handle($msg, $match);
  49. return;
  50. }
  51. }
  52. } catch (\Exception $e) {
  53. $telegramService = new TelegramService();
  54. $telegramService->sendMessage($msg->chat_id, $e->getMessage());
  55. }
  56. }
  57. public function getBotName()
  58. {
  59. $telegramService = new TelegramService();
  60. $response = $telegramService->getMe();
  61. return $response->result->username;
  62. }
  63. private function getMessage(array $data)
  64. {
  65. if (!isset($data['message'])) return false;
  66. $obj = new \StdClass();
  67. if (!isset($data['message']['text'])) return false;
  68. $text = explode(' ', $data['message']['text']);
  69. $obj->command = $text[0];
  70. $obj->args = array_slice($text, 1);
  71. $obj->chat_id = $data['message']['chat']['id'];
  72. $obj->message_id = $data['message']['message_id'];
  73. $obj->message_type = !isset($data['message']['reply_to_message']['text']) ? 'message' : 'reply_message';
  74. $obj->text = $data['message']['text'];
  75. $obj->is_private = $data['message']['chat']['type'] === 'private';
  76. if ($obj->message_type === 'reply_message') {
  77. $obj->reply_text = $data['message']['reply_to_message']['text'];
  78. }
  79. return $obj;
  80. }
  81. }