明树Git Lab

Commit 8e0e93bc authored by zfp1's avatar zfp1

update

parent 12931480
const express = require('express'); const express = require('express');
const axios = require('axios'); const axios = require('axios');
const xml2js = require('xml2js'); const xml2js = require('xml2js');
const bodyParser = require('body-parser');
const crypto = require('crypto'); const crypto = require('crypto');
const app = express(); const app = express();
const port = 3001; const port = 3000;
// 微信服务号配置 // 微信服务号配置
const WECHAT_TOKEN = 'deepseektest'; const WECHAT_TOKEN = 'deepseektest';
const DEEPSEEK_API_URL = 'https://api.deepseek.com/v1/ask'; // 请替换为 DeepSeek 的具体 API 地址 const DEEPSEEK_API_URL = 'https://api.deepseek.com/v1/ask'; // 请替换为 DeepSeek 的具体 API 地址
const DEEPSEEK_API_KEY = 'sk-14f0cf16bad245169cd2563e0f9b678e'; // 请替换为你的 DeepSeek API 密钥 const DEEPSEEK_API_KEY = 'your_deepseek_api_key'; // 请替换为你的 DeepSeek API 密钥
// 最大消息长度(微信最大支持2048字节) // 最大消息长度(微信最大支持2048字节)
const MAX_TEXT_LENGTH = 2048; const MAX_TEXT_LENGTH = 2048;
// 解析 XML 数据
app.use(bodyParser.xml({
explicitArray: false,
ignoreAttrs: true
}));
// 微信验证 // 微信验证
app.get('/wechat', (req, res) => { app.get('/wechat', (req, res) => {
const { signature, timestamp, nonce, echostr } = req.query; const { signature, timestamp, nonce, echostr } = req.query;
...@@ -52,17 +45,20 @@ function escapeXml(str) { ...@@ -52,17 +45,20 @@ function escapeXml(str) {
} }
// 处理微信消息 // 处理微信消息
app.post('/wechat', async (req, res) => { app.post('/wechat', express.raw({ type: 'application/xml' }), async (req, res) => {
const { xml } = req.body; const parser = new xml2js.Parser({ explicitArray: false, ignoreAttrs: true });
try {
// 解析微信发来的 XML 数据
const xmlData = await parser.parseStringPromise(req.body);
// 获取用户提问的内容 // 获取用户提问的内容
const userMessage = xml.Content; const userMessage = xmlData.xml.Content;
if (!userMessage) { if (!userMessage) {
return res.send('No content'); return res.send('No content');
} }
try {
// 调用 DeepSeek API 获取答案 // 调用 DeepSeek API 获取答案
const deepSeekResponse = await axios.post(DEEPSEEK_API_URL, { const deepSeekResponse = await axios.post(DEEPSEEK_API_URL, {
apiKey: DEEPSEEK_API_KEY, apiKey: DEEPSEEK_API_KEY,
...@@ -83,8 +79,8 @@ app.post('/wechat', async (req, res) => { ...@@ -83,8 +79,8 @@ app.post('/wechat', async (req, res) => {
// 构建微信返回消息格式 // 构建微信返回消息格式
const responseMessage = ` const responseMessage = `
<xml> <xml>
<ToUserName><![CDATA[${xml.FromUserName}]]></ToUserName> <ToUserName><![CDATA[${xmlData.xml.FromUserName}]]></ToUserName>
<FromUserName><![CDATA[${xml.ToUserName}]]></FromUserName> <FromUserName><![CDATA[${xmlData.xml.ToUserName}]]></FromUserName>
<CreateTime>${Date.now()}</CreateTime> <CreateTime>${Date.now()}</CreateTime>
<MsgType><![CDATA[text]]></MsgType> <MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[${answer}]]></Content> <Content><![CDATA[${answer}]]></Content>
...@@ -98,8 +94,8 @@ app.post('/wechat', async (req, res) => { ...@@ -98,8 +94,8 @@ app.post('/wechat', async (req, res) => {
console.error('Error calling DeepSeek API:', error); console.error('Error calling DeepSeek API:', error);
const errorMessage = ` const errorMessage = `
<xml> <xml>
<ToUserName><![CDATA[${xml.FromUserName}]]></ToUserName> <ToUserName><![CDATA[${xmlData.xml.FromUserName}]]></ToUserName>
<FromUserName><![CDATA[${xml.ToUserName}]]></FromUserName> <FromUserName><![CDATA[${xmlData.xml.ToUserName}]]></FromUserName>
<CreateTime>${Date.now()}</CreateTime> <CreateTime>${Date.now()}</CreateTime>
<MsgType><![CDATA[text]]></MsgType> <MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[Sorry, I couldn't fetch the answer right now. Please try again later.]]></Content> <Content><![CDATA[Sorry, I couldn't fetch the answer right now. Please try again later.]]></Content>
......
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