明树Git Lab

Commit 58176b5f authored by zfp1's avatar zfp1

update

parent 302a6035
...@@ -68,10 +68,20 @@ app.post('/wechat', async (req, res) => { ...@@ -68,10 +68,20 @@ app.post('/wechat', async (req, res) => {
// 5. 调用DeepSeek API // 5. 调用DeepSeek API
const deepseekResponse = await callDeepSeekAPI(message.content); const deepseekResponse = await callDeepSeekAPI(message.content);
if (deepseekResponse.length > 2040) {
deepseekResponse = deepseekResponse.substring(0, MAX_TEXT_LENGTH) + '... (Answer truncated)';
}
deepseekResponse= escapeXml(deepseekResponse);
// 6. 构建响应XML // 6. 构建响应XML
const responseXml = buildTextResponse(message, deepseekResponse); const responseXml =`
<xml>
<ToUserName><![CDATA[${xmlData.xml.FromUserName}]]></ToUserName>
<FromUserName><![CDATA[${xmlData.xml.ToUserName}]]></FromUserName>
<CreateTime>${Date.now()}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[${deepseekResponse}]]></Content>
</xml>
`;
res.set('Content-Type', 'text/xml'); res.set('Content-Type', 'text/xml');
console.log("responseXml:::" ,responseXml); console.log("responseXml:::" ,responseXml);
return res.send(responseXml); return res.send(responseXml);
...@@ -126,6 +136,19 @@ function buildTextResponse(message, content) { ...@@ -126,6 +136,19 @@ function buildTextResponse(message, content) {
}); });
} }
// 特殊字符转义(XML 转义)
function escapeXml(str) {
return str.replace(/[&<>'"]/g, function (char) {
switch (char) {
case '&': return '&amp;';
case '<': return '&lt;';
case '>': return '&gt;';
case "'": return '&apos;';
case '"': return '&quot;';
}
});
}
// 启动服务 // 启动服务
app.listen(port, () => { app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/wechat`); console.log(`Server running at http://localhost:${port}/wechat`);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment