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

94 lines
3.9 KiB
JavaScript

'use strict';
const generateId = require('../../utils/generateId.util');
const { sessionStore } = require('../../core');
const { objectMapper, pick } = require('../../utils/commons.util');
const { createOutboundMessage } = require('../../services/outbound_messages.service');
const waEmitter = require('../../core/emitter');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* @deprecated 即将废弃
*/
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;
}
const wsToSend = sessionStore.getSession(from);
6 months ago
// console.log('find wsToSend', wsToSend)
if (!wsToSend) {
ctx.assert(wsToSend, 400, 'Session not found'); // 404
return;
}
try {
// wsToSend.sendTextMessage(to, content, actionId).then(({ messageId }) => {
// const messageId = generateId();
const _data = ctx.request.body;
const defaultR = { direction: 'outbound' };
const r1 = pick(_data, ['actionId', 'msgtype', 'externalId']);
r1.id = actionId; // id not null
// r1.wamid = messageId;
r1.msg_status = 'ready';
r1.createTime = Date.now();
const record = objectMapper(_data, { from: 'from', to: 'to' }, false);
const byType = _data.msgtype === 'text' ? { text_body: _data.msgcontent.body, text_preview_url: _data.msgcontent.preview_url } : {};
const toUpsert = { ...defaultR, ...r1, ...record, ...byType, message_origin: JSON.stringify(_data) };
await createOutboundMessage({ ...toUpsert });
// return 'Message sent successfully'; // { wsToSend, ret: 'Message sent successfully' };
// });
// wsToSend.sendTextMessage(to, content, actionId);
waEmitter.emit('request.' + from + '.send.text', { to, content, externalId: 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');
}
};
exports.send = async ctx => {
const { msgtype, 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 = sessionStore.getSession(from);
// console.log('find wsToSend', wsToSend)
if (!wsToSend) {
ctx.assert(wsToSend, 400, '未登录WhatsApp, 请到个人资料页扫码登录'); // 404
return;
}
try {
// wsToSend.sendTextMessage(to, content, actionId).then(({ messageId }) => {
// const messageId = generateId();
const _data = ctx.request.body;
const defaultR = { direction: 'outbound' };
const r1 = pick(_data, ['actionId', 'msgtype', 'externalId']);
r1.id = actionId; // id not null
// r1.wamid = messageId;
r1.msg_status = 'ready';
r1.createTime = Date.now();
const record = objectMapper(_data, { from: 'from', to: 'to' }, false);
const byType = _data.msgtype === 'text' ? { text_body: _data.msgcontent.body, text_preview_url: _data.msgcontent.preview_url } : {};
const toUpsert = { ...defaultR, ...r1, ...record, ...byType, message_origin: JSON.stringify(_data) };
await createOutboundMessage({ ...toUpsert });
// return 'Message sent successfully'; // { wsToSend, ret: 'Message sent successfully' };
// });
// wsToSend.sendTextMessage(to, content, actionId);
// todo: build msgtype content to wa
waEmitter.emit('request.' + from + '.send.' + msgtype, { to, content, externalId: 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');
}
};