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.
34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
'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.text || '';
|
|
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);
|
|
// await upsertOutboundMessage();
|
|
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');
|
|
}
|
|
};
|