From 705da5d4dba53da41c8cbf51095310f02875aeb0 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 23 Sep 2021 11:15:01 +0800 Subject: [PATCH] fix: special char of markdown --- functions/TgInterface.php | 41 ++++++++++++++++++++++----------------- main.php | 1 - 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/functions/TgInterface.php b/functions/TgInterface.php index d621578..1e47c5e 100644 --- a/functions/TgInterface.php +++ b/functions/TgInterface.php @@ -48,26 +48,31 @@ class tgApi { // Telegram消息发送接口 return file_get_contents($url); } + public function md_encode($str) { + return strtr($str, array( + '_' => '\_', + '*' => '\*', + '[' => '\[', + '`' => '\`', + )); + } + function debug() { // 调试接口 global $tgEnv; - $msg = '---- tgEnv Content ----' . PHP_EOL; - $msg .= 'myId: ' . $tgEnv['myInfo']['id'] . PHP_EOL; - $msg .= 'myName: ' . $tgEnv['myInfo']['name'] . PHP_EOL; - $msg .= 'myAccount: ' . $tgEnv['myInfo']['account'] . PHP_EOL; - $msg .= 'isCallback: ' . ($tgEnv['isCallback'] ? 'true' : 'false') . PHP_EOL; - $msg .= 'isGroup: ' . ($tgEnv['isGroup'] ? 'true' : 'false') . PHP_EOL; - $msg .= 'messageText: ' . $tgEnv['messageText'] . PHP_EOL; - $msg .= 'messageId: ' . $tgEnv['messageId'] . PHP_EOL; - $msg .= 'chatId: ' . $tgEnv['chatId'] . PHP_EOL; - $msg .= 'userId: ' . $tgEnv['userId'] . PHP_EOL; - $msg .= 'userName: ' . $tgEnv['userName'] . PHP_EOL; - $msg .= 'userAccount: ' . $tgEnv['userAccount'] . PHP_EOL; - $msg .= 'demo: ' . 'dnomd343' . PHP_EOL; - $msg .= 'userLanguage: ' . $tgEnv['userLanguage'] . PHP_EOL; - tgApi::sendMessage(array( - 'text' => $msg, - 'parse_mode' => 'HTML', // HTML格式输出 - )); + $msg = '_---- tgEnv Content ----_' . PHP_EOL; + $msg .= '*myId:* ' . $tgEnv['myInfo']['id'] . PHP_EOL; + $msg .= '*myName:* ' . tgApi::md_encode($tgEnv['myInfo']['name']) . PHP_EOL; + $msg .= '*myAccount:* ' . tgApi::md_encode($tgEnv['myInfo']['account']) . PHP_EOL; + $msg .= '*isCallback:* ' . ($tgEnv['isCallback'] ? 'true' : 'false') . PHP_EOL; + $msg .= '*isGroup:* ' . ($tgEnv['isGroup'] ? 'true' : 'false') . PHP_EOL; + $msg .= '*messageText:* ' . tgApi::md_encode($tgEnv['messageText']) . PHP_EOL; + $msg .= '*messageId:* ' . $tgEnv['messageId'] . PHP_EOL; + $msg .= '*chatId:* ' . $tgEnv['chatId'] . PHP_EOL; + $msg .= '*userId:* ' . $tgEnv['userId'] . PHP_EOL; + $msg .= '*userName:* ' . tgApi::md_encode($tgEnv['userName']) . PHP_EOL; + $msg .= '*userAccount:* ' . tgApi::md_encode($tgEnv['userAccount']) . PHP_EOL; + $msg .= '*userLanguage:* ' . tgApi::md_encode($tgEnv['userLanguage']) . PHP_EOL; + tgApi::sendMarkdown($msg); } } diff --git a/main.php b/main.php index c65419e..8bac786 100644 --- a/main.php +++ b/main.php @@ -16,7 +16,6 @@ $env = loadEnv('.env'); // 载入环境变量 fastcgi_finish_request(); // 断开连接 ini_set('date.timezone', $env['TIME_ZONE']); // 设置时区 $tgEnv = initBot(file_get_contents("php://input")); // 初始化bot配置 -tgApi::debug(); route($tgEnv['messageText']); // 发往请求路由 ?>