new(basic.gblib): New batch features.
This commit is contained in:
parent
14b3ff1fe1
commit
ccc8fdd2d7
1 changed files with 69 additions and 71 deletions
|
@ -732,96 +732,94 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendToDevice(to: any, msg: string, conversationId) {
|
public async sendToDevice(to: any, msg: string, conversationId) {
|
||||||
try{
|
try {
|
||||||
const cmd = '/audio ';
|
const cmd = '/audio ';
|
||||||
let url;
|
let url;
|
||||||
let chatId = WhatsappDirectLine.chatIds[conversationId];
|
let chatId = WhatsappDirectLine.chatIds[conversationId];
|
||||||
|
|
||||||
if (typeof msg !== 'object' && msg.startsWith(cmd)) {
|
if (typeof msg !== 'object' && msg.startsWith(cmd)) {
|
||||||
msg = msg.substr(cmd.length);
|
msg = msg.substr(cmd.length);
|
||||||
|
|
||||||
return await this.sendTextAsAudioToDevice(to, msg, chatId);
|
return await this.sendTextAsAudioToDevice(to, msg, chatId);
|
||||||
} else {
|
} else {
|
||||||
let options, messages;
|
let options, messages;
|
||||||
const botNumber = this.min.core.getParam(this.min.instance, 'Bot Number', null);
|
const botNumber = this.min.core.getParam(this.min.instance, 'Bot Number', null);
|
||||||
|
|
||||||
switch (this.provider) {
|
switch (this.provider) {
|
||||||
case 'meta':
|
case 'meta':
|
||||||
let whatsappServiceNumber, whatsappServiceKey;
|
let whatsappServiceNumber, whatsappServiceKey;
|
||||||
if (botNumber && this.min.instance.whatsappServiceNumber) {
|
if (botNumber && this.min.instance.whatsappServiceNumber) {
|
||||||
whatsappServiceNumber = this.min.instance.whatsappServiceNumber;
|
whatsappServiceNumber = this.min.instance.whatsappServiceNumber;
|
||||||
whatsappServiceKey = this.min.instance.whatsappServiceKey;
|
whatsappServiceKey = this.min.instance.whatsappServiceKey;
|
||||||
} else {
|
} else {
|
||||||
whatsappServiceNumber = GBServer.globals.minBoot.instance.whatsappServiceNumber;
|
whatsappServiceNumber = GBServer.globals.minBoot.instance.whatsappServiceNumber;
|
||||||
whatsappServiceKey = GBServer.globals.minBoot.instance.whatsappServiceKey;
|
whatsappServiceKey = GBServer.globals.minBoot.instance.whatsappServiceKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
const driver = createBot(whatsappServiceNumber, whatsappServiceKey);
|
const driver = createBot(whatsappServiceNumber, whatsappServiceKey);
|
||||||
|
|
||||||
if (msg['name']) {
|
if (msg['name']) {
|
||||||
const res = await driver.sendTemplate(to, msg['name'], 'pt_br', msg['components']);
|
const res = await driver.sendTemplate(to, msg['name'], 'pt_br', msg['components']);
|
||||||
} else {
|
} else {
|
||||||
messages = msg.match(/(.|[\r\n]){1,4096}/g);
|
messages = msg.match(/(.|[\r\n]){1,4096}/g);
|
||||||
|
|
||||||
|
await CollectionUtil.asyncForEach(messages, async msg => {
|
||||||
|
await driver.sendText(to, msg);
|
||||||
|
|
||||||
|
await GBUtil.sleep(3000);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 'official':
|
||||||
|
if (to.charAt(0) !== '+') {
|
||||||
|
to = `+${to}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
messages = msg.match(/(.|[\r\n]){1,1000}/g);
|
||||||
|
|
||||||
await CollectionUtil.asyncForEach(messages, async msg => {
|
await CollectionUtil.asyncForEach(messages, async msg => {
|
||||||
await driver.sendText(to, msg);
|
|
||||||
|
|
||||||
await GBUtil.sleep(3000);
|
await GBUtil.sleep(3000);
|
||||||
|
await this.customClient.messages.create({
|
||||||
|
body: msg,
|
||||||
|
from: `whatsapp:${botNumber}`,
|
||||||
|
to: `whatsapp:${to}`
|
||||||
|
// TODO: mediaUrl.
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'official':
|
|
||||||
if (to.charAt(0) !== '+') {
|
|
||||||
to = `+${to}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
messages = msg.match(/(.|[\r\n]){1,1000}/g);
|
case 'GeneralBots':
|
||||||
|
to = to.replace('+', '');
|
||||||
await CollectionUtil.asyncForEach(messages, async msg => {
|
if (to.indexOf('@') == -1) {
|
||||||
await GBUtil.sleep(3000);
|
if (to.length == 18) {
|
||||||
await this.customClient.messages.create({
|
to = to + '@g.us';
|
||||||
body: msg,
|
} else {
|
||||||
from: `whatsapp:${botNumber}`,
|
to = to + '@c.us';
|
||||||
to: `whatsapp:${to}`
|
}
|
||||||
// TODO: mediaUrl.
|
}
|
||||||
});
|
if ((await this.customClient.getState()) === WAState.CONNECTED) {
|
||||||
});
|
await this.customClient.sendMessage(to, msg);
|
||||||
|
} else {
|
||||||
break;
|
GBLogEx.info(this.min, `WhatsApp OFFLINE ${to}: ${msg}`);
|
||||||
|
|
||||||
case 'GeneralBots':
|
|
||||||
to = to.replace('+', '');
|
|
||||||
if (to.indexOf('@') == -1) {
|
|
||||||
if (to.length == 18) {
|
|
||||||
to = to + '@g.us';
|
|
||||||
} else {
|
|
||||||
to = to + '@c.us';
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if ((await this.customClient.getState()) === WAState.CONNECTED) {
|
|
||||||
await this.customClient.sendMessage(to, msg);
|
|
||||||
} else {
|
|
||||||
GBLogEx.info(this.min, `WhatsApp OFFLINE ${to}: ${msg}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options) {
|
if (options) {
|
||||||
try {
|
try {
|
||||||
GBLogEx.info(this.min, `Message [${msg}] is being sent to ${to}...`);
|
GBLogEx.info(this.min, `Message [${msg}] is being sent to ${to}...`);
|
||||||
await fetch(url, options);
|
await fetch(url, options);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
GBLog.error(`Error sending message to Whatsapp provider ${JSON.stringify(error)}`);
|
GBLog.error(`Error sending message to Whatsapp provider ${JSON.stringify(error)}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
try {
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
GBLog.error(`GBWhatsApp ERR: ${GBUtil.toYAML(error)}`);
|
GBLog.error(`GBWhatsApp ERR: ${GBUtil.toYAML(error)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendToDeviceEx(to, text, locale, conversationId) {
|
public async sendToDeviceEx(to, text, locale, conversationId) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue