明树Git Lab
Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
D
deepseek_wechat_chat
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zengfanpei
deepseek_wechat_chat
Commits
063cafe5
Commit
063cafe5
authored
Feb 18, 2025
by
zfp1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
524c55bd
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
7 deletions
+6
-7
index4.js
index4.js
+6
-7
No files found.
index4.js
View file @
063cafe5
...
@@ -10,8 +10,8 @@ const port = 3001;
...
@@ -10,8 +10,8 @@ const port = 3001;
// 微信服务号配置
// 微信服务号配置
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'
;
// 请替换为 DeepSeek 的具体 API 地址
const
DEEPSEEK_API_KEY
=
'
your_deepseek_api_key
'
;
// 请替换为你的 DeepSeek API 密钥
const
DEEPSEEK_API_KEY
=
'
sk-14f0cf16bad245169cd2563e0f9b678e
'
;
// 请替换为你的 DeepSeek API 密钥
// 最大消息长度(微信最大支持2048字节)
// 最大消息长度(微信最大支持2048字节)
const
MAX_TEXT_LENGTH
=
2048
;
const
MAX_TEXT_LENGTH
=
2048
;
...
@@ -49,15 +49,14 @@ function escapeXml(str) {
...
@@ -49,15 +49,14 @@ function escapeXml(str) {
// 处理微信消息
// 处理微信消息
app
.
post
(
'/wechat'
,
async
(
req
,
res
)
=>
{
app
.
post
(
'/wechat'
,
async
(
req
,
res
)
=>
{
const
parser
=
new
xml2js
.
Parser
({
explicitArray
:
false
,
ignoreAttrs
:
true
});
try
{
try
{
// 解析微信发来的 XML 数据
// 解析微信发来的 XML 数据
const
xmlData
=
await
parser
.
parseStringPromise
(
req
.
body
);
const
xmlData
=
await
xml2js
.
parseStringPromise
(
req
.
body
);
// 获取用户提问的内容
// 获取用户提问的内容
const
userMessage
=
xmlData
.
xml
.
Content
;
const
userMessage
=
xmlData
.
xml
.
Content
;
console
.
log
(
userMessage
,
"==============="
)
if
(
!
userMessage
)
{
if
(
!
userMessage
)
{
return
res
.
send
(
'No content'
);
return
res
.
send
(
'No content'
);
}
}
...
@@ -67,9 +66,9 @@ app.post('/wechat', async (req, res) => {
...
@@ -67,9 +66,9 @@ app.post('/wechat', async (req, res) => {
apiKey
:
DEEPSEEK_API_KEY
,
apiKey
:
DEEPSEEK_API_KEY
,
question
:
userMessage
,
question
:
userMessage
,
});
});
console
.
log
(
deepSeekResponse
,
"============"
)
// 获取 DeepSeek API 返回的答案
// 获取 DeepSeek API 返回的答案
let
answer
=
deepSeekResponse
.
data
.
answer
||
'Sorry, I could not find an answer.'
;
let
answer
=
deepSeekResponse
.
choices
[
0
].
message
.
content
||
'Sorry, I could not find an answer.'
;
// 如果回答超长,截取并添加提示
// 如果回答超长,截取并添加提示
if
(
answer
.
length
>
MAX_TEXT_LENGTH
)
{
if
(
answer
.
length
>
MAX_TEXT_LENGTH
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment