Improvements on Whatsapp directline.
This commit is contained in:
parent
72433236b0
commit
1bc4b37827
2 changed files with 15 additions and 30 deletions
|
@ -80,7 +80,7 @@ export class KBService {
|
||||||
GuaribasQuestion.findOne({
|
GuaribasQuestion.findOne({
|
||||||
where: {
|
where: {
|
||||||
instanceId: instanceId,
|
instanceId: instanceId,
|
||||||
content: { $like: `%${text.trim()}%` }
|
content: `%${text.trim()}%`
|
||||||
}
|
}
|
||||||
}).then((question: GuaribasQuestion) => {
|
}).then((question: GuaribasQuestion) => {
|
||||||
if (question) {
|
if (question) {
|
||||||
|
|
|
@ -55,6 +55,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
whatsappServiceNumber: string;
|
whatsappServiceNumber: string;
|
||||||
whatsappServiceUrl: string;
|
whatsappServiceUrl: string;
|
||||||
botId: string;
|
botId: string;
|
||||||
|
watermark: string = null;
|
||||||
|
|
||||||
conversationIds = {};
|
conversationIds = {};
|
||||||
|
|
||||||
|
@ -88,7 +89,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
qs:
|
qs:
|
||||||
{
|
{
|
||||||
token: this.whatsappServiceKey,
|
token: this.whatsappServiceKey,
|
||||||
webhookUrl: `https://cec02d94.ngrok.io/instances/${this.botId}/whatsapp`
|
webhookUrl: `https://a2fc0900.ngrok.io/instances/${this.botId}/whatsapp`
|
||||||
},
|
},
|
||||||
headers:
|
headers:
|
||||||
{
|
{
|
||||||
|
@ -98,6 +99,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await request.post(options);
|
const result = await request.post(options);
|
||||||
|
logger.info(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('Error initializing DirectLine client', error);
|
logger.error('Error initializing DirectLine client', error);
|
||||||
}
|
}
|
||||||
|
@ -115,7 +117,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
let from = req.body.messages[0].author.split('@')[0];
|
let from = req.body.messages[0].author.split('@')[0];
|
||||||
let fromName = req.body.messages[0].senderName;
|
let fromName = req.body.messages[0].senderName;
|
||||||
|
|
||||||
if (req.body.messages[0].fromMe){
|
if (req.body.messages[0].fromMe) {
|
||||||
return; // Exit here.
|
return; // Exit here.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,8 +150,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
} else {
|
} else {
|
||||||
this.inputMessage(client, conversationId, text,
|
this.inputMessage(client, conversationId, text,
|
||||||
from, fromName);
|
from, fromName);
|
||||||
|
|
||||||
this.pollMessages(client, conversationId, from, fromName);
|
|
||||||
}
|
}
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
|
@ -183,14 +183,13 @@ export class WhatsappDirectLine extends GBService {
|
||||||
logger.info(`GBWhatsapp: Starting polling message for conversationId:
|
logger.info(`GBWhatsapp: Starting polling message for conversationId:
|
||||||
${conversationId}.`);
|
${conversationId}.`);
|
||||||
|
|
||||||
var watermark = null;
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
client.Conversations.Conversations_GetActivities({
|
client.Conversations.Conversations_GetActivities({
|
||||||
conversationId:
|
conversationId:
|
||||||
conversationId, watermark: watermark
|
conversationId, watermark: this.watermark
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
watermark = response.obj.watermark;
|
this.watermark = response.obj.watermark;
|
||||||
return response.obj.activities;
|
return response.obj.activities;
|
||||||
})
|
})
|
||||||
.then((activities) => {
|
.then((activities) => {
|
||||||
|
@ -205,23 +204,22 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
// Ignore own messages.
|
// Ignore own messages.
|
||||||
|
|
||||||
activities = activities.filter((m) => { return m.from.id === this.botId && m.type === "message"});
|
activities = activities.filter((m) => { return m.from.id === this.botId && m.type === "message" });
|
||||||
|
|
||||||
if (activities.length) {
|
if (activities.length) {
|
||||||
|
|
||||||
// Print other messages.
|
// Print other messages.
|
||||||
|
|
||||||
this.printMessage(activities[0], conversationId, from, fromName);
|
activities.forEach(activity => {
|
||||||
// activities.forEach(activity => {
|
this.printMessage(activity, conversationId, from, fromName);
|
||||||
// this.printMessage(activity, conversationId, from, fromName);
|
});
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printMessage(activity, conversationId, from, fromName) {
|
printMessage(activity, conversationId, from, fromName) {
|
||||||
|
|
||||||
let output: string;
|
let output = "";
|
||||||
|
|
||||||
if (activity.text) {
|
if (activity.text) {
|
||||||
logger.info(`GBWhatsapp: MSG: ${activity.text}`);
|
logger.info(`GBWhatsapp: MSG: ${activity.text}`);
|
||||||
|
@ -232,7 +230,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
activity.attachments.forEach((attachment) => {
|
activity.attachments.forEach((attachment) => {
|
||||||
switch (attachment.contentType) {
|
switch (attachment.contentType) {
|
||||||
case "application/vnd.microsoft.card.hero":
|
case "application/vnd.microsoft.card.hero":
|
||||||
output += this.renderHeroCard(attachment);
|
output += `\n${this.renderHeroCard(attachment)}`;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "image/png":
|
case "image/png":
|
||||||
|
@ -247,20 +245,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
|
|
||||||
renderHeroCard(attachment) {
|
renderHeroCard(attachment) {
|
||||||
let output: string;
|
return `${attachment.content.title} - ${attachment.content.text}`;
|
||||||
let width = 70;
|
|
||||||
|
|
||||||
let contentLine = function (content) {
|
|
||||||
return ' '.repeat((width - content.length) / 2) +
|
|
||||||
content +
|
|
||||||
' '.repeat((width - content.length) / 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
output += '/' + '*'.repeat(width + 1);
|
|
||||||
output += '*' + contentLine(attachment.content.title) + '*';
|
|
||||||
output += '*' + ' '.repeat(width) + '*';
|
|
||||||
output += '*' + contentLine(attachment.content.text) + '*';
|
|
||||||
output += '*'.repeat(width + 1) + '/';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendToDevice(conversationId, to, toName, msg) {
|
async sendToDevice(conversationId, to, toName, msg) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue