'use strict'; const generateId = require('../../utils/generateId.util'); const { sessionService } = require('../../core'); const { upsertOutboundMessage } = require('../../services/outbound_messages.service'); function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } exports.sendText = async ctx => { const { from, to, msgcontent, content: _content, actionId } = ctx.request.body; const content = _content || msgcontent.body || ''; if (!from || !content) { ctx.assert(from, 400, 'From and message are required'); return; } const wsToSend = sessionService.getSession(from); // console.log('find wsToSend', wsToSend) if (!wsToSend) { ctx.assert(wsToSend, 400, 'Session not found'); // 404 return; } // return wsToSend; try { wsToSend.sendTextMessage(to, content, actionId).then(sockMsg => { const { key } = sockMsg; upsertOutboundMessage(null, { ...ctx.request.body, id: key.id || '' }); }); // const sockMsg = await wsToSend.sendTextMessage(to, content, actionId); return 'Message sent successfully'; // { wsToSend, ret: 'Message sent successfully' }; } catch (error) { console.error('Error sending message:', error); ctx.assert(null, 500, 'Failed to send message'); } };