TelegramController.php 4.3 KB

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