mirror of
				https://github.com/v2board/v2board.git
				synced 2025-10-31 01:11:46 +08:00 
			
		
		
		
	update: telegram group join request approve
This commit is contained in:
		| @@ -10,25 +10,29 @@ class TelegramController extends Controller | ||||
| { | ||||
|     protected $msg; | ||||
|     protected $commands = []; | ||||
|     protected $telegramService; | ||||
|  | ||||
|     public function __construct(Request $request) | ||||
|     { | ||||
|         if ($request->input('access_token') !== md5(config('v2board.telegram_bot_token'))) { | ||||
|             abort(401); | ||||
|         } | ||||
|  | ||||
|         $this->telegramService = new TelegramService(); | ||||
|     } | ||||
|  | ||||
|     public function webhook(Request $request) | ||||
|     { | ||||
|         $this->msg = $this->getMessage($request->input()); | ||||
|         if (!$this->msg) return; | ||||
|         info($request->input()); | ||||
|         $this->formatMessage($request->input()); | ||||
|         $this->formatChatJoinRequest($request->input()); | ||||
|         $this->handle(); | ||||
|     } | ||||
|  | ||||
|     public function handle() | ||||
|     { | ||||
|         if (!$this->msg) return; | ||||
|         $msg = $this->msg; | ||||
|  | ||||
|         $commandName = explode('@', $msg->command); | ||||
|  | ||||
|         // To reduce request, only commands contains @ will get the bot name | ||||
| @@ -59,34 +63,62 @@ class TelegramController extends Controller | ||||
|                 } | ||||
|             } | ||||
|         } catch (\Exception $e) { | ||||
|             $telegramService = new TelegramService(); | ||||
|             $telegramService->sendMessage($msg->chat_id, $e->getMessage()); | ||||
|             $this->telegramService->sendMessage($msg->chat_id, $e->getMessage()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public function getBotName() | ||||
|     { | ||||
|         $telegramService = new TelegramService(); | ||||
|         $response = $telegramService->getMe(); | ||||
|         $response = $this->telegramService->getMe(); | ||||
|         return $response->result->username; | ||||
|     } | ||||
|  | ||||
|     private function getMessage(array $data) | ||||
|     private function formatMessage(array $data) | ||||
|     { | ||||
|         if (!isset($data['message'])) return false; | ||||
|         if (!isset($data['message'])) return; | ||||
|         if (!isset($data['message']['text'])) return; | ||||
|         $obj = new \StdClass(); | ||||
|         if (!isset($data['message']['text'])) return false; | ||||
|         $text = explode(' ', $data['message']['text']); | ||||
|         $obj->command = $text[0]; | ||||
|         $obj->args = array_slice($text, 1); | ||||
|         $obj->chat_id = $data['message']['chat']['id']; | ||||
|         $obj->message_id = $data['message']['message_id']; | ||||
|         $obj->message_type = !isset($data['message']['reply_to_message']['text']) ? 'message' : 'reply_message'; | ||||
|         $obj->message_type = 'message'; | ||||
|         $obj->text = $data['message']['text']; | ||||
|         $obj->is_private = $data['message']['chat']['type'] === 'private'; | ||||
|         if ($obj->message_type === 'reply_message') { | ||||
|         if (isset($data['message']['reply_to_message']['text'])) { | ||||
|             $obj->message_type = 'reply_message'; | ||||
|             $obj->reply_text = $data['message']['reply_to_message']['text']; | ||||
|         } | ||||
|         return $obj; | ||||
|         $this->msg = $obj; | ||||
|     } | ||||
|  | ||||
|     private function formatChatJoinRequest(array $data) | ||||
|     { | ||||
|         if (!isset($data['chat_join_request'])) return; | ||||
|         if (!isset($data['chat_join_request']['from']['id'])) return; | ||||
|         if (!isset($data['chat_join_request']['chat']['id'])) return; | ||||
|         $user = \App\Models\User::where('telegram_id', $data['chat_join_request']['from']['id']) | ||||
|             ->first(); | ||||
|         if (!$user) { | ||||
|             $this->telegramService->declineChatJoinRequest( | ||||
|                 $data['chat_join_request']['chat']['id'], | ||||
|                 $data['chat_join_request']['from']['id'] | ||||
|             ); | ||||
|             return; | ||||
|         } | ||||
|         $userService = new \App\Services\UserService(); | ||||
|         if (!$userService->isAvailable($user)) { | ||||
|             $this->telegramService->declineChatJoinRequest( | ||||
|                 $data['chat_join_request']['chat']['id'], | ||||
|                 $data['chat_join_request']['from']['id'] | ||||
|             ); | ||||
|             return; | ||||
|         } | ||||
|         $userService = new \App\Services\UserService(); | ||||
|         $this->telegramService->approveChatJoinRequest( | ||||
|             $data['chat_join_request']['chat']['id'], | ||||
|             $data['chat_join_request']['from']['id'] | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user