TelegramController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. protected $telegramService;
  11. public function __construct(Request $request)
  12. {
  13. if ($request->input('access_token') !== md5(config('v2board.telegram_bot_token'))) {
  14. abort(401);
  15. }
  16. $this->telegramService = new TelegramService();
  17. }
  18. public function webhook(Request $request)
  19. {
  20. info($request->input());
  21. $this->formatMessage($request->input());
  22. $this->formatChatJoinRequest($request->input());
  23. $this->handle();
  24. }
  25. public function handle()
  26. {
  27. if (!$this->msg) return;
  28. $msg = $this->msg;
  29. $commandName = explode('@', $msg->command);
  30. // To reduce request, only commands contains @ will get the bot name
  31. if (count($commandName) == 2) {
  32. $botName = $this->getBotName();
  33. if ($commandName[1] === $botName){
  34. $msg->command = $commandName[0];
  35. }
  36. }
  37. try {
  38. foreach (glob(base_path('app//Plugins//Telegram//Commands') . '/*.php') as $file) {
  39. $command = basename($file, '.php');
  40. $class = '\\App\\Plugins\\Telegram\\Commands\\' . $command;
  41. if (!class_exists($class)) continue;
  42. $instance = new $class();
  43. if ($msg->message_type === 'message') {
  44. if (!isset($instance->command)) continue;
  45. if ($msg->command !== $instance->command) continue;
  46. $instance->handle($msg);
  47. return;
  48. }
  49. if ($msg->message_type === 'reply_message') {
  50. if (!isset($instance->regex)) continue;
  51. if (!preg_match($instance->regex, $msg->reply_text, $match)) continue;
  52. $instance->handle($msg, $match);
  53. return;
  54. }
  55. }
  56. } catch (\Exception $e) {
  57. $this->telegramService->sendMessage($msg->chat_id, $e->getMessage());
  58. }
  59. }
  60. public function getBotName()
  61. {
  62. $response = $this->telegramService->getMe();
  63. return $response->result->username;
  64. }
  65. private function formatMessage(array $data)
  66. {
  67. if (!isset($data['message'])) return;
  68. if (!isset($data['message']['text'])) return;
  69. $obj = new \StdClass();
  70. $text = explode(' ', $data['message']['text']);
  71. $obj->command = $text[0];
  72. $obj->args = array_slice($text, 1);
  73. $obj->chat_id = $data['message']['chat']['id'];
  74. $obj->message_id = $data['message']['message_id'];
  75. $obj->message_type = 'message';
  76. $obj->text = $data['message']['text'];
  77. $obj->is_private = $data['message']['chat']['type'] === 'private';
  78. if (isset($data['message']['reply_to_message']['text'])) {
  79. $obj->message_type = 'reply_message';
  80. $obj->reply_text = $data['message']['reply_to_message']['text'];
  81. }
  82. $this->msg = $obj;
  83. }
  84. private function formatChatJoinRequest(array $data)
  85. {
  86. if (!isset($data['chat_join_request'])) return;
  87. if (!isset($data['chat_join_request']['from']['id'])) return;
  88. if (!isset($data['chat_join_request']['chat']['id'])) return;
  89. $user = \App\Models\User::where('telegram_id', $data['chat_join_request']['from']['id'])
  90. ->first();
  91. if (!$user) {
  92. $this->telegramService->declineChatJoinRequest(
  93. $data['chat_join_request']['chat']['id'],
  94. $data['chat_join_request']['from']['id']
  95. );
  96. return;
  97. }
  98. $userService = new \App\Services\UserService();
  99. if (!$userService->isAvailable($user)) {
  100. $this->telegramService->declineChatJoinRequest(
  101. $data['chat_join_request']['chat']['id'],
  102. $data['chat_join_request']['from']['id']
  103. );
  104. return;
  105. }
  106. $userService = new \App\Services\UserService();
  107. $this->telegramService->approveChatJoinRequest(
  108. $data['chat_join_request']['chat']['id'],
  109. $data['chat_join_request']['from']['id']
  110. );
  111. }
  112. }