'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 } = 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 { const sockMsg = wsToSend.sendTextMessage(to, content); console.log(JSON.stringify(sockMsg, undefined, 2)); await upsertOutboundMessage(null, { ...ctx.request.body, id: sockMsg.key || sockMsg.id }); 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'); } };