You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Global-sales/wai-server/api/messages/message.controller.js

37 lines
1.2 KiB
JavaScript

'use strict';
const generateId = require('../../utils/generateId.util');
6 months ago
const { sessionService } = require('../../core');
const { upsertOutboundMessage } = require('../../services/outbound_messages.service');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
6 months ago
exports.sendText = async ctx => {
const { from, to, msgcontent, content: _content, actionId } = ctx.request.body;
6 months ago
const content = _content || msgcontent.body || '';
if (!from || !content) {
ctx.assert(from, 400, 'From and message are required');
return;
}
6 months ago
const wsToSend = sessionService.getSession(from);
6 months ago
// 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);
6 months ago
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');
}
};