|
|
@ -18,27 +18,12 @@ const generateId = require('../../utils/generateId.util');
|
|
|
|
const NodeCache = require('node-cache');
|
|
|
|
const NodeCache = require('node-cache');
|
|
|
|
const P = require('pino');
|
|
|
|
const P = require('pino');
|
|
|
|
|
|
|
|
|
|
|
|
waEmitter.on('message:updated', event => {
|
|
|
|
|
|
|
|
console.info('msg:evt:updated', event);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
waEmitter.on('message:received', event => {
|
|
|
|
|
|
|
|
console.info('msg:evt:received', event);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
waEmitter.on('connection:open', event => {
|
|
|
|
|
|
|
|
console.info('con:evt.open', event);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
waEmitter.on('connection:close', event => {
|
|
|
|
|
|
|
|
console.info('con:evt.close', event);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const createWhatsApp = async phone => {
|
|
|
|
const createWhatsApp = async phone => {
|
|
|
|
// let waSocket = null;
|
|
|
|
|
|
|
|
let qrCode = null;
|
|
|
|
let qrCode = null;
|
|
|
|
let connectionStatus = 'offline'
|
|
|
|
|
|
|
|
const channelId = generateId();
|
|
|
|
const channelId = generateId();
|
|
|
|
const whatsAppNo = phone;
|
|
|
|
const whatsAppNo = phone;
|
|
|
|
// 储存键值对 msgId-externalId
|
|
|
|
// 储存键值对 msgId-externalId
|
|
|
|
// TODO 什么时候清理旧的?
|
|
|
|
// TODO 使用 NodeCache 储存,设置有效期。⬇️
|
|
|
|
const msgIdMap = new Map();
|
|
|
|
const msgIdMap = new Map();
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs-' + phone + '_' + channelId + '.txt'));
|
|
|
|
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs-' + phone + '_' + channelId + '.txt'));
|
|
|
|
logger.level = 'trace';
|
|
|
|
logger.level = 'trace';
|
|
|
@ -72,13 +57,10 @@ const createWhatsApp = async phone => {
|
|
|
|
const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid);
|
|
|
|
const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid);
|
|
|
|
|
|
|
|
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
|
|
|
|
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (text.indexOf('「自~测」') > -1) {
|
|
|
|
|
|
|
|
sendTextMessage(fromWhatsAppNo, '这是自测消息:' + new Date().toString());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text;
|
|
|
|
const externalId = msgIdMap.get(msg.key.id);
|
|
|
|
const externalId = msgIdMap.get(msg.key.id);
|
|
|
|
|
|
|
|
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
if (msg.key.fromMe) {
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
id: msg.key.id,
|
|
|
|
id: msg.key.id,
|
|
|
@ -296,9 +278,10 @@ const createWhatsApp = async phone => {
|
|
|
|
status: 'open', whatsAppNo, channelId,
|
|
|
|
status: 'open', whatsAppNo, channelId,
|
|
|
|
eventSource: serverConfig.name + '.connection.update.open',
|
|
|
|
eventSource: serverConfig.name + '.connection.update.open',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// 考虑迁移到 connection.update 事件之外,测试看是否能发送消息?
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.text', event => {
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.text', event => {
|
|
|
|
const {to: number, externalId, content} = event;
|
|
|
|
const {to: number, externalId, content} = event;
|
|
|
|
console.info('request.' + whatsAppNo + '.send.message:', event)
|
|
|
|
console.info('request.' + whatsAppNo + '.send.text:', event)
|
|
|
|
// const jid = formatPhoneNumber(event.to);
|
|
|
|
// const jid = formatPhoneNumber(event.to);
|
|
|
|
waSocket.sendMessage(
|
|
|
|
waSocket.sendMessage(
|
|
|
|
number + '@s.whatsapp.net', { text: content }
|
|
|
|
number + '@s.whatsapp.net', { text: content }
|
|
|
@ -318,11 +301,10 @@ const createWhatsApp = async phone => {
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
//sendTextMessage(number, content, externalId);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.image', event => {
|
|
|
|
waEmitter.on('request.' + whatsAppNo + '.send.image', event => {
|
|
|
|
const {to: number, externalId, content} = event;
|
|
|
|
const {to: number, externalId, imageUrl} = event;
|
|
|
|
const jid = formatPhoneNumber(event.to);
|
|
|
|
const jid = formatPhoneNumber(event.to);
|
|
|
|
waSocket.sendMessage(
|
|
|
|
waSocket.sendMessage(
|
|
|
|
jid, {image: { url: imageUrl }}
|
|
|
|
jid, {image: { url: imageUrl }}
|
|
|
@ -348,7 +330,6 @@ const createWhatsApp = async phone => {
|
|
|
|
// 第一次一分钟,后面是 20 秒更新一次
|
|
|
|
// 第一次一分钟,后面是 20 秒更新一次
|
|
|
|
if (qrCode === null) {
|
|
|
|
if (qrCode === null) {
|
|
|
|
qrCode = qr;
|
|
|
|
qrCode = qr;
|
|
|
|
console.info('qr: ', qr);
|
|
|
|
|
|
|
|
waEmitter.emit('creds:update', {
|
|
|
|
waEmitter.emit('creds:update', {
|
|
|
|
id: generateId(),
|
|
|
|
id: generateId(),
|
|
|
|
qr, whatsAppNo,
|
|
|
|
qr, whatsAppNo,
|
|
|
|