明树Git Lab

Commit cd3a7b22 authored by zfp1's avatar zfp1

3

parent a57dfd19
......@@ -2,65 +2,69 @@ const express = require('express');
const crypto = require('crypto');
const { OpenAI } = require('openai');
const xml2js = require('xml2js');
const app = express();
app.use(express.text({ type: 'text/xml' }));
app.use(express.text({ type: 'text/xml' }));
// 配置参数 [5]()
const openai = new OpenAI({
baseURL: 'https://api.deepseek.com/v1',
baseURL: 'https://api.deepseek.com/v1',
apiKey: 'sk-14f0cf16bad245169cd2563e0f9b678e'
});
// 微信服务号验证
app.get('/wechat', (req, res) => {
const { signature, timestamp, nonce, echostr } = req.query;
app.get('/wechat', (req, res) => {
const { signature, timestamp, nonce, echostr } = req.query;
const token = 'deepseektest';
const sorted = [token, timestamp, nonce].sort().join('');
const sha1 = crypto.createHash('sha1').update(sorted).digest('hex');
sha1 === signature ? res.send(echostr) : res.status(403).send(' 验证失败');
const sha1 = crypto.createHash('sha1').update(sorted).digest('hex');
sha1 === signature ? res.send(echostr) : res.status(403).send(' 验证失败');
});
// 消息处理流程
app.post('/wechat', async (req, res) => {
app.post('/wechat', async (req, res) => {
try {
// XML解析
const result = await xml2js.parseStringPromise(req.body);
const message = result.xml.Content[0];
const fromUser = result.xml.FromUserName[0];
const result = await xml2js.parseStringPromise(req.body);
const message = result.xml.Content[0];
const fromUser = result.xml.FromUserName[0];
// API调用 [5]()
const completion = await openai.chat.completions.create({
const completion = await openai.chat.completions.create({
model: "deepseek-chat",
messages: [
{ role: "system", content: "你是一个微信服务号智能助手,限制你的字符输出要低于2048个字节。" },{
role: "user",
content: message
}],
stream: false
{ role: "system", content: "你是一个微信服务号智能助手, 限制你的字符输出要低于2048个字节。" }, {
role: "user",
content: message
}],
stream: true
});
let fullContent = '';
for await (const chunk of completion) {
fullContent += chunk.choices[0].message.content || '';
}
console.log("completion:::", completion);
console.log("completion:::", fullContent);
// 响应构造
const responseXML = `
<xml>
<ToUserName><![CDATA[${fromUser}]]></ToUserName>
<FromUserName><![CDATA[${result.xml.ToUserName[0]}]]></FromUserName>
<CreateTime>${Math.floor(Date.now()/1000)}</CreateTime>
<CreateTime>${Math.floor(Date.now() / 1000)}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[${completion.choices[0].message.content}]]></Content>
<Content><![CDATA[${fullContent}]]></Content>
</xml>
`;
res.set('Content-Type', 'text/xml');
res.set('Content-Type', 'text/xml');
console.log(responseXML)
res.send(responseXML);
res.send(responseXML);
} catch (error) {
console.error(error);
console.error(error);
res.status(500).send(' 服务异常');
}
});
app.listen(3001, () => console.log(' 服务运行在3001端口'));
\ No newline at end of file
app.listen(3001, () => console.log(' 服务运行在3001端口'));
\ No newline at end of file
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