new(WhatsApp.gblib): Official Twilio driver.
This commit is contained in:
parent
cae8408b59
commit
ac0b3f150e
5 changed files with 70 additions and 390 deletions
|
@ -708,7 +708,7 @@ ENDPOINT_UPDATE=true
|
||||||
value = null;
|
value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value ?? defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -258,6 +258,7 @@ export class GBMinService {
|
||||||
* installing all BASIC artifacts from .gbdialog and OAuth2.
|
* installing all BASIC artifacts from .gbdialog and OAuth2.
|
||||||
*/
|
*/
|
||||||
public async mountBot(instance: IGBInstance) {
|
public async mountBot(instance: IGBInstance) {
|
||||||
|
|
||||||
// Build bot adapter.
|
// Build bot adapter.
|
||||||
|
|
||||||
const { min, adapter, conversationState } = await this.buildBotAdapter(
|
const { min, adapter, conversationState } = await this.buildBotAdapter(
|
||||||
|
@ -439,16 +440,24 @@ export class GBMinService {
|
||||||
|
|
||||||
this.createCheckHealthAddress(GBServer.globals.server, min, min.instance);
|
this.createCheckHealthAddress(GBServer.globals.server, min, min.instance);
|
||||||
|
|
||||||
|
// Setups official handler for WhatsApp.
|
||||||
|
|
||||||
|
GBServer.globals.server.post(`/${min.instance.botId}/whatsapp`, async (req, res) => {
|
||||||
|
const to = req.body.To.replace(/whatsapp\:\+/gi, '');
|
||||||
|
const whatsAppDirectLine = WhatsappDirectLine.botsByNumber[to];
|
||||||
|
await whatsAppDirectLine.WhatsAppCallback(req, res, whatsAppDirectLine.botId);
|
||||||
|
}).bind(min);
|
||||||
|
|
||||||
GBDeployer.mountGBKBAssets(`${botId}.gbkb`, botId, `${botId}.gbkb`);
|
GBDeployer.mountGBKBAssets(`${botId}.gbkb`, botId, `${botId}.gbkb`);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static isChatAPI(req: any, res: any) {
|
public static getProviderName(req: any, res: any) {
|
||||||
if (!res) {
|
if (!res) {
|
||||||
return 'GeneralBots';
|
return 'GeneralBots';
|
||||||
}
|
}
|
||||||
if (req.NumMedia)
|
if (req.body?.AccountSid)
|
||||||
{
|
{
|
||||||
return 'Official';
|
return 'official';
|
||||||
}
|
}
|
||||||
return req.body.phone_id ? 'maytapi' : 'chatapi';
|
return req.body.phone_id ? 'maytapi' : 'chatapi';
|
||||||
}
|
}
|
||||||
|
@ -833,7 +842,8 @@ export class GBMinService {
|
||||||
|
|
||||||
await min.whatsAppDirectLine.setup(true);
|
await min.whatsAppDirectLine.setup(true);
|
||||||
} else {
|
} else {
|
||||||
if (min !== minBoot && minBoot.instance.whatsappServiceKey) {
|
if (min !== minBoot && minBoot.instance.whatsappServiceKey
|
||||||
|
&& min.instance.webchatKey) {
|
||||||
min.whatsAppDirectLine = new WhatsappDirectLine(
|
min.whatsAppDirectLine = new WhatsappDirectLine(
|
||||||
min,
|
min,
|
||||||
min.botId,
|
min.botId,
|
||||||
|
|
|
@ -81,6 +81,8 @@ export class ChatServices {
|
||||||
subjects: GuaribasSubject[]
|
subjects: GuaribasSubject[]
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
return { answer: undefined, questionId: 0 };
|
||||||
|
|
||||||
if (!process.env.OPENAI_API_KEY) {
|
if (!process.env.OPENAI_API_KEY) {
|
||||||
return { answer: undefined, questionId: 0 };
|
return { answer: undefined, questionId: 0 };
|
||||||
}
|
}
|
||||||
|
|
|
@ -228,112 +228,27 @@ export class AskDialog extends IGBDialog {
|
||||||
min.instance.searchScore ? min.instance.searchScore : minBoot.instance.searchScore
|
min.instance.searchScore ? min.instance.searchScore : minBoot.instance.searchScore
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: https://github.com/GeneralBots/BotServer/issues/9 user.lastQuestion = text;
|
const results = await service.ask(min,step.context.activity['pid'], text, searchScore, null /* user.subjects */);
|
||||||
|
|
||||||
const resultsA = await service.ask(min,step.context.activity['pid'], text, searchScore, null /* user.subjects */);
|
|
||||||
|
|
||||||
// If there is some result, answer immediately.
|
// If there is some result, answer immediately.
|
||||||
|
|
||||||
if (resultsA !== undefined && resultsA.answer !== undefined) {
|
if (results !== undefined && results.answer !== undefined) {
|
||||||
// Saves some context info.
|
|
||||||
|
|
||||||
// user.isAsking = false;
|
|
||||||
// user.lastQuestionId = resultsA.questionId;
|
|
||||||
|
|
||||||
// Sends the answer to all outputs, including projector.
|
// Sends the answer to all outputs, including projector.
|
||||||
|
|
||||||
answer = resultsA.answer;
|
answer = results.answer;
|
||||||
|
|
||||||
// If this search was restricted to some subjects...
|
|
||||||
}
|
|
||||||
// TODO: https://github.com/GeneralBots/BotServer/issues/9
|
|
||||||
// else if (user.subjects && user.subjects.length > 0) {
|
|
||||||
// // ..second time running Search, now with no filter.
|
|
||||||
|
|
||||||
// const resultsB = await service.ask(min.instance, text, searchScore, undefined);
|
|
||||||
|
|
||||||
// // If there is some result, answer immediately.
|
|
||||||
|
|
||||||
// if (resultsB !== undefined && resultsB.answer !== undefined) {
|
|
||||||
// // Saves some context info.
|
|
||||||
|
|
||||||
// // user2.isAsking = false;
|
|
||||||
// // user2.lastQuestionId = resultsB.questionId;
|
|
||||||
|
|
||||||
// // Informs user that a broader search will be used.
|
|
||||||
|
|
||||||
// if (user2.subjects.length > 0) {
|
|
||||||
// await min.conversationalService.sendText(min, step, Messages[locale].wider_answer);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// answer = resultsB.answer;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Try to answer by search.
|
|
||||||
|
|
||||||
if (answer) {
|
|
||||||
return await AskDialog.handleAnswer(service, min, step, user, answer);
|
return await AskDialog.handleAnswer(service, min, step, user, answer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tries to answer by NLP.
|
|
||||||
|
|
||||||
let handled = await min.conversationalService.routeNLP(step, min, text);
|
|
||||||
if (handled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tries to answer by Reading Comprehension.
|
|
||||||
|
|
||||||
if (process.env.GBMODELS_SERVER) {
|
|
||||||
text = await min.conversationalService.translate(min, text, 'en');
|
|
||||||
let answered = false;
|
|
||||||
|
|
||||||
const docs = await min.kbService['getDocs'](min.instance.instanceId);
|
|
||||||
|
|
||||||
await CollectionUtil.asyncForEach(docs, async (doc: GuaribasAnswer) => {
|
|
||||||
if (!answered) {
|
|
||||||
const answerText = await min.kbService['readComprehension'](min.instance.instanceId, doc.content, text);
|
|
||||||
answered = true;
|
|
||||||
text = await min.conversationalService.translate(
|
|
||||||
min,
|
|
||||||
text,
|
|
||||||
user.locale
|
|
||||||
? user.locale
|
|
||||||
: min.core.getParam<string>(min.instance, 'Locale', GBConfigService.get('LOCALE'))
|
|
||||||
);
|
|
||||||
await min.conversationalService.sendText(min, step, answerText);
|
|
||||||
await min.conversationalService.sendEvent(min, step, 'stop', undefined);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return await step.replaceDialog('/ask', { isReturning: true });
|
|
||||||
}
|
|
||||||
if (process.env.OPENAI_EMAIL) {
|
|
||||||
if (!GBServer.globals.chatGPT) {
|
|
||||||
GBServer.globals.chatGPT = new ChatGPTAPIBrowser({
|
|
||||||
email: process.env.OPENAI_EMAIL,
|
|
||||||
password: process.env.OPENAI_PASSWORD
|
|
||||||
});
|
|
||||||
await GBServer.globals.chatGPT.init();
|
|
||||||
}
|
|
||||||
const CHATGPT_TIMEOUT = 60 * 1000;
|
|
||||||
GBLog.info(`ChatGPT being used...`);
|
|
||||||
|
|
||||||
const response = await GBServer.globals.chatGPT.sendMessage(text, { timeoutMs: CHATGPT_TIMEOUT });
|
|
||||||
|
|
||||||
if (!response) {
|
|
||||||
GBLog.info(`SEARCH called but NO answer could be found (zero results).`);
|
GBLog.info(`SEARCH called but NO answer could be found (zero results).`);
|
||||||
} else {
|
|
||||||
await min.conversationalService.sendText(min, step, response);
|
|
||||||
}
|
|
||||||
return await step.replaceDialog('/ask', { isReturning: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not found.
|
// Not found.
|
||||||
|
|
||||||
const message = min.core.getParam<string>(min.instance, 'Not Found Message', Messages[locale].did_not_find);
|
const message = min.core.getParam<string>(min.instance, 'Not Found Message', Messages[locale].did_not_find);
|
||||||
|
|
||||||
await min.conversationalService.sendText(min, step, message);
|
await min.conversationalService.sendText(min, step, message);
|
||||||
|
|
||||||
return await step.replaceDialog('/ask', { isReturning: true });
|
return await step.replaceDialog('/ask', { isReturning: true });
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
@ -105,12 +105,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
this.whatsappServiceUrl = whatsappServiceUrl;
|
this.whatsappServiceUrl = whatsappServiceUrl;
|
||||||
this.provider =
|
this.provider =
|
||||||
whatsappServiceKey === 'internal'
|
whatsappServiceKey === 'internal'
|
||||||
? 'GeneralBots'
|
? 'GeneralBots' : 'official';
|
||||||
: whatsappServiceKey.indexOf('official') > -1
|
|
||||||
? 'official'
|
|
||||||
: whatsappServiceKey !== 'internal'
|
|
||||||
? 'graphapi'
|
|
||||||
: 'chatapi';
|
|
||||||
this.groupId = groupId;
|
this.groupId = groupId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,10 +129,10 @@ export class WhatsappDirectLine extends GBService {
|
||||||
let options: any;
|
let options: any;
|
||||||
|
|
||||||
switch (this.provider) {
|
switch (this.provider) {
|
||||||
case 'Official':
|
case 'official':
|
||||||
const accountSid = process.env.TWILIO_ACCOUNT_SID;
|
const accountSid = process.env.TWILIO_ACCOUNT_SID;
|
||||||
const authToken = process.env.TWILIO_AUTH_TOKEN;
|
const authToken = process.env.TWILIO_AUTH_TOKEN;
|
||||||
this.customClient = twilio(accountSid, authToken);
|
this.customClient = twilio(null, authToken, { accountSid: accountSid });
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'GeneralBots':
|
case 'GeneralBots':
|
||||||
|
@ -207,59 +202,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
setUrl = false;
|
setUrl = false;
|
||||||
|
|
||||||
break;
|
|
||||||
case 'chatapi':
|
|
||||||
options = {
|
|
||||||
method: 'POST',
|
|
||||||
url: urlJoin(this.whatsappServiceUrl, 'webhook'),
|
|
||||||
timeout: 10000,
|
|
||||||
qs: {
|
|
||||||
token: this.whatsappServiceKey,
|
|
||||||
webhookUrl: `${GBServer.globals.publicAddress}/webhooks/whatsapp/${this.botId}`,
|
|
||||||
set: true
|
|
||||||
},
|
|
||||||
headers: {
|
|
||||||
'cache-control': 'no-cache'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
case 'official':
|
|
||||||
url = urlJoin(this.whatsappServiceUrl, 'webhook');
|
|
||||||
options = {
|
|
||||||
method: 'POST',
|
|
||||||
url: url,
|
|
||||||
timeout: 10000,
|
|
||||||
qs: {
|
|
||||||
token: this.whatsappServiceKey,
|
|
||||||
webhookUrl: `${GBServer.globals.publicAddress}/webhooks/whatsapp/${this.botId}`,
|
|
||||||
set: true
|
|
||||||
},
|
|
||||||
headers: {
|
|
||||||
'cache-control': 'no-cache'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'maytapi':
|
|
||||||
const phoneId = this.whatsappServiceNumber.split(';')[0];
|
|
||||||
let productId = this.whatsappServiceNumber.split(';')[1];
|
|
||||||
url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`;
|
|
||||||
body = {
|
|
||||||
webhook: `${GBServer.globals.publicAddress}/webhooks/whatsapp/${this.botId}`,
|
|
||||||
ack_delivery: false
|
|
||||||
};
|
|
||||||
WhatsappDirectLine.phones[phoneId] = this.botId;
|
|
||||||
|
|
||||||
options = {
|
|
||||||
url: url,
|
|
||||||
method: 'POST',
|
|
||||||
body: body,
|
|
||||||
headers: {
|
|
||||||
'x-maytapi-key': this.whatsappServiceKey,
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
json: true
|
|
||||||
};
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -301,7 +243,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static providerFromRequest(req: any) {
|
public static providerFromRequest(req: any) {
|
||||||
return req.body.messages ? 'chatapi' : req.body.message ? 'maytapi' : req.body.message ? 'graphapi' : 'GeneralBots';
|
return req.body.ProfileName ? 'official' : 'GeneralBots';
|
||||||
}
|
}
|
||||||
|
|
||||||
public async received(req, res) {
|
public async received(req, res) {
|
||||||
|
@ -313,6 +255,14 @@ export class WhatsappDirectLine extends GBService {
|
||||||
let attachments = null;
|
let attachments = null;
|
||||||
|
|
||||||
switch (provider) {
|
switch (provider) {
|
||||||
|
|
||||||
|
case 'official':
|
||||||
|
message = req.body;
|
||||||
|
from = req.body.From.replace(/whatsapp\:\+/gi, '');
|
||||||
|
to = req.body.To.replace(/whatsapp\:\+/gi, '');
|
||||||
|
text = req.body.Body;
|
||||||
|
fromName = req.body.ProfileName;
|
||||||
|
break;
|
||||||
case 'GeneralBots':
|
case 'GeneralBots':
|
||||||
message = req;
|
message = req;
|
||||||
to = message.to.endsWith('@g.us') ? message.to.split('@')[0] : message.to.split('@')[0];
|
to = message.to.endsWith('@g.us') ? message.to.split('@')[0] : message.to.split('@')[0];
|
||||||
|
@ -355,44 +305,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'chatapi':
|
|
||||||
message = req.body.messages[0];
|
|
||||||
text = message.body;
|
|
||||||
from = req.body.messages[0].author.split('@')[0];
|
|
||||||
fromName = req.body.messages[0].senderName;
|
|
||||||
|
|
||||||
if (message.type !== 'chat') {
|
|
||||||
attachments = [];
|
|
||||||
attachments.push({
|
|
||||||
name: 'uploaded',
|
|
||||||
contentType: 'application/octet-stream',
|
|
||||||
contentUrl: message.body
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (req.body.messages[0].fromMe) {
|
|
||||||
res.end();
|
|
||||||
|
|
||||||
return; // Exit here.
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'graphapi':
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'maytapi':
|
|
||||||
message = req.body.message;
|
|
||||||
text = message.text;
|
|
||||||
from = req.body.user.phone;
|
|
||||||
fromName = req.body.user.name;
|
|
||||||
|
|
||||||
if (req.body.message.fromMe) {
|
|
||||||
res.end();
|
|
||||||
|
|
||||||
return; // Exit here.
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
text = text.replace(/\@\d+ /gi, '');
|
text = text.replace(/\@\d+ /gi, '');
|
||||||
|
@ -406,11 +318,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
botShortcuts = botShortcuts.split(' ');
|
botShortcuts = botShortcuts.split(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (provider === 'chatapi') {
|
if (provider === 'GeneralBots') {
|
||||||
if (message.chatName.charAt(0) !== '+') {
|
|
||||||
group = message.chatName;
|
|
||||||
}
|
|
||||||
} else if (provider === 'GeneralBots') {
|
|
||||||
if (message.from.endsWith('@g.us')) {
|
if (message.from.endsWith('@g.us')) {
|
||||||
group = message.from;
|
group = message.from;
|
||||||
}
|
}
|
||||||
|
@ -749,66 +657,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
await this.customClient.sendMessage(to, attachment, { caption: caption });
|
await this.customClient.sendMessage(to, attachment, { caption: caption });
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'chatapi':
|
|
||||||
options = {
|
|
||||||
method: 'POST',
|
|
||||||
url: urlJoin(this.whatsappServiceUrl, 'sendFile'),
|
|
||||||
qs: {
|
|
||||||
token: this.whatsappServiceKey
|
|
||||||
},
|
|
||||||
json: true,
|
|
||||||
body: {
|
|
||||||
phone: to,
|
|
||||||
body: url,
|
|
||||||
filename: filename,
|
|
||||||
caption: caption
|
|
||||||
},
|
|
||||||
headers: {
|
|
||||||
'cache-control': 'no-cache'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'maytapi':
|
|
||||||
let contents = 0;
|
|
||||||
let body = {
|
|
||||||
to_number: to,
|
|
||||||
type: 'media',
|
|
||||||
message: url,
|
|
||||||
text: caption
|
|
||||||
};
|
|
||||||
|
|
||||||
let phoneId = this.whatsappServiceNumber.split(';')[0];
|
|
||||||
let productId = this.whatsappServiceNumber.split(';')[1];
|
|
||||||
|
|
||||||
options = {
|
|
||||||
url: `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`,
|
|
||||||
method: 'post',
|
|
||||||
json: true,
|
|
||||||
body,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'x-maytapi-key': this.whatsappServiceKey
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'graphapi':
|
|
||||||
url = `https://graph.facebook.com/v15.0/${phoneId}/messages`;
|
|
||||||
options = {
|
|
||||||
method: 'POST',
|
|
||||||
timeout: 10000,
|
|
||||||
headers: {
|
|
||||||
token: `Bearer `,
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: {
|
|
||||||
messaging_product: 'whatsapp',
|
|
||||||
recipient_type: 'individual',
|
|
||||||
to: phoneId
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options) {
|
if (options) {
|
||||||
|
@ -831,24 +679,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'chatapi':
|
|
||||||
options = {
|
|
||||||
method: 'POST',
|
|
||||||
url: urlJoin(this.whatsappServiceUrl, 'sendPTT'),
|
|
||||||
qs: {
|
|
||||||
token: this.whatsappServiceKey,
|
|
||||||
phone: chatId ? null : to,
|
|
||||||
chatId: chatId,
|
|
||||||
body: url
|
|
||||||
},
|
|
||||||
headers: {
|
|
||||||
'cache-control': 'no-cache'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'maytapi':
|
|
||||||
throw GBError.create('Sending audio in Maytapi not supported.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options) {
|
if (options) {
|
||||||
|
@ -881,11 +711,16 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
switch (this.provider) {
|
switch (this.provider) {
|
||||||
|
|
||||||
case 'Official':
|
case 'official':
|
||||||
const botNumber = this.min.core.getParam(this.min.instance, 'Bot Number', null);
|
const botNumber = this.min.core.getParam(this.min.instance, 'Bot Number', null);
|
||||||
if (to.charAt(0) !== '+') {
|
if (to.charAt(0) !== '+') {
|
||||||
to = `+${to}`
|
to = `+${to}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let messages = msg.match(/(.|[\r\n]){1,1000}/g)
|
||||||
|
|
||||||
|
await CollectionUtil.asyncForEach(messages, async msg => {
|
||||||
|
await GBUtil.sleep(3000);
|
||||||
await this.customClient.messages
|
await this.customClient.messages
|
||||||
.create({
|
.create({
|
||||||
body: msg,
|
body: msg,
|
||||||
|
@ -894,6 +729,9 @@ export class WhatsappDirectLine extends GBService {
|
||||||
// TODO: mediaUrl.
|
// TODO: mediaUrl.
|
||||||
});
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
case 'GeneralBots':
|
case 'GeneralBots':
|
||||||
to = to.replace('+', '');
|
to = to.replace('+', '');
|
||||||
|
@ -913,38 +751,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'chatapi':
|
|
||||||
options = {
|
|
||||||
method: 'POST',
|
|
||||||
url: urlJoin(this.whatsappServiceUrl, 'message'),
|
|
||||||
qs: {
|
|
||||||
token: this.whatsappServiceKey,
|
|
||||||
phone: chatId ? null : to,
|
|
||||||
chatId: chatId,
|
|
||||||
body: msg
|
|
||||||
},
|
|
||||||
headers: {
|
|
||||||
'cache-control': 'no-cache'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
case 'maytapi':
|
|
||||||
let phoneId = this.whatsappServiceNumber.split(';')[0];
|
|
||||||
let productId = this.whatsappServiceNumber.split(';')[1];
|
|
||||||
url = `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`;
|
|
||||||
|
|
||||||
options = {
|
|
||||||
method: 'post',
|
|
||||||
json: true,
|
|
||||||
body: { type: 'text', message: msg, to_number: to },
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'x-maytapi-key': this.whatsappServiceKey
|
|
||||||
}
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
case 'graphapi':
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options) {
|
if (options) {
|
||||||
|
@ -963,25 +769,26 @@ export class WhatsappDirectLine extends GBService {
|
||||||
await this.sendToDevice(to, text, conversationId);
|
await this.sendToDevice(to, text, conversationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async WhatsAppCallback(req, res) {
|
private async WhatsAppCallback(req, res, botId = null) {
|
||||||
try {
|
try {
|
||||||
if (!req.body && req.type !== 'ptt') {
|
if (!req.body && req.type !== 'ptt') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let provider = GBMinService.isChatAPI(req, res);
|
let provider = GBMinService.getProviderName(req, res);
|
||||||
let id;
|
let id;
|
||||||
let senderName;
|
let senderName;
|
||||||
let botId;
|
|
||||||
let text;
|
let text;
|
||||||
|
|
||||||
switch (provider) {
|
switch (provider) {
|
||||||
|
|
||||||
case 'Official':
|
case 'official':
|
||||||
|
|
||||||
const { body } = req;
|
const { body } = req;
|
||||||
|
|
||||||
let message;
|
id = body.From.replace(/whatsapp\:\+/, '');
|
||||||
|
senderName = body.ProfileName;
|
||||||
|
text = body.Body;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -995,47 +802,8 @@ export class WhatsappDirectLine extends GBService {
|
||||||
id = req.from.split('@')[0];
|
id = req.from.split('@')[0];
|
||||||
senderName = req._data.notifyName;
|
senderName = req._data.notifyName;
|
||||||
text = req.body;
|
text = req.body;
|
||||||
botId = this.botId;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'chatapi':
|
|
||||||
if (req.body.ack) {
|
|
||||||
res.status(200);
|
|
||||||
res.end();
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (req.body.messages[0].fromMe) {
|
|
||||||
res.end();
|
|
||||||
|
|
||||||
return; // Exit here.
|
|
||||||
}
|
|
||||||
id = req.body.messages[0].author.split('@')[0];
|
|
||||||
senderName = req.body.messages[0].senderName;
|
|
||||||
text = req.body.messages[0].body;
|
|
||||||
botId = req.params.botId;
|
|
||||||
if (botId === '[default]' || botId === undefined) {
|
|
||||||
botId = GBConfigService.get('BOT_ID');
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'maytapi':
|
|
||||||
if (req.body.type !== 'message') {
|
|
||||||
res.status(200);
|
|
||||||
res.end();
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (req.body.message.fromMe) {
|
|
||||||
res.end();
|
|
||||||
|
|
||||||
return; // Exit here.
|
|
||||||
}
|
|
||||||
id = req.body.user.phone;
|
|
||||||
senderName = req.body.user.name;
|
|
||||||
text = req.body.message.text;
|
|
||||||
|
|
||||||
botId = WhatsappDirectLine.phones[req.body.phoneId];
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sec = new SecService();
|
const sec = new SecService();
|
||||||
|
@ -1113,14 +881,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
text = text.replace(/\@\d+ /gi, '');
|
text = text.replace(/\@\d+ /gi, '');
|
||||||
|
|
||||||
let group;
|
let group;
|
||||||
if (provider === 'chatapi') {
|
if (provider === 'GeneralBots') {
|
||||||
// Ensures that the bot group is the active bot for the user (like switching).
|
|
||||||
|
|
||||||
const message = req.body.messages[0];
|
|
||||||
if (message.chatName.charAt(0) !== '+') {
|
|
||||||
group = message.chatName;
|
|
||||||
}
|
|
||||||
} else if (provider === 'GeneralBots') {
|
|
||||||
// Ensures that the bot group is the active bot for the user (like switching).
|
// Ensures that the bot group is the active bot for the user (like switching).
|
||||||
|
|
||||||
const message = req;
|
const message = req;
|
||||||
|
@ -1185,11 +946,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
if (startDialog) {
|
if (startDialog) {
|
||||||
GBLog.info(`Calling /start to Auto start ${startDialog} for ${activeMin.instance.instanceId}...`);
|
GBLog.info(`Calling /start to Auto start ${startDialog} for ${activeMin.instance.instanceId}...`);
|
||||||
if (provider === 'chatapi') {
|
if (provider === 'GeneralBots') {
|
||||||
req.body.messages[0].body = `/start`;
|
|
||||||
} else if (provider === 'maytapi') {
|
|
||||||
req.body.message = `/start`;
|
|
||||||
} else {
|
|
||||||
req.body = `/start`;
|
req.body = `/start`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1221,11 +978,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
if (startDialog) {
|
if (startDialog) {
|
||||||
|
|
||||||
GBLog.info(`Calling /start for Auto start : ${startDialog} for ${activeMin.instance.botId}...`);
|
GBLog.info(`Calling /start for Auto start : ${startDialog} for ${activeMin.instance.botId}...`);
|
||||||
if (provider === 'chatapi') {
|
if (provider === 'GeneralBots') {
|
||||||
req.body.messages[0].body = `/start`;
|
|
||||||
} else if (provider === 'maytapi') {
|
|
||||||
req.body.message = `/start`;
|
|
||||||
} else {
|
|
||||||
req.body = `/start`;
|
req.body = `/start`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue