Files
jjx-web-sign-only/uniCloud-tcb/cloudfunctions/router/service/client/ai/pub/getReviewResult.js
T

48 lines
1.6 KiB
JavaScript

module.exports = {
main: async (event) => {
let { data = {} } = event;
const COZE_TOKEN = 'sat_OiXlI4Da5Q99fxmul6pNfExmA6hR898UbxBDwrvjZ638APxRSlFnXbz6zyFedKNG';
const { chat_id, conversation_id } = data;
if (!chat_id || !conversation_id) return { code: -1, msg: '参数不完整' };
// 查询对话状态
const statusRes = await uniCloud.httpclient.request(
'https://api.coze.cn/v3/chat?chat_id=' + chat_id + '&conversation_id=' + conversation_id + '&bot_id=7689821072762011698',
{ method: 'GET', dataType: 'text', headers: { 'Authorization': 'Bearer ' + COZE_TOKEN }, timeout: 15000 }
);
let status = 'unknown';
try {
const parsed = JSON.parse(statusRes.data);
status = parsed.data && parsed.data.status;
} catch (e) {}
if (status !== 'completed') {
return { code: 0, data: { status } };
}
// 获取消息
const msgRes = await uniCloud.httpclient.request(
'https://api.coze.cn/v3/chat/message/list?chat_id=' + chat_id + '&conversation_id=' + conversation_id,
{ method: 'GET', dataType: 'text', headers: { 'Authorization': 'Bearer ' + COZE_TOKEN }, timeout: 15000 }
);
let answer = '';
try {
const parsed = JSON.parse(msgRes.data);
const messages = parsed.data;
if (Array.isArray(messages)) {
for (const m of messages) {
if (m.type === 'answer' && m.role === 'assistant') {
answer = m.content;
break;
}
}
}
} catch (e) {}
return { code: 0, data: { status: 'completed', answer } };
}
};