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

38 lines
1.0 KiB
JavaScript

'use strict';
const generateId = require('../../utils/generateId.util');
const { sessionService } = require('../../core');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
exports.sendText111 = async ctx => {
const { type } = ctx.params;
const body = ctx.request.body;
ctx.assert(null, 400, `debug: content: {${type}} {${body.content}}`);
ctx.assert(null, 400, 'The message info is malformed!');
return body;
};
exports.sendText = async ctx => {
const { from, to, content } = ctx.request.body;
if (!from || !content) {
ctx.assert(from, 400, 'From and message are required');
return;
}
const wsToSend = sessionService.getSession(from);
if (!wsToSend) {
ctx.assert(wsToSend, 400, 'Session not found'); // 404
return;
}
// return wsToSend;
try {
await wsToSend.sendTextMessage(to, content);
return { wsToSend, ret: 'Message sent successfully' };
} catch (error) {
console.error('Error sending message:', error);
ctx.assert(null, 500, 'Failed to send message');
}
};