diff --git a/app/Command/XCat.php b/app/Command/XCat.php index 5e45cfcb..960dd878 100644 --- a/app/Command/XCat.php +++ b/app/Command/XCat.php @@ -110,6 +110,8 @@ class XCat return $this->iptest(); case ('delSubCache'): return $this->delSubCache(); + case ('setNewTelegram'): + return $this->setNewTelegram(); default: return $this->defaultAction(); } @@ -284,6 +286,19 @@ class XCat } } + public function setNewTelegram() + { + $WebhookUrl = (Config::get('baseUrl') . '/TelegramCallback?token=' . Config::get('new_telegram_request_token')); + $telegram = new \Telegram\Bot\Api(Config::get('new_telegram_token')); + $response = $telegram->setWebhook(['url' => $WebhookUrl]); + $response = json_decode($response, true); + if ($response['ok'] === true) { + echo("设置成功!"); + } else { + echo("设置失败!"); + } + } + public function initQQWry() { echo ('开始下载纯真 IP 数据库....'); diff --git a/app/Controllers/HomeController.php b/app/Controllers/HomeController.php index b8fa1a2a..b839e17f 100644 --- a/app/Controllers/HomeController.php +++ b/app/Controllers/HomeController.php @@ -10,6 +10,7 @@ use App\Utils\TelegramSessionManager; use App\Utils\TelegramProcess; use App\Utils\Geetest; use App\Utils\Tools; +use App\Utils\Telegram\Process; use Slim\Http\{Request, Response}; use Psr\Http\Message\ResponseInterface; @@ -123,6 +124,23 @@ class HomeController extends BaseController return $response->write($result); } + /** + * @param Request $request + * @param Response $response + * @param array $args + */ + public function NewTelegram($request, $response, $args): ResponseInterface + { + $token = $request->getQueryParam('token'); + if ($token == Config::get('new_telegram_request_token')) { + Process::index(); + $result = '1'; + } else { + $result = '0'; + } + return $response->write($result); + } + /** * @param Request $request * @param Response $response diff --git a/app/Utils/Telegram/Callback.php b/app/Utils/Telegram/Callback.php new file mode 100644 index 00000000..39146d3b --- /dev/null +++ b/app/Utils/Telegram/Callback.php @@ -0,0 +1,39 @@ + $Callback->getFrom()->getId(), + 'name' => $Callback->getFrom()->getFirstName() . ' ' . $Callback->getFrom()->getLastName(), + 'username' => $Callback->getFrom()->getUsername(), + ]; + + // 消息会话 ID + $ChatID = $Callback->getMessage()->getChat()->getId(); + + // 回调数据 + $CallbackData = $Callback->getData(); + + // 回调数据处理 + self::CallbackDataHandler($user, $bot, $Callback, $CallbackData, $ChatID, $SendUser); + } + + /** + * + * 回调数据处理 + * + */ + public static function CallbackDataHandler($user, $bot, $Callback, $CallbackData, $ChatID, $SendUser) + { + return; + } +} diff --git a/app/Utils/Telegram/Commands/HelpCommand.php b/app/Utils/Telegram/Commands/HelpCommand.php new file mode 100644 index 00000000..946000c5 --- /dev/null +++ b/app/Utils/Telegram/Commands/HelpCommand.php @@ -0,0 +1,71 @@ +getUpdate(); + $Message = $Update->getMessage(); + + if ($Message->getChat()->getId() > 0) { + // 私人会话 + + // 发送 '输入中' 会话状态 + $this->replyWithChatAction(['action' => Actions::TYPING]); + + // 触发用户 + $SendUser = [ + 'id' => $Message->getFrom()->getId(), + 'name' => $Message->getFrom()->getFirstName() . ' ' . $Message->getFrom()->getLastName(), + 'username' => $Message->getFrom()->getUsername(), + ]; + + // 回送信息 + $this->replyWithMessage( + [ + 'text' => '喵?', + ] + ); + } else { + // 群组 + + if (Config::get('new_telegram_group_quiet') === true) { + // 群组中不回应 + return; + } + + // 发送 '输入中' 会话状态 + $this->replyWithChatAction(['action' => Actions::TYPING]); + + // 回送信息 + $this->replyWithMessage( + [ + 'text' => '喵?', + ] + ); + } + } +} diff --git a/app/Utils/Telegram/Commands/StartCommand.php b/app/Utils/Telegram/Commands/StartCommand.php new file mode 100644 index 00000000..f444eb7c --- /dev/null +++ b/app/Utils/Telegram/Commands/StartCommand.php @@ -0,0 +1,100 @@ +getUpdate(); + $Message = $Update->getMessage(); + + if ($Message->getChat()->getId() > 0) { + // 私人会话 + + // 发送 '输入中' 会话状态 + $this->replyWithChatAction(['action' => Actions::TYPING]); + + // 触发用户 + $SendUser = [ + 'id' => $Message->getFrom()->getId(), + 'name' => $Message->getFrom()->getFirstName() . ' ' . $Message->getFrom()->getLastName(), + 'username' => $Message->getFrom()->getUsername(), + ]; + // 消息内容 + $MessageText = $Message->getText(); + if (strpos($MessageText, ' ') !== false) { + $MessageText = str_replace('/' . $this->name, '', $MessageText); + $BindCode = trim($MessageText); + if (strlen($BindCode) == 16) { + $Uid = TelegramSessionManager::verify_bind_session($BindCode); + if ($Uid == 0) { + $text = '绑定失败了呢,经检查发现:【' . $BindCode . '】的有效期为 10 分钟,您可以在我们网站上的 **资料编辑** 页面刷新后重试.'; + } else { + $BinsUser = User::where('id', $Uid)->first(); + $BinsUser->telegram_id = $SendUser['id']; + $BinsUser->im_type = 4; + $BinsUser->im_value = $SendUser['username']; + $BinsUser->save(); + if ($BinsUser->is_admin >= 1) { + $text = '尊敬的**管理员**您好,恭喜绑定成功。' . PHP_EOL .'当前绑定邮箱为:' . $BinsUser->email; + } else { + if ($BinsUser->class >= 1) { + $text = '尊敬的 **VIP ' . $BinsUser->class . '** 用户您好.' . PHP_EOL . '恭喜您绑定成功,当前绑定邮箱为:' . $BinsUser->email; + } else { + $text = '绑定成功了,您的邮箱为:' . $BinsUser->email; + } + } + } + } + // 回送信息 + $this->replyWithMessage( + [ + 'text' => $text, + 'parse_mode' => 'Markdown', + ] + ); + } + // 触发 /help + $this->triggerCommand('help'); + } else { + // 群组 + + if (Config::get('new_telegram_group_quiet') === true) { + // 群组中不回应 + return; + } + + // 发送 '输入中' 会话状态 + $this->replyWithChatAction(['action' => Actions::TYPING]); + // 回送信息 + $this->replyWithMessage( + [ + 'text' => '喵喵喵.', + ] + ); + } + } +} diff --git a/app/Utils/Telegram/Process.php b/app/Utils/Telegram/Process.php new file mode 100644 index 00000000..a0f9523a --- /dev/null +++ b/app/Utils/Telegram/Process.php @@ -0,0 +1,37 @@ +addCommands( + [ + Commands\HelpCommand::class, + Commands\StartCommand::class, + ] + ); + $update = $bot->commandsHandler(true); + + if ($update->getCallbackQuery() !== null) { + $id = $update->getCallbackQuery()->getFrom()->getId(); + $user = User::where('telegram_id', $id)->first(); + Callback::CallbackQueryMethod($user, $bot, $update->getCallbackQuery()); + } + + // $Message = $update->getMessage(); + // $MessageData = $Message->getText(); + + } catch (Exception $e) { + $e->getMessage(); + } + } +} diff --git a/composer.json b/composer.json index 9f2602c5..02da0375 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "lokielse/omnipay-alipay": "*", "khanamiryan/qrcode-detector-decoder": "*", "remotelyliving/php-dns": "3.0.6", - "symfony/yaml": "^4.4@dev" + "symfony/yaml": "^4.4@dev", + "irazasyed/telegram-bot-sdk": "^2.0" }, "autoload": { "psr-4": { diff --git a/config/.config.example.php b/config/.config.example.php index 6763e089..84ddb340 100644 --- a/config/.config.example.php +++ b/config/.config.example.php @@ -150,6 +150,14 @@ $_ENV['telegram_bot'] = '_bot'; //Telegram 机器人账号 $_ENV['telegram_group_quiet'] = true; //Telegram 机器人在群组中不回应 $_ENV['telegram_request_token'] = ''; //Telegram 机器人请求Key,随意设置,由大小写英文和数字组成,更新这个参数之后请 php xcat setTelegram +# 新的 Telegram BOT +$_ENV['new_telegram_enable'] = false; //是否开启Telegram Bot +$_ENV['new_telegram_token'] = ''; //Telegram Bot 的 Token,跟 Father Bot 申请 +$_ENV['new_telegram_username'] = ''; //Telegram Bot 用户名 +$_ENV['new_telegram_group_quiet'] = true; //Telegram Bot 在群组中不回应 +$_ENV['new_telegram_group_chatid'] = ''; //Telegram 群组会话 ID,把 Bot 拉进群里之后跟他 /ping 一下即可得到。 +$_ENV['new_telegram_request_token'] = ''; //Telegram 机器人请求验证 Key,随意设置,由大小写英文和数字组成,更新这个参数之后请 php xcat setNewTelegram + //沟通设置-------------------------------------------------------------------------------------------- #客服系统设置,注册地址 https://www.mylivechat.com diff --git a/config/routes.php b/config/routes.php index 1b32474a..46b0fad4 100644 --- a/config/routes.php +++ b/config/routes.php @@ -63,6 +63,9 @@ $app->post('/telegram_callback', App\Controllers\HomeController::class . ':teleg $app->post('/tomato_back/{type}', 'App\Services\Payment:notify'); $app->get('/tomato_back/{type}', 'App\Services\Payment:notify'); +// New Telegram +$app->post('/TelegramCallback', App\Controllers\HomeController::class . ':NewTelegram'); + // User Center $app->group('/user', function () { $this->get('', App\Controllers\UserController::class . ':index');