|
@@ -10,25 +10,29 @@ class TelegramController extends Controller
|
|
{
|
|
{
|
|
protected $msg;
|
|
protected $msg;
|
|
protected $commands = [];
|
|
protected $commands = [];
|
|
|
|
+ protected $telegramService;
|
|
|
|
|
|
public function __construct(Request $request)
|
|
public function __construct(Request $request)
|
|
{
|
|
{
|
|
if ($request->input('access_token') !== md5(config('v2board.telegram_bot_token'))) {
|
|
if ($request->input('access_token') !== md5(config('v2board.telegram_bot_token'))) {
|
|
abort(401);
|
|
abort(401);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ $this->telegramService = new TelegramService();
|
|
}
|
|
}
|
|
|
|
|
|
public function webhook(Request $request)
|
|
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();
|
|
$this->handle();
|
|
}
|
|
}
|
|
|
|
|
|
public function handle()
|
|
public function handle()
|
|
{
|
|
{
|
|
|
|
+ if (!$this->msg) return;
|
|
$msg = $this->msg;
|
|
$msg = $this->msg;
|
|
-
|
|
|
|
$commandName = explode('@', $msg->command);
|
|
$commandName = explode('@', $msg->command);
|
|
|
|
|
|
// To reduce request, only commands contains @ will get the bot name
|
|
// To reduce request, only commands contains @ will get the bot name
|
|
@@ -59,34 +63,62 @@ class TelegramController extends Controller
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
- $telegramService = new TelegramService();
|
|
|
|
- $telegramService->sendMessage($msg->chat_id, $e->getMessage());
|
|
|
|
|
|
+ $this->telegramService->sendMessage($msg->chat_id, $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public function getBotName()
|
|
public function getBotName()
|
|
{
|
|
{
|
|
- $telegramService = new TelegramService();
|
|
|
|
- $response = $telegramService->getMe();
|
|
|
|
|
|
+ $response = $this->telegramService->getMe();
|
|
return $response->result->username;
|
|
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();
|
|
$obj = new \StdClass();
|
|
- if (!isset($data['message']['text'])) return false;
|
|
|
|
$text = explode(' ', $data['message']['text']);
|
|
$text = explode(' ', $data['message']['text']);
|
|
$obj->command = $text[0];
|
|
$obj->command = $text[0];
|
|
$obj->args = array_slice($text, 1);
|
|
$obj->args = array_slice($text, 1);
|
|
$obj->chat_id = $data['message']['chat']['id'];
|
|
$obj->chat_id = $data['message']['chat']['id'];
|
|
$obj->message_id = $data['message']['message_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->text = $data['message']['text'];
|
|
$obj->is_private = $data['message']['chat']['type'] === 'private';
|
|
$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'];
|
|
$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']
|
|
|
|
+ );
|
|
}
|
|
}
|
|
}
|
|
}
|