|
|
|
@ -3,7 +3,8 @@
|
|
|
|
|
const generateId = require('../../utils/generateId.util');
|
|
|
|
|
const plannerEvents = require('../emitter');
|
|
|
|
|
// const createAsyncQueueProcessor = require('../emitter/queueProcessor');
|
|
|
|
|
const { getAgentSession, createAgentSession } = require('../../services/agent_sessions.service');
|
|
|
|
|
const { getConnection } = require('../../services/connections.service')
|
|
|
|
|
const { getAgentSession, createAgentSession, update: updateAgentSession } = require('../../services/agent_sessions.service');
|
|
|
|
|
// const { getOutboundMessage, upsertOutboundMessage } = require('../../services/outbound_messages.service');
|
|
|
|
|
const { isEmpty } = require('../../utils/commons.util');
|
|
|
|
|
const { logger, getUserLogger } = require('../../utils/logger.util');
|
|
|
|
@ -25,10 +26,13 @@ const setupUserMessageHandler = async () => {
|
|
|
|
|
const { from, to, direction } = msgRow;
|
|
|
|
|
const _whatsAppNo = to;
|
|
|
|
|
const _contact = from;
|
|
|
|
|
if (!AGENT_PHONE_NO.includes(_whatsAppNo) || direction !== 'inbound') {
|
|
|
|
|
getUserLogger(_whatsAppNo).info({ eventName, msgRow });
|
|
|
|
|
|
|
|
|
|
const hostedUser = await getConnection({ wa_id: _whatsAppNo, status: 'hosted' });
|
|
|
|
|
if ((!AGENT_PHONE_NO.includes(_whatsAppNo) && isEmpty(hostedUser)) || direction !== 'inbound') {
|
|
|
|
|
// logger.info({ eventName, msgRow }, 'ignore message');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
getUserLogger(_whatsAppNo).info({ eventName, msgRow });
|
|
|
|
|
try {
|
|
|
|
|
const agentSession = await getAgentSession({ hosted: _whatsAppNo, contact: _contact });
|
|
|
|
|
|
|
|
|
@ -79,6 +83,22 @@ const setupAgentMessageHandler = async () => {
|
|
|
|
|
agent_name: agentRes.agent.name,
|
|
|
|
|
model_id: agentRes.usage.models[0].model_id,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// 更新
|
|
|
|
|
await updateAgentSession(
|
|
|
|
|
{
|
|
|
|
|
agent_session_id: agentSessionId,
|
|
|
|
|
hosted: _whatsAppNo,
|
|
|
|
|
contact: _contact,
|
|
|
|
|
agent_id: agentRes.agent.id,
|
|
|
|
|
agent_name: agentRes.agent.name,
|
|
|
|
|
model_id: agentRes.usage.models[0].model_id,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
hosted: _whatsAppNo,
|
|
|
|
|
contact: _contact,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// agent -> contact
|
|
|
|
|
const msgReady = { from: _whatsAppNo, to: _contact, msgcontent: { body: agentMsg.text }, msgtype: 'text', actionId: `.${generateId()}.${agentSessionId}` };
|
|
|
|
|