diff --git a/app/Command/XCat.php b/app/Command/XCat.php index 814c3434..b616fee1 100644 --- a/app/Command/XCat.php +++ b/app/Command/XCat.php @@ -272,22 +272,24 @@ class XCat public function setTelegram() { - // $bot = new BotApi(Config::get('telegram_token')); - // $ch = curl_init(); - // curl_setopt($ch, CURLOPT_URL, sprintf('https://api.telegram.org/bot%s/deleteWebhook', Config::get('telegram_token'))); - // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); - // $deleteWebhookReturn = json_decode(curl_exec($ch)); - // curl_close($ch); - // if ($deleteWebhookReturn->ok && $deleteWebhookReturn->result && $bot->setWebhook(Config::get('baseUrl') . '/telegram_callback?token=' . Config::get('telegram_request_token')) == 1) { - // echo ('设置成功!' . PHP_EOL); - // } - - $WebhookUrl = (Config::get('baseUrl') . '/TelegramCallback?token=' . Config::get('telegram_request_token')); - $telegram = new \Telegram\Bot\Api(Config::get('telegram_token')); - $telegram->removeWebhook(); - if ($telegram->setWebhook(['url' => $WebhookUrl])) { - echo('Bot @' . $telegram->getMe()->getUsername() . ' 设置成功!'); + if (Config::get('use_new_telegram_bot') === true) { + $WebhookUrl = (Config::get('baseUrl') . '/TelegramCallback?token=' . Config::get('telegram_request_token')); + $telegram = new \Telegram\Bot\Api(Config::get('telegram_token')); + $telegram->removeWebhook(); + if ($telegram->setWebhook(['url' => $WebhookUrl])) { + echo('New Bot @' . $telegram->getMe()->getUsername() . ' 设置成功!'); + } + } else { + $bot = new BotApi(Config::get('telegram_token')); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, sprintf('https://api.telegram.org/bot%s/deleteWebhook', Config::get('telegram_token'))); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); + $deleteWebhookReturn = json_decode(curl_exec($ch)); + curl_close($ch); + if ($deleteWebhookReturn->ok && $deleteWebhookReturn->result && $bot->setWebhook(Config::get('baseUrl') . '/telegram_callback?token=' . Config::get('telegram_request_token')) == 1) { + echo ('Old Bot 设置成功!' . PHP_EOL); + } } } diff --git a/app/Utils/Telegram/Callback.php b/app/Utils/Telegram/Callback.php deleted file mode 100644 index 246f73ee..00000000 --- a/app/Utils/Telegram/Callback.php +++ /dev/null @@ -1,785 +0,0 @@ - $Callback->getFrom()->getId(), - 'name' => $Callback->getFrom()->getFirstName() . ' ' . $Callback->getFrom()->getLastName(), - 'username' => $Callback->getFrom()->getUsername(), - ]; - - // 触发源消息发送时间 - $MessageSendTime = $Callback->getMessage()->getDate(); - - // 消息是否可编辑 - $AllowEditMessage = (time() < $MessageSendTime + 172800 - ? true - : false); - - $Data = [ - 'CallbackData' => $Callback->getData(), // 回调数据 - 'ChatID' => $Callback->getMessage()->getChat()->getId(), // 消息会话 ID - 'MessageID' => $Callback->getMessage()->getMessageId(), // 触发源信息 ID - 'AllowEditMessage' => $AllowEditMessage, // 消息是否可编辑 - ]; - - if ($Data['ChatID'] < 0) { - // 群组 - if (Config::get('telegram_group_quiet') === true) { - // 群组中不回应 - return; - } - } - - switch (true) { - case (strpos($Data['CallbackData'], 'user.') === 0): - // 用户相关 - self::UserHandler($user, $bot, $Callback, $Data, $SendUser); - break; - default: - // 回调数据处理 - self::CallbackDataHandler($user, $bot, $Callback, $Data, $SendUser); - break; - } - } - - /** - * - * 回调数据处理 - * - */ - public static function CallbackDataHandler($user, $bot, $Callback, $Data, $SendUser) - { - $CallbackDataExplode = explode('|', $Data['CallbackData']); - switch ($CallbackDataExplode[0]) { - case 'general.pricing': - // 产品介绍 - $sendMessage = [ - 'text' => Config::get('telegram_general_pricing'), - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => [ - Reply::getInlinekeyboard() - ] - ] - ), - ]; - break; - case 'general.terms': - // 服务条款 - $sendMessage = [ - 'text' => Config::get('telegram_general_terms'), - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => [ - Reply::getInlinekeyboard() - ] - ] - ), - ]; - break; - default: - // 主菜单 - $temp = Reply::getInlinekeyboard($user, 'index'); - $sendMessage = [ - 'text' => $temp['text'], - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $temp['keyboard'] - ] - ), - ]; - break; - } - $sendMessage = array_merge( - $sendMessage, - [ - 'chat_id' => $Data['ChatID'], - 'message_id' => $Data['MessageID'], - 'parse_mode' => 'HTML', - ] - ); - if ($Data['AllowEditMessage']) { - // 消息可编辑 - Process::SendPost('editMessageText', $sendMessage); - return; - } - $bot->sendMessage($sendMessage); - } - - /** - * - * 用户相关回调数据处理 - * - */ - public static function UserHandler($user, $bot, $Callback, $Data, $SendUser) - { - if ($user == null) { - if ($Data['ChatID'] < 0) { - return Process::SendPost( - 'answerCallbackQuery', - [ - 'callback_query_id' => $Callback->getId(), - 'text' => '您好,您尚未绑定账户,无法签到.', - 'show_alert' => true, - ] - ); - } else { - return self::CallbackDataHandler($user, $bot, $Callback, $Data, $SendUser); - } - } - - $CallbackDataExplode = explode('|', $Data['CallbackData']); - $Operate = explode('.', $CallbackDataExplode[0]); - $op_1 = $Operate[1]; - switch ($op_1) { - case 'edit': - // 资料编辑 - $op_2 = $Operate[2]; - switch ($op_2) { - case 'update_link': - // 重置订阅链接 - $temp = Reply::getInlinekeyboard($user, 'user.subscribe'); - $user->clean_link(); - $sendMessage = [ - 'text' => '订阅链接重置成功,请在下方重新更新订阅.', - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $temp['keyboard'] - ] - ), - ]; - break; - case 'update_passwd': - // 重置链接密码 - $user->passwd = Tools::genRandomChar(8); - if ($user->save()) { - $temp = Reply::getInlinekeyboard($user, 'user.subscribe'); - $sendMessage = [ - 'text' => '连接密码更新成功,请在下方重新更新订阅.' . PHP_EOL . PHP_EOL . '新的连接密码为:' . $user->passwd, - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $temp['keyboard'] - ] - ), - ]; - } else { - $temp = Reply::getInlinekeyboard(); - $sendMessage = [ - 'text' => '出现错误,连接密码更新失败,请联系管理员.', - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $temp['keyboard'] - ] - ), - ]; - } - break; - case 'encrypt': - // 加密方式更改 - $keyboard = [ - Reply::getInlinekeyboard() - ]; - if (Config::get('protocol_specify') === true) { - if (isset($CallbackDataExplode[1])) { - if (in_array($CallbackDataExplode[1], Config::getSupportParam('method')) && Config::get('protocol_specify') === true) { - $temp = $user->setMethod($CallbackDataExplode[1]); - if ($temp['ok'] === true) { - $text = '您当前的加密方式为:' . $user->method . PHP_EOL . PHP_EOL . $temp['msg']; - } else { - $text = '发生错误,请重新选择.' . PHP_EOL . PHP_EOL . $temp['msg']; - } - } else { - $text = '发生错误,请重新选择.'; - } - } else { - $Encrypts = []; - foreach (Config::getSupportParam('method') as $value) { - $Encrypts[] = [ - 'text' => $value, - 'callback_data' => 'user.edit.encrypt|' . $value - ]; - } - $Encrypts = array_chunk($Encrypts, 2); - $keyboard = []; - foreach ($Encrypts as $Encrypt) { - $keyboard[] = $Encrypt; - } - $keyboard[] = Reply::getInlinekeyboard(); - $text = '您当前的加密方式为:' . $user->method; - } - } else { - $text = '当前不允许私自更改.'; - } - $sendMessage = [ - 'text' => $text, - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $keyboard - ] - ), - ]; - break; - case 'protocol': - // 协议更改 - $keyboard = [ - Reply::getInlinekeyboard() - ]; - if (Config::get('protocol_specify') === true) { - if (isset($CallbackDataExplode[1])) { - if (in_array($CallbackDataExplode[1], Config::getSupportParam('protocol')) && Config::get('protocol_specify') === true) { - $temp = $user->setProtocol($CallbackDataExplode[1]); - if ($temp['ok'] === true) { - $text = '您当前的协议为:' . $user->protocol . PHP_EOL . PHP_EOL . $temp['msg']; - } else { - $text = '发生错误,请重新选择.' . PHP_EOL . PHP_EOL . $temp['msg']; - } - } else { - $text = '发生错误,请重新选择.'; - } - } else { - $Protocols = []; - foreach (Config::getSupportParam('protocol') as $value) { - $Protocols[] = [ - 'text' => $value, - 'callback_data' => 'user.edit.protocol|' . $value - ]; - } - $Protocols = array_chunk($Protocols, 1); - $keyboard = []; - foreach ($Protocols as $Protocol) { - $keyboard[] = $Protocol; - } - $keyboard[] = Reply::getInlinekeyboard(); - $text = '您当前的协议为:' . $user->protocol; - } - } else { - $text = '当前不允许私自更改.'; - } - $sendMessage = [ - 'text' => $text, - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $keyboard - ] - ), - ]; - break; - case 'obfs': - // 混淆更改 - $keyboard = [ - Reply::getInlinekeyboard() - ]; - if (Config::get('protocol_specify') === true) { - if (isset($CallbackDataExplode[1])) { - if (in_array($CallbackDataExplode[1], Config::getSupportParam('obfs')) && Config::get('protocol_specify') === true) { - $temp = $user->setObfs($CallbackDataExplode[1]); - if ($temp['ok'] === true) { - $text = '您当前的协议为:' . $user->obfs . PHP_EOL . PHP_EOL . $temp['msg']; - } else { - $text = '发生错误,请重新选择.' . PHP_EOL . PHP_EOL . $temp['msg']; - } - } else { - $text = '发生错误,请重新选择.'; - } - } else { - $Obfss = []; - foreach (Config::getSupportParam('obfs') as $value) { - $Obfss[] = [ - 'text' => $value, - 'callback_data' => 'user.edit.obfs|' . $value - ]; - } - $Obfss = array_chunk($Obfss, 1); - $keyboard = []; - foreach ($Obfss as $Obfs) { - $keyboard[] = $Obfs; - } - $keyboard[] = Reply::getInlinekeyboard(); - $text = '您当前的协议为:' . $user->obfs; - } - } else { - $text = '当前不允许私自更改.'; - } - $sendMessage = [ - 'text' => $text, - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $keyboard - ] - ), - ]; - break; - case 'sendemail': - // 每日邮件设置更改 - $keyboard = [ - [ - [ - 'text' => '更改开启/关闭', - 'callback_data' => 'user.edit.sendemail.update' - ] - ], - Reply::getInlinekeyboard() - ]; - $op_3 = $Operate[3]; - switch ($op_3) { - case 'update': - $user->sendDailyMail = ($user->sendDailyMail == 0 ? 1 : 0); - if ($user->save()) { - $text = '设置更改成功,每日邮件接收当前设置为:'; - $text .= ''; - $text .= ($user->sendDailyMail == 0 ? '不发送' : '发送'); - $text .= ''; - } else { - $text = '发生错误.'; - } - break; - default: - $text = '每日邮件接收当前设置为:'; - $text .= ''; - $text .= ($user->sendDailyMail == 0 ? '不发送' : '发送'); - $text .= ''; - break; - } - $sendMessage = [ - 'text' => $text, - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $keyboard - ] - ), - ]; - break; - case 'unbind': - // Telegram 账户解绑 - $Data['AllowEditMessage'] = false; - $sendMessage = [ - 'text' => '发送 /unbind 账户邮箱 进行解绑.', - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => null - ]; - break; - case 'unban': - // 群组解封 - Process::SendPost( - 'unbanChatMember', - [ - 'chat_id' => Config::get('telegram_chatid'), - 'user_id' => $SendUser['id'], - ] - ); - return Process::SendPost( - 'answerCallbackQuery', - [ - 'callback_query_id' => $Callback->getId(), - 'text' => '已提交解封,如您仍无法加入群组,请联系管理员.', - 'show_alert' => true, - ] - ); - break; - default: - $temp = Reply::getInlinekeyboard($user, 'user.edit'); - $text = '您可在此编辑您的资料或连接信息:' . PHP_EOL . PHP_EOL; - $text .= '端口:' . $user->port . PHP_EOL; - $text .= '密码:' . $user->passwd . PHP_EOL; - $text .= '加密:' . $user->method . PHP_EOL; - $text .= '协议:' . $user->protocol . PHP_EOL; - $text .= '混淆:' . $user->obfs; - $sendMessage = [ - 'text' => $text, - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $temp['keyboard'] - ] - ), - ]; - break; - } - break; - case 'subscribe': - // 订阅中心 - if (isset($CallbackDataExplode[1])) { - $temp = []; - $temp['keyboard'] = [ - Reply::getInlinekeyboard() - ]; - $UserApiUrl = LinkController::getSubinfo($user, 0)['link']; - switch ($CallbackDataExplode[1]) { - case '?clash=1': - $temp['text'] = '您的 Clash 配置文件.' . PHP_EOL . '同时,您也可使用该订阅链接:' . $UserApiUrl . $CallbackDataExplode[1]; - $token = LinkController::GenerateSSRSubCode($user->id, 0); - $filename = 'Clash_' . $token . '_' . time() . '.yaml'; - $filepath = BASE_PATH . '/storage/SendTelegram/' . $filename; - $fh = fopen($filepath, 'w+'); - $string = LinkController::getClash($user, 1, [], [], false, 0); - fwrite($fh, $string); - fclose($fh); - $bot->sendDocument( - [ - 'chat_id' => $Data['ChatID'], - 'document' => $filepath, - 'caption' => $temp['text'], - ] - ); - unlink($filepath); - break; - case '?clash=2': - $temp['text'] = '您的 ClashR 配置文件.' . PHP_EOL . '同时,您也可使用该订阅链接:' . $UserApiUrl . $CallbackDataExplode[1]; - $token = LinkController::GenerateSSRSubCode($user->id, 0); - $filename = 'ClashR_' . $token . '_' . time() . '.yaml'; - $filepath = BASE_PATH . '/storage/SendTelegram/' . $filename; - $fh = fopen($filepath, 'w+'); - $string = LinkController::getClash($user, 2, [], [], false, 0); - fwrite($fh, $string); - fclose($fh); - $bot->sendDocument( - [ - 'chat_id' => $Data['ChatID'], - 'document' => $filepath, - 'caption' => $temp['text'], - ] - ); - unlink($filepath); - break; - case '?quantumult=3': - $temp['text'] = '点击打开配置文件,选择分享 拷贝到 Quantumult,选择更新配置.'; - $token = LinkController::GenerateSSRSubCode($user->id, 0); - $filename = 'Quantumult_' . $token . '_' . time() . '.conf'; - $filepath = BASE_PATH . '/storage/SendTelegram/' . $filename; - $fh = fopen($filepath, 'w+'); - $string = LinkController::GetQuantumult($user, 3, [], [], false, 0); - fwrite($fh, $string); - fclose($fh); - $bot->sendDocument( - [ - 'chat_id' => $Data['ChatID'], - 'document' => $filepath, - 'caption' => $temp['text'], - ] - ); - unlink($filepath); - break; - case '?surge=2': - $temp['text'] = '点击打开配置文件,选择分享 拷贝到 Surge,点击启动.'; - $token = LinkController::GenerateSSRSubCode($user->id, 0); - $filename = 'Surge_' . $token . '_' . time() . '.conf'; - $filepath = BASE_PATH . '/storage/SendTelegram/' . $filename; - $fh = fopen($filepath, 'w+'); - $string = LinkController::getSurge($user, 2, [], [], false, 0); - fwrite($fh, $string); - fclose($fh); - $bot->sendDocument( - [ - 'chat_id' => $Data['ChatID'], - 'document' => $filepath, - 'caption' => $temp['text'], - ] - ); - unlink($filepath); - break; - case '?surge=3': - $temp['text'] = '点击打开配置文件,选择分享 拷贝到 Surge,点击启动.'; - $token = LinkController::GenerateSSRSubCode($user->id, 0); - $filename = 'Surge_' . $token . '_' . time() . '.conf'; - $filepath = BASE_PATH . '/storage/SendTelegram/' . $filename; - $fh = fopen($filepath, 'w+'); - $string = LinkController::getSurge($user, 3, [], [], false, 0); - fwrite($fh, $string); - fclose($fh); - $bot->sendDocument( - [ - 'chat_id' => $Data['ChatID'], - 'document' => $filepath, - 'caption' => $temp['text'], - ] - ); - unlink($filepath); - break; - default: - $temp['text'] = '该订阅链接为:' . PHP_EOL . PHP_EOL . $UserApiUrl . $CallbackDataExplode[1]; - break; - } - } else { - $temp = Reply::getInlinekeyboard($user, 'user.subscribe'); - } - $sendMessage = [ - 'text' => $temp['text'], - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $temp['keyboard'] - ] - ), - ]; - break; - case 'invite': - // 分享计划 - $op_2 = $Operate[2]; - switch ($op_2) { - case 'get': - $Data['AllowEditMessage'] = false; - $code = InviteCode::where('user_id', $user->id)->first(); - if ($code == null) { - $user->addInviteCode(); - $code = InviteCode::where('user_id', $user->id)->first(); - } - $inviteUrl = Config::get('baseUrl') . '/auth/register?code=' . $code->code; - $text = '' . $inviteUrl . ''; - $sendMessage = [ - 'text' => $text, - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => null - ]; - break; - default: - if (!$paybacks_sum = Payback::where('ref_by', $user->id)->sum('ref_get')) { - $paybacks_sum = 0; - } - $text = [ - '分享计划,您每邀请 1 位用户注册:', - '', - '- 您会获得 ' . Config::get('invite_gift') . 'G 流量奖励.', - '- 对方将获得 ' . Config::get('invite_get_money') . ' 元 奖励作为初始资金.', - '- 对方充值时您还会获得对方充值金额的 ' . Config::get('code_payback') . '% 的返利.', - '', - '已获得返利:' . $paybacks_sum . ' 元.', - ]; - $keyboard = [ - [ - [ - 'text' => '获取我的邀请链接', - 'callback_data' => 'user.invite.get' - ] - ], - Reply::getInlinekeyboard() - ]; - $sendMessage = [ - 'text' => implode(PHP_EOL, $text), - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $keyboard - ] - ), - ]; - break; - } - break; - case 'checkin': - // 签到 - $op_2 = $Operate[2]; - $triggerUser = Process::getUser($op_2); - if ($triggerUser->id != $user->id) { - return Process::SendPost( - 'answerCallbackQuery', - [ - 'callback_query_id' => $Callback->getId(), - 'text' => '您无法从他人的消息中签到.', - 'show_alert' => true, - ] - ); - } - $checkin = $user->checkin(); - $text = $checkin['msg']; - // 回送信息 - $sendMessage = [ - 'text' => $text, - 'reply_to_message_id' => $Data['MessageID'], - 'parse_mode' => 'Markdown', - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => [ - [ - [ - 'text' => '已签到', - 'callback_data' => 'user.checkin.' . $SendUser['id'] - ] - ], - ] - ] - ), - ]; - break; - default: - // 用户中心 - $op_2 = $Operate[2]; - switch ($op_2) { - case 'login_log': - // 登录记录 - $iplocation = new QQWry(); - $totallogin = LoginIp::where('userid', '=', $user->id)->where('type', '=', 0)->orderBy('datetime', 'desc')->take(10)->get(); - $userloginip = []; - foreach ($totallogin as $single) { - $location = $iplocation->getlocation($single->ip); - $loginiplocation = iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']); - if (!in_array($loginiplocation, $userloginip)) { - $userloginip[] = $loginiplocation; - } - } - $text = ('以下是您最近 10 次的登录位置:' . - PHP_EOL . - PHP_EOL . - implode('、', $userloginip)); - $sendMessage = [ - 'text' => $text, - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => [ - Reply::getInlinekeyboard() - ] - ] - ), - ]; - break; - case 'usage_log': - // 使用记录 - $iplocation = new QQWry(); - $total = Ip::where('datetime', '>=', time() - 300)->where('userid', '=', $user->id)->get(); - $userip = []; - foreach ($total as $single) { - $single->ip = Tools::getRealIp($single->ip); - $is_node = Node::where('node_ip', $single->ip)->first(); - if ($is_node) { - continue; - } - $location = $iplocation->getlocation($single->ip); - $userip[$single->ip] = '[' . $single->ip . '] ' . iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']); - } - $text = ('以下是您最近 5 分钟的使用 IP:' . - PHP_EOL . - PHP_EOL . - implode(PHP_EOL, $userip)); - $sendMessage = [ - 'text' => $text, - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => [ - Reply::getInlinekeyboard() - ] - ] - ), - ]; - break; - case 'rebate_log': - // 返利记录 - $paybacks = Payback::where('ref_by', $user->id)->orderBy('datetime', 'desc')->take(10)->get(); - $temp = []; - foreach ($paybacks as $payback) { - $temp[] = '#' . $payback->id . ':' . ($payback->user() != null ? $payback->user()->user_name : '已注销') . ':' . $payback->ref_get . ' 元'; - } - $text = ('以下是您最近 10 条返利记录:' . - PHP_EOL . - PHP_EOL . - implode(PHP_EOL, $temp)); - $sendMessage = [ - 'text' => $text, - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => [ - Reply::getInlinekeyboard() - ] - ] - ), - ]; - break; - case 'subscribe_log': - // 订阅记录 - $iplocation = new QQWry(); - $logs = UserSubscribeLog::orderBy('id', 'desc')->where('user_id', $user->id)->take(10)->get(); - $temp = []; - foreach ($logs as $log) { - $location = $iplocation->getlocation($log->request_ip); - $temp[] = '' . $log->request_time . ' 在 [' . $log->request_ip . '] ' . iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']) . ' 访问了 ' . $log->subscribe_type . ' 订阅'; - } - $text = ('以下是您最近 10 条订阅记录:' . - PHP_EOL . - PHP_EOL . - implode(PHP_EOL . PHP_EOL, $temp)); - $sendMessage = [ - 'text' => $text, - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => [ - Reply::getInlinekeyboard() - ] - ] - ), - ]; - break; - default: - $temp = Reply::getInlinekeyboard($user, 'user.index'); - $sendMessage = [ - 'text' => $temp['text'], - 'disable_web_page_preview' => false, - 'reply_to_message_id' => null, - 'reply_markup' => json_encode( - [ - 'inline_keyboard' => $temp['keyboard'] - ] - ), - ]; - break; - } - break; - } - $sendMessage = array_merge( - $sendMessage, - [ - 'chat_id' => $Data['ChatID'], - 'message_id' => $Data['MessageID'], - 'parse_mode' => 'HTML', - ] - ); - if ($Data['AllowEditMessage']) { - // 消息可编辑 - Process::SendPost('editMessageText', $sendMessage); - return; - } - $bot->sendMessage($sendMessage); - } -} diff --git a/app/Utils/Telegram/Callbacks/Callback.php b/app/Utils/Telegram/Callbacks/Callback.php new file mode 100644 index 00000000..92e4ce43 --- /dev/null +++ b/app/Utils/Telegram/Callbacks/Callback.php @@ -0,0 +1,120 @@ + $Callback->getFrom()->getId(), + 'name' => $Callback->getFrom()->getFirstName() . ' ' . $Callback->getFrom()->getLastName(), + 'username' => $Callback->getFrom()->getUsername(), + ]; + + // 触发源消息发送时间 + $MessageSendTime = $Callback->getMessage()->getDate(); + + // 消息是否可编辑 + $AllowEditMessage = (time() < $MessageSendTime + 172800 + ? true + : false); + + $Data = [ + 'CallbackData' => $Callback->getData(), // 回调数据 + 'ChatID' => $Callback->getMessage()->getChat()->getId(), // 消息会话 ID + 'MessageID' => $Callback->getMessage()->getMessageId(), // 触发源信息 ID + 'AllowEditMessage' => $AllowEditMessage, // 消息是否可编辑 + ]; + + if ($Data['ChatID'] < 0 && Config::get('telegram_group_quiet') === true) { + // 群组中不回应 + return; + } + + switch (true) { + case (strpos($Data['CallbackData'], 'user.') === 0): + // 用户相关 + UserCallback::handler($bot, $Callback, $Data, $SendUser); + break; + default: + // 回调数据处理 + self::CallbackDataHandler($bot, $Callback, $Data, $SendUser); + break; + } + } + + /** + * + * 回调数据处理 + * + */ + public static function CallbackDataHandler($bot, $Callback, $Data, $SendUser) + { + $CallbackDataExplode = explode('|', $Data['CallbackData']); + switch ($CallbackDataExplode[0]) { + case 'general.pricing': + // 产品介绍 + $sendMessage = [ + 'text' => Config::get('telegram_general_pricing'), + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => [ + Reply::getInlinekeyboard() + ] + ] + ), + ]; + break; + case 'general.terms': + // 服务条款 + $sendMessage = [ + 'text' => Config::get('telegram_general_terms'), + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => [ + Reply::getInlinekeyboard() + ] + ] + ), + ]; + break; + default: + // 主菜单 + $temp = Reply::getInlinekeyboard(null, 'index'); + $sendMessage = [ + 'text' => $temp['text'], + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $temp['keyboard'] + ] + ), + ]; + break; + } + $sendMessage = array_merge( + $sendMessage, + [ + 'chat_id' => $Data['ChatID'], + 'message_id' => $Data['MessageID'], + 'parse_mode' => 'HTML', + ] + ); + if ($Data['AllowEditMessage']) { + // 消息可编辑 + TelegramTools::SendPost('editMessageText', $sendMessage); + return; + } + $bot->sendMessage($sendMessage); + } +} diff --git a/app/Utils/Telegram/Callbacks/UserCallback.php b/app/Utils/Telegram/Callbacks/UserCallback.php new file mode 100644 index 00000000..aa2aff18 --- /dev/null +++ b/app/Utils/Telegram/Callbacks/UserCallback.php @@ -0,0 +1,785 @@ + $Callback->getId(), + 'text' => '您好,您尚未绑定账户,无法进行操作.', + 'show_alert' => true, + ] + ); + } else { + return Callback::CallbackDataHandler($bot, $Callback, $Data, $SendUser); + } + } + if ($user->telegram_id != $SendUser['id']) { + return TelegramTools::SendPost( + 'answerCallbackQuery', + [ + 'callback_query_id' => $Callback->getId(), + 'text' => '您好,您无法操作他人的账户.', + 'show_alert' => true, + ] + ); + } + + $CallbackDataExplode = explode('|', $Data['CallbackData']); + $Operate = explode('.', $CallbackDataExplode[0]); + $op_1 = $Operate[1]; + switch ($op_1) { + case 'edit': + // 资料编辑 + self::UserEdit($user, $bot, $Callback, $Data, $SendUser); + break; + case 'subscribe': + // 订阅中心 + self::UserSubscribe($user, $bot, $Callback, $Data, $SendUser); + break; + case 'invite': + // 分享计划 + self::UserInvite($user, $bot, $Callback, $Data, $SendUser); + break; + case 'checkin': + // 签到 + self::UserCheckin($user, $bot, $Callback, $Data, $SendUser); + break; + default: + // 用户中心 + self::UserCenter($user, $bot, $Callback, $Data, $SendUser); + break; + } + } + + /** + * + * 用户中心 + * + */ + public static function UserCenter($user, $bot, $Callback, $Data, $SendUser) + { + $CallbackDataExplode = explode('|', $Data['CallbackData']); + $Operate = explode('.', $CallbackDataExplode[0]); + $op_2 = $Operate[2]; + switch ($op_2) { + case 'login_log': + // 登录记录 + $iplocation = new QQWry(); + $totallogin = LoginIp::where('userid', '=', $user->id)->where('type', '=', 0)->orderBy('datetime', 'desc')->take(10)->get(); + $userloginip = []; + foreach ($totallogin as $single) { + $location = $iplocation->getlocation($single->ip); + $loginiplocation = iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']); + if (!in_array($loginiplocation, $userloginip)) { + $userloginip[] = $loginiplocation; + } + } + $text = ('以下是您最近 10 次的登录位置:' . + PHP_EOL . + PHP_EOL . + implode('、', $userloginip)); + $sendMessage = [ + 'text' => $text, + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => [ + Reply::getInlinekeyboard() + ] + ] + ), + ]; + break; + case 'usage_log': + // 使用记录 + $iplocation = new QQWry(); + $total = Ip::where('datetime', '>=', time() - 300)->where('userid', '=', $user->id)->get(); + $userip = []; + foreach ($total as $single) { + $single->ip = Tools::getRealIp($single->ip); + $is_node = Node::where('node_ip', $single->ip)->first(); + if ($is_node) { + continue; + } + $location = $iplocation->getlocation($single->ip); + $userip[$single->ip] = '[' . $single->ip . '] ' . iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']); + } + $text = ('以下是您最近 5 分钟的使用 IP:' . + PHP_EOL . + PHP_EOL . + implode(PHP_EOL, $userip)); + $sendMessage = [ + 'text' => $text, + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => [ + Reply::getInlinekeyboard() + ] + ] + ), + ]; + break; + case 'rebate_log': + // 返利记录 + $paybacks = Payback::where('ref_by', $user->id)->orderBy('datetime', 'desc')->take(10)->get(); + $temp = []; + foreach ($paybacks as $payback) { + $temp[] = '#' . $payback->id . ':' . ($payback->user() != null ? $payback->user()->user_name : '已注销') . ':' . $payback->ref_get . ' 元'; + } + $text = ('以下是您最近 10 条返利记录:' . + PHP_EOL . + PHP_EOL . + implode(PHP_EOL, $temp)); + $sendMessage = [ + 'text' => $text, + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => [ + Reply::getInlinekeyboard() + ] + ] + ), + ]; + break; + case 'subscribe_log': + // 订阅记录 + $iplocation = new QQWry(); + $logs = UserSubscribeLog::orderBy('id', 'desc')->where('user_id', $user->id)->take(10)->get(); + $temp = []; + foreach ($logs as $log) { + $location = $iplocation->getlocation($log->request_ip); + $temp[] = '' . $log->request_time . ' 在 [' . $log->request_ip . '] ' . iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']) . ' 访问了 ' . $log->subscribe_type . ' 订阅'; + } + $text = ('以下是您最近 10 条订阅记录:' . + PHP_EOL . + PHP_EOL . + implode(PHP_EOL . PHP_EOL, $temp)); + $sendMessage = [ + 'text' => $text, + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => [ + Reply::getInlinekeyboard() + ] + ] + ), + ]; + break; + default: + $temp = Reply::getInlinekeyboard($user, 'user.index'); + $sendMessage = [ + 'text' => $temp['text'], + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $temp['keyboard'] + ] + ), + ]; + break; + } + $sendMessage = array_merge( + $sendMessage, + [ + 'chat_id' => $Data['ChatID'], + 'message_id' => $Data['MessageID'], + 'parse_mode' => 'HTML', + ] + ); + if ($Data['AllowEditMessage']) { + // 消息可编辑 + TelegramTools::SendPost('editMessageText', $sendMessage); + return; + } + $bot->sendMessage($sendMessage); + } + + /** + * + * 用户编辑 + * + */ + public static function UserEdit($user, $bot, $Callback, $Data, $SendUser) + { + $CallbackDataExplode = explode('|', $Data['CallbackData']); + $Operate = explode('.', $CallbackDataExplode[0]); + $op_2 = $Operate[2]; + switch ($op_2) { + case 'update_link': + // 重置订阅链接 + $temp = Reply::getInlinekeyboard($user, 'user.subscribe'); + $user->clean_link(); + $sendMessage = [ + 'text' => '订阅链接重置成功,请在下方重新更新订阅.', + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $temp['keyboard'] + ] + ), + ]; + break; + case 'update_passwd': + // 重置链接密码 + $user->passwd = Tools::genRandomChar(8); + if ($user->save()) { + $temp = Reply::getInlinekeyboard($user, 'user.subscribe'); + $sendMessage = [ + 'text' => '连接密码更新成功,请在下方重新更新订阅.' . PHP_EOL . PHP_EOL . '新的连接密码为:' . $user->passwd, + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $temp['keyboard'] + ] + ), + ]; + } else { + $temp = Reply::getInlinekeyboard(); + $sendMessage = [ + 'text' => '出现错误,连接密码更新失败,请联系管理员.', + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $temp['keyboard'] + ] + ), + ]; + } + break; + case 'encrypt': + // 加密方式更改 + $keyboard = [ + Reply::getInlinekeyboard() + ]; + if (Config::get('protocol_specify') === true) { + if (isset($CallbackDataExplode[1])) { + if (in_array($CallbackDataExplode[1], Config::getSupportParam('method')) && Config::get('protocol_specify') === true) { + $temp = $user->setMethod($CallbackDataExplode[1]); + if ($temp['ok'] === true) { + $text = '您当前的加密方式为:' . $user->method . PHP_EOL . PHP_EOL . $temp['msg']; + } else { + $text = '发生错误,请重新选择.' . PHP_EOL . PHP_EOL . $temp['msg']; + } + } else { + $text = '发生错误,请重新选择.'; + } + } else { + $Encrypts = []; + foreach (Config::getSupportParam('method') as $value) { + $Encrypts[] = [ + 'text' => $value, + 'callback_data' => 'user.edit.encrypt|' . $value + ]; + } + $Encrypts = array_chunk($Encrypts, 2); + $keyboard = []; + foreach ($Encrypts as $Encrypt) { + $keyboard[] = $Encrypt; + } + $keyboard[] = Reply::getInlinekeyboard(); + $text = '您当前的加密方式为:' . $user->method; + } + } else { + $text = '当前不允许私自更改.'; + } + $sendMessage = [ + 'text' => $text, + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $keyboard + ] + ), + ]; + break; + case 'protocol': + // 协议更改 + $keyboard = [ + Reply::getInlinekeyboard() + ]; + if (Config::get('protocol_specify') === true) { + if (isset($CallbackDataExplode[1])) { + if (in_array($CallbackDataExplode[1], Config::getSupportParam('protocol')) && Config::get('protocol_specify') === true) { + $temp = $user->setProtocol($CallbackDataExplode[1]); + if ($temp['ok'] === true) { + $text = '您当前的协议为:' . $user->protocol . PHP_EOL . PHP_EOL . $temp['msg']; + } else { + $text = '发生错误,请重新选择.' . PHP_EOL . PHP_EOL . $temp['msg']; + } + } else { + $text = '发生错误,请重新选择.'; + } + } else { + $Protocols = []; + foreach (Config::getSupportParam('protocol') as $value) { + $Protocols[] = [ + 'text' => $value, + 'callback_data' => 'user.edit.protocol|' . $value + ]; + } + $Protocols = array_chunk($Protocols, 1); + $keyboard = []; + foreach ($Protocols as $Protocol) { + $keyboard[] = $Protocol; + } + $keyboard[] = Reply::getInlinekeyboard(); + $text = '您当前的协议为:' . $user->protocol; + } + } else { + $text = '当前不允许私自更改.'; + } + $sendMessage = [ + 'text' => $text, + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $keyboard + ] + ), + ]; + break; + case 'obfs': + // 混淆更改 + $keyboard = [ + Reply::getInlinekeyboard() + ]; + if (Config::get('protocol_specify') === true) { + if (isset($CallbackDataExplode[1])) { + if (in_array($CallbackDataExplode[1], Config::getSupportParam('obfs')) && Config::get('protocol_specify') === true) { + $temp = $user->setObfs($CallbackDataExplode[1]); + if ($temp['ok'] === true) { + $text = '您当前的协议为:' . $user->obfs . PHP_EOL . PHP_EOL . $temp['msg']; + } else { + $text = '发生错误,请重新选择.' . PHP_EOL . PHP_EOL . $temp['msg']; + } + } else { + $text = '发生错误,请重新选择.'; + } + } else { + $Obfss = []; + foreach (Config::getSupportParam('obfs') as $value) { + $Obfss[] = [ + 'text' => $value, + 'callback_data' => 'user.edit.obfs|' . $value + ]; + } + $Obfss = array_chunk($Obfss, 1); + $keyboard = []; + foreach ($Obfss as $Obfs) { + $keyboard[] = $Obfs; + } + $keyboard[] = Reply::getInlinekeyboard(); + $text = '您当前的协议为:' . $user->obfs; + } + } else { + $text = '当前不允许私自更改.'; + } + $sendMessage = [ + 'text' => $text, + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $keyboard + ] + ), + ]; + break; + case 'sendemail': + // 每日邮件设置更改 + $keyboard = [ + [ + [ + 'text' => '更改开启/关闭', + 'callback_data' => 'user.edit.sendemail.update' + ] + ], + Reply::getInlinekeyboard() + ]; + $op_3 = $Operate[3]; + switch ($op_3) { + case 'update': + $user->sendDailyMail = ($user->sendDailyMail == 0 ? 1 : 0); + if ($user->save()) { + $text = '设置更改成功,每日邮件接收当前设置为:'; + $text .= ''; + $text .= ($user->sendDailyMail == 0 ? '不发送' : '发送'); + $text .= ''; + } else { + $text = '发生错误.'; + } + break; + default: + $text = '每日邮件接收当前设置为:'; + $text .= ''; + $text .= ($user->sendDailyMail == 0 ? '不发送' : '发送'); + $text .= ''; + break; + } + $sendMessage = [ + 'text' => $text, + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $keyboard + ] + ), + ]; + break; + case 'unbind': + // Telegram 账户解绑 + $Data['AllowEditMessage'] = false; + $sendMessage = [ + 'text' => '发送 /unbind 账户邮箱 进行解绑.', + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => null + ]; + break; + case 'unban': + // 群组解封 + TelegramTools::SendPost( + 'unbanChatMember', + [ + 'chat_id' => Config::get('telegram_chatid'), + 'user_id' => $SendUser['id'], + ] + ); + return TelegramTools::SendPost( + 'answerCallbackQuery', + [ + 'callback_query_id' => $Callback->getId(), + 'text' => '已提交解封,如您仍无法加入群组,请联系管理员.', + 'show_alert' => true, + ] + ); + break; + default: + $temp = Reply::getInlinekeyboard($user, 'user.edit'); + $text = '您可在此编辑您的资料或连接信息:' . PHP_EOL . PHP_EOL; + $text .= '端口:' . $user->port . PHP_EOL; + $text .= '密码:' . $user->passwd . PHP_EOL; + $text .= '加密:' . $user->method . PHP_EOL; + $text .= '协议:' . $user->protocol . PHP_EOL; + $text .= '混淆:' . $user->obfs; + $sendMessage = [ + 'text' => $text, + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $temp['keyboard'] + ] + ), + ]; + break; + } + $sendMessage = array_merge( + $sendMessage, + [ + 'chat_id' => $Data['ChatID'], + 'message_id' => $Data['MessageID'], + 'parse_mode' => 'HTML', + ] + ); + if ($Data['AllowEditMessage']) { + // 消息可编辑 + TelegramTools::SendPost('editMessageText', $sendMessage); + return; + } + $bot->sendMessage($sendMessage); + } + + /** + * + * 用户订阅 + * + */ + public static function UserSubscribe($user, $bot, $Callback, $Data, $SendUser) + { + $CallbackDataExplode = explode('|', $Data['CallbackData']); + $Operate = explode('.', $CallbackDataExplode[0]); + // 订阅中心 + if (isset($CallbackDataExplode[1])) { + $temp = []; + $temp['keyboard'] = [ + Reply::getInlinekeyboard() + ]; + $UserApiUrl = LinkController::getSubinfo($user, 0)['link']; + switch ($CallbackDataExplode[1]) { + case '?clash=1': + $temp['text'] = '您的 Clash 配置文件.' . PHP_EOL . '同时,您也可使用该订阅链接:' . $UserApiUrl . $CallbackDataExplode[1]; + $token = LinkController::GenerateSSRSubCode($user->id, 0); + $filename = 'Clash_' . $token . '_' . time() . '.yaml'; + $filepath = BASE_PATH . '/storage/SendTelegram/' . $filename; + $fh = fopen($filepath, 'w+'); + $string = LinkController::getClash($user, 1, [], [], false, 0); + fwrite($fh, $string); + fclose($fh); + $bot->sendDocument( + [ + 'chat_id' => $Data['ChatID'], + 'document' => $filepath, + 'caption' => $temp['text'], + ] + ); + unlink($filepath); + break; + case '?clash=2': + $temp['text'] = '您的 ClashR 配置文件.' . PHP_EOL . '同时,您也可使用该订阅链接:' . $UserApiUrl . $CallbackDataExplode[1]; + $token = LinkController::GenerateSSRSubCode($user->id, 0); + $filename = 'ClashR_' . $token . '_' . time() . '.yaml'; + $filepath = BASE_PATH . '/storage/SendTelegram/' . $filename; + $fh = fopen($filepath, 'w+'); + $string = LinkController::getClash($user, 2, [], [], false, 0); + fwrite($fh, $string); + fclose($fh); + $bot->sendDocument( + [ + 'chat_id' => $Data['ChatID'], + 'document' => $filepath, + 'caption' => $temp['text'], + ] + ); + unlink($filepath); + break; + case '?quantumult=3': + $temp['text'] = '点击打开配置文件,选择分享 拷贝到 Quantumult,选择更新配置.'; + $token = LinkController::GenerateSSRSubCode($user->id, 0); + $filename = 'Quantumult_' . $token . '_' . time() . '.conf'; + $filepath = BASE_PATH . '/storage/SendTelegram/' . $filename; + $fh = fopen($filepath, 'w+'); + $string = LinkController::GetQuantumult($user, 3, [], [], false, 0); + fwrite($fh, $string); + fclose($fh); + $bot->sendDocument( + [ + 'chat_id' => $Data['ChatID'], + 'document' => $filepath, + 'caption' => $temp['text'], + ] + ); + unlink($filepath); + break; + case '?surge=2': + $temp['text'] = '点击打开配置文件,选择分享 拷贝到 Surge,点击启动.'; + $token = LinkController::GenerateSSRSubCode($user->id, 0); + $filename = 'Surge_' . $token . '_' . time() . '.conf'; + $filepath = BASE_PATH . '/storage/SendTelegram/' . $filename; + $fh = fopen($filepath, 'w+'); + $string = LinkController::getSurge($user, 2, [], [], false, 0); + fwrite($fh, $string); + fclose($fh); + $bot->sendDocument( + [ + 'chat_id' => $Data['ChatID'], + 'document' => $filepath, + 'caption' => $temp['text'], + ] + ); + unlink($filepath); + break; + case '?surge=3': + $temp['text'] = '点击打开配置文件,选择分享 拷贝到 Surge,点击启动.'; + $token = LinkController::GenerateSSRSubCode($user->id, 0); + $filename = 'Surge_' . $token . '_' . time() . '.conf'; + $filepath = BASE_PATH . '/storage/SendTelegram/' . $filename; + $fh = fopen($filepath, 'w+'); + $string = LinkController::getSurge($user, 3, [], [], false, 0); + fwrite($fh, $string); + fclose($fh); + $bot->sendDocument( + [ + 'chat_id' => $Data['ChatID'], + 'document' => $filepath, + 'caption' => $temp['text'], + ] + ); + unlink($filepath); + break; + default: + $temp['text'] = '该订阅链接为:' . PHP_EOL . PHP_EOL . $UserApiUrl . $CallbackDataExplode[1]; + break; + } + } else { + $temp = Reply::getInlinekeyboard($user, 'user.subscribe'); + } + $sendMessage = [ + 'text' => $temp['text'], + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $temp['keyboard'] + ] + ), + ]; + $sendMessage = array_merge( + $sendMessage, + [ + 'chat_id' => $Data['ChatID'], + 'message_id' => $Data['MessageID'], + 'parse_mode' => 'HTML', + ] + ); + if ($Data['AllowEditMessage']) { + // 消息可编辑 + TelegramTools::SendPost('editMessageText', $sendMessage); + return; + } + $bot->sendMessage($sendMessage); + } + + /** + * + * 分享计划 + * + */ + public static function UserInvite($user, $bot, $Callback, $Data, $SendUser) + { + $CallbackDataExplode = explode('|', $Data['CallbackData']); + $Operate = explode('.', $CallbackDataExplode[0]); + $op_2 = $Operate[2]; + switch ($op_2) { + case 'get': + $Data['AllowEditMessage'] = false; + $code = InviteCode::where('user_id', $user->id)->first(); + if ($code == null) { + $user->addInviteCode(); + $code = InviteCode::where('user_id', $user->id)->first(); + } + $inviteUrl = Config::get('baseUrl') . '/auth/register?code=' . $code->code; + $text = '' . $inviteUrl . ''; + $sendMessage = [ + 'text' => $text, + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => null + ]; + break; + default: + if (!$paybacks_sum = Payback::where('ref_by', $user->id)->sum('ref_get')) { + $paybacks_sum = 0; + } + $text = [ + '分享计划,您每邀请 1 位用户注册:', + '', + '- 您会获得 ' . Config::get('invite_gift') . 'G 流量奖励.', + '- 对方将获得 ' . Config::get('invite_get_money') . ' 元 奖励作为初始资金.', + '- 对方充值时您还会获得对方充值金额的 ' . Config::get('code_payback') . '% 的返利.', + '', + '已获得返利:' . $paybacks_sum . ' 元.', + ]; + $keyboard = [ + [ + [ + 'text' => '获取我的邀请链接', + 'callback_data' => 'user.invite.get' + ] + ], + Reply::getInlinekeyboard() + ]; + $sendMessage = [ + 'text' => implode(PHP_EOL, $text), + 'disable_web_page_preview' => false, + 'reply_to_message_id' => null, + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => $keyboard + ] + ), + ]; + break; + } + $sendMessage = array_merge( + $sendMessage, + [ + 'chat_id' => $Data['ChatID'], + 'message_id' => $Data['MessageID'], + 'parse_mode' => 'HTML', + ] + ); + if ($Data['AllowEditMessage']) { + // 消息可编辑 + TelegramTools::SendPost('editMessageText', $sendMessage); + return; + } + $bot->sendMessage($sendMessage); + } + + /** + * + * 每日签到 + * + */ + public static function UserCheckin($user, $bot, $Callback, $Data, $SendUser) + { + $checkin = $user->checkin(); + $text = $checkin['msg']; + // 回送信息 + $sendMessage = [ + 'text' => $text, + 'reply_to_message_id' => $Data['MessageID'], + 'parse_mode' => 'Markdown', + 'reply_markup' => json_encode( + [ + 'inline_keyboard' => [ + [ + [ + 'text' => '已签到', + 'callback_data' => 'user.checkin.' . $SendUser['id'] + ] + ], + ] + ] + ), + ]; + $sendMessage = array_merge( + $sendMessage, + [ + 'chat_id' => $Data['ChatID'], + 'message_id' => $Data['MessageID'], + 'parse_mode' => 'HTML', + ] + ); + if ($Data['AllowEditMessage']) { + // 消息可编辑 + TelegramTools::SendPost('editMessageText', $sendMessage); + return; + } + $bot->sendMessage($sendMessage); + } +} diff --git a/app/Utils/Telegram/Commands/CheckinCommand.php b/app/Utils/Telegram/Commands/CheckinCommand.php index dde12b36..33b0168c 100644 --- a/app/Utils/Telegram/Commands/CheckinCommand.php +++ b/app/Utils/Telegram/Commands/CheckinCommand.php @@ -68,17 +68,17 @@ class CheckinCommand extends Command $response = $this->replyWithMessage( [ 'text' => Config::get('user_not_bind_reply'), - 'parse_mode' => 'MarkdownV2', + 'parse_mode' => 'Markdown', ] ); } else { - $checkin = $User->checkin(); + $checkin = $User->checkin(); // 回送信息 $response = $this->replyWithMessage( [ 'text' => $checkin['msg'], 'reply_to_message_id' => $Message->getMessageId(), - 'parse_mode' => 'MarkdownV2', + 'parse_mode' => 'Markdown', ] ); } diff --git a/app/Utils/Telegram/Commands/InfoCommand.php b/app/Utils/Telegram/Commands/InfoCommand.php index 59230f64..d9441950 100644 --- a/app/Utils/Telegram/Commands/InfoCommand.php +++ b/app/Utils/Telegram/Commands/InfoCommand.php @@ -4,7 +4,7 @@ namespace App\Utils\Telegram\Commands; use App\Services\Config; use App\Models\User; -use App\Utils\Telegram\{Process, Reply, TelegramTools}; +use App\Utils\Telegram\{Reply, TelegramTools}; use Telegram\Bot\Actions; use Telegram\Bot\Commands\Command; @@ -81,7 +81,7 @@ class InfoCommand extends Command 'name' => $Message->getReplyToMessage()->getFrom()->getFirstName() . ' ' . $Message->getReplyToMessage()->getFrom()->getLastName(), 'username' => $Message->getReplyToMessage()->getFrom()->getUsername(), ]; - $User = Process::getUser($FindUser['id']); + $User = TelegramTools::getUser($FindUser['id']); if ($User == null) { $response = $this->replyWithMessage( [ diff --git a/app/Utils/Telegram/Commands/MenuCommand.php b/app/Utils/Telegram/Commands/MenuCommand.php index 542622a4..d5ae433c 100644 --- a/app/Utils/Telegram/Commands/MenuCommand.php +++ b/app/Utils/Telegram/Commands/MenuCommand.php @@ -2,7 +2,7 @@ namespace App\Utils\Telegram\Commands; -use App\Utils\Telegram\{Process, Reply}; +use App\Utils\Telegram\{TelegramTools, Reply}; use Telegram\Bot\Actions; use Telegram\Bot\Commands\Command; @@ -45,7 +45,7 @@ class MenuCommand extends Command 'username' => $Message->getFrom()->getUsername(), ]; - $user = Process::getUser($SendUser['id']); + $user = TelegramTools::getUser($SendUser['id']); $reply = Reply::getInlinekeyboard($user, 'index'); diff --git a/app/Utils/Telegram/Commands/PingCommand.php b/app/Utils/Telegram/Commands/PingCommand.php index 0a9dde12..dd44cd3c 100644 --- a/app/Utils/Telegram/Commands/PingCommand.php +++ b/app/Utils/Telegram/Commands/PingCommand.php @@ -46,7 +46,7 @@ class PingCommand extends Command // 回送信息 $this->replyWithMessage( [ - 'text' => implode(PHP_EOL ,$text), + 'text' => implode(PHP_EOL, $text), 'parse_mode' => 'Markdown', ] ); @@ -70,7 +70,7 @@ class PingCommand extends Command // 回送信息 $this->replyWithMessage( [ - 'text' => implode(PHP_EOL ,$text), + 'text' => implode(PHP_EOL, $text), ] ); } diff --git a/app/Utils/Telegram/Commands/SetuserCommand.php b/app/Utils/Telegram/Commands/SetuserCommand.php index b73f370c..0ad5d000 100644 --- a/app/Utils/Telegram/Commands/SetuserCommand.php +++ b/app/Utils/Telegram/Commands/SetuserCommand.php @@ -2,9 +2,9 @@ namespace App\Utils\Telegram\Commands; -use App\Models\{User, TelegramTasks}; +use App\Models\User; use App\Services\Config; -use App\Utils\Telegram\{Process, Reply, TelegramTools}; +use App\Utils\Telegram\{Reply, TelegramTools}; use Telegram\Bot\Actions; use Telegram\Bot\Commands\Command; @@ -92,7 +92,7 @@ class SetuserCommand extends Command 'name' => $Message->getReplyToMessage()->getFrom()->getFirstName() . ' ' . $Message->getReplyToMessage()->getFrom()->getLastName(), 'username' => $Message->getReplyToMessage()->getFrom()->getUsername(), ]; - $User = Process::getUser($FindUser['id']); + $User = TelegramTools::getUser($FindUser['id']); if ($User == null) { $response = $this->replyWithMessage( [ @@ -234,7 +234,7 @@ class SetuserCommand extends Command $useMethod = $useTempMethod; } } - $User = Process::getUser($UserCode, $useMethod); + $User = TelegramTools::getUser($UserCode, $useMethod); if ($User == null) { $response = $this->replyWithMessage( diff --git a/app/Utils/Telegram/Commands/StartCommand.php b/app/Utils/Telegram/Commands/StartCommand.php index 3b160064..acc820cd 100644 --- a/app/Utils/Telegram/Commands/StartCommand.php +++ b/app/Utils/Telegram/Commands/StartCommand.php @@ -5,7 +5,6 @@ namespace App\Utils\Telegram\Commands; use App\Models\User; use App\Services\Config; use App\Utils\TelegramSessionManager; -use App\Utils\Telegram\Process; use Telegram\Bot\Actions; use Telegram\Bot\Commands\Command; @@ -62,7 +61,7 @@ class StartCommand extends Command $BinsUser->im_value = $SendUser['username']; $BinsUser->save(); if ($BinsUser->is_admin >= 1) { - $text = '尊敬的**管理员**您好,恭喜绑定成功。' . PHP_EOL .'当前绑定邮箱为:' . $BinsUser->email; + $text = '尊敬的**管理员**您好,恭喜绑定成功。' . PHP_EOL . '当前绑定邮箱为:' . $BinsUser->email; } else { if ($BinsUser->class >= 1) { $text = '尊敬的 **VIP ' . $BinsUser->class . '** 用户您好.' . PHP_EOL . '恭喜您绑定成功,当前绑定邮箱为:' . $BinsUser->email; diff --git a/app/Utils/Telegram/Commands/UnbindCommand.php b/app/Utils/Telegram/Commands/UnbindCommand.php index 9572eb94..e2ab53cf 100644 --- a/app/Utils/Telegram/Commands/UnbindCommand.php +++ b/app/Utils/Telegram/Commands/UnbindCommand.php @@ -56,7 +56,7 @@ class UnbindCommand extends Command $this->replyWithMessage( [ 'text' => Config::get('user_not_bind_reply'), - 'parse_mode' => 'MarkdownV2', + 'parse_mode' => 'Markdown', ] ); return; @@ -72,7 +72,7 @@ class UnbindCommand extends Command $this->replyWithMessage( [ 'text' => $text, - 'parse_mode' => 'MarkdownV2', + 'parse_mode' => 'Markdown', ] ); return; @@ -87,7 +87,7 @@ class UnbindCommand extends Command $this->replyWithMessage( [ 'text' => $text, - 'parse_mode' => 'MarkdownV2', + 'parse_mode' => 'Markdown', ] ); } diff --git a/app/Utils/Telegram/Message.php b/app/Utils/Telegram/Message.php index 59940b07..5ab72389 100644 --- a/app/Utils/Telegram/Message.php +++ b/app/Utils/Telegram/Message.php @@ -8,7 +8,7 @@ use App\Services\Config; class Message { - public static function MessageMethod($user, $bot, $Message) + public static function MessageMethod($bot, $Message) { // 触发用户 $SendUser = [ @@ -17,6 +17,8 @@ class Message 'username' => $Message->getFrom()->getUsername(), ]; + $user = TelegramTools::getUser($SendUser['id']); + // 消息会话 ID $ChatID = $Message->getChat()->getId(); @@ -71,7 +73,7 @@ class Message 'reply_to_message_id' => $Message->getMessageId(), ] ); - Process::SendPost( + TelegramTools::SendPost( 'kickChatMember', [ 'chat_id' => $ChatID, @@ -99,7 +101,7 @@ class Message } } else { // 新成员加入群组 - $NewUser = Process::getUser($Member['id']); + $NewUser = TelegramTools::getUser($Member['id']); $deNewChatMember = json_decode($NewChatMember, true); if ( Config::get('group_bound_user') === true @@ -117,7 +119,7 @@ class Message 'reply_to_message_id' => $Message->getMessageId(), ] ); - Process::SendPost( + TelegramTools::SendPost( 'kickChatMember', [ 'chat_id' => $ChatID, diff --git a/app/Utils/Telegram/Process.php b/app/Utils/Telegram/Process.php index b423ca08..00c20456 100644 --- a/app/Utils/Telegram/Process.php +++ b/app/Utils/Telegram/Process.php @@ -28,44 +28,13 @@ class Process ); $update = $bot->commandsHandler(true); if ($update->getCallbackQuery() !== null) { - $id = $update->getCallbackQuery()->getFrom()->getId(); - $user = self::getUser($id); - Callback::CallbackQueryMethod($user, $bot, $update->getCallbackQuery()); + Callbacks\Callback::CallbackQueryMethod($bot, $update->getCallbackQuery()); } if ($update->getMessage() !== null) { - $id = $update->getMessage()->getFrom()->getId(); - $user = self::getUser($id); - Message::MessageMethod($user, $bot, $update->getMessage()); + Message::MessageMethod($bot, $update->getMessage()); } } catch (Exception $e) { $e->getMessage(); } } - - public static function getUser($value, $method = 'telegram_id') - { - return User::where($method, $value)->first(); - } - - /** - * Sends a POST request to Telegram Bot API. - * 伪异步,无结果返回. - * - * @param array $params - * - * @return string - */ - public static function SendPost($Method, $Params) - { - $URL = 'https://api.telegram.org/bot' . Config::get('telegram_token') . '/' . $Method; - $POSTData = json_encode($Params); - $C = curl_init(); - curl_setopt($C, CURLOPT_URL, $URL); - curl_setopt($C, CURLOPT_POST, 1); - curl_setopt($C, CURLOPT_HTTPHEADER, ['Content-Type:application/json; charset=utf-8']); - curl_setopt($C, CURLOPT_POSTFIELDS, $POSTData); - curl_setopt($C, CURLOPT_TIMEOUT, 1); - curl_exec($C); - curl_close($C); - } } diff --git a/app/Utils/Telegram/TelegramTools.php b/app/Utils/Telegram/TelegramTools.php index 1a69fe21..bb02a920 100644 --- a/app/Utils/Telegram/TelegramTools.php +++ b/app/Utils/Telegram/TelegramTools.php @@ -2,12 +2,48 @@ namespace App\Utils\Telegram; -use App\Models\TelegramTasks; +use App\Models\{TelegramTasks, User}; use App\Services\Config; use App\Utils\Tools; class TelegramTools { + + /** + * 搜索用户 + * + * @param string $value 搜索值 + * @param string $method 查找列 + * + * @return \App\Models\User + */ + public static function getUser($value, $method = 'telegram_id') + { + return User::where($method, $value)->first(); + } + + /** + * Sends a POST request to Telegram Bot API. + * 伪异步,无结果返回. + * + * @param array $params + * + * @return string + */ + public static function SendPost($Method, $Params) + { + $URL = 'https://api.telegram.org/bot' . Config::get('telegram_token') . '/' . $Method; + $POSTData = json_encode($Params); + $C = curl_init(); + curl_setopt($C, CURLOPT_URL, $URL); + curl_setopt($C, CURLOPT_POST, 1); + curl_setopt($C, CURLOPT_HTTPHEADER, ['Content-Type:application/json; charset=utf-8']); + curl_setopt($C, CURLOPT_POSTFIELDS, $POSTData); + curl_setopt($C, CURLOPT_TIMEOUT, 1); + curl_exec($C); + curl_close($C); + } + /** * 用户识别搜索字段 * diff --git a/config/.config.example.php b/config/.config.example.php index f90199e5..d2951546 100644 --- a/config/.config.example.php +++ b/config/.config.example.php @@ -143,6 +143,7 @@ $_ENV['finance_public'] = true; //财务报告是否向群公开 $_ENV['enable_welcome_message'] = true; //机器人发送欢迎消息 # Telegram BOT +$_ENV['use_new_telegram_bot'] = true; //是否使用新的 Telegram Bot $_ENV['enable_telegram'] = false; //是否开启Telegram Bot $_ENV['telegram_token'] = ''; //Telegram Bot 的 Token,跟 Father Bot 申请 $_ENV['telegram_bot'] = ''; //Telegram Bot 用户名 diff --git a/config/routes.php b/config/routes.php index e964ccdb..d628f609 100644 --- a/config/routes.php +++ b/config/routes.php @@ -59,12 +59,13 @@ $app->get('/500', App\Controllers\HomeController::class . ':page500'); $app->post('/notify', App\Controllers\HomeController::class . ':notify'); $app->get('/tos', App\Controllers\HomeController::class . ':tos'); $app->get('/staff', App\Controllers\HomeController::class . ':staff'); -$app->post('/telegram_callback', App\Controllers\HomeController::class . ':telegram'); -$app->post('/tomato_back/{type}', 'App\Services\Payment:notify'); -$app->get('/tomato_back/{type}', 'App\Services\Payment:notify'); +$app->post('/tomato_back/{type}', App\Services\Payment::class . 'notify'); +$app->get('/tomato_back/{type}', App\Services\Payment::class . 'notify'); // New Telegram $app->post('/TelegramCallback', App\Controllers\HomeController::class . ':NewTelegram'); +// Old Telegram +$app->post('/telegram_callback', App\Controllers\HomeController::class . ':telegram'); // User Center $app->group('/user', function () {