new(whatsapp.gblib): General Bots WhatsApp provider.
This commit is contained in:
parent
b84d1a6983
commit
6891b8c8c4
2 changed files with 65 additions and 35 deletions
|
@ -384,12 +384,10 @@ export class SystemKeywords {
|
||||||
public async getRandomId() {
|
public async getRandomId() {
|
||||||
|
|
||||||
const idGeneration = this.dk['idGeneration'];
|
const idGeneration = this.dk['idGeneration'];
|
||||||
if (idGeneration && idGeneration.trim().toLowerCase() === 'number')
|
if (idGeneration && idGeneration.trim().toLowerCase() === 'number') {
|
||||||
{
|
|
||||||
return GBAdminService.getNumberIdentifier();
|
return GBAdminService.getNumberIdentifier();
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
return GBAdminService.getRndReadableIdentifier().substr(5);
|
return GBAdminService.getRndReadableIdentifier().substr(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -271,6 +271,38 @@ export class GBConversationalService {
|
||||||
// tslint:disable:no-unsafe-any due to Nexmo.
|
// tslint:disable:no-unsafe-any due to Nexmo.
|
||||||
public async sendSms(min: GBMinInstance, mobile: string, text: string): Promise<any> {
|
public async sendSms(min: GBMinInstance, mobile: string, text: string): Promise<any> {
|
||||||
GBLog.info(`Sending SMS to ${mobile} with text: '${text}'.`);
|
GBLog.info(`Sending SMS to ${mobile} with text: '${text}'.`);
|
||||||
|
|
||||||
|
if (!min.instance.smsKey && min.instance.smsSecret) {
|
||||||
|
let options = {
|
||||||
|
method: 'POST',
|
||||||
|
url: 'http://sms-api.megaconecta.com.br/mt',
|
||||||
|
headers: {
|
||||||
|
"content-type": "application/json",
|
||||||
|
"authorization": `Bearer ${min.instance.smsSecret}`
|
||||||
|
},
|
||||||
|
body: [
|
||||||
|
{
|
||||||
|
"numero": `${mobile}`,
|
||||||
|
"servico": "short",
|
||||||
|
"mensagem": text,
|
||||||
|
"parceiro_id": "",
|
||||||
|
"codificacao": "0"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
json: true
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const results = await request(options);
|
||||||
|
|
||||||
|
return results[0].translations[0].text;
|
||||||
|
} catch (error) {
|
||||||
|
const msg = `Error calling SMS service. Error is: ${error}.`;
|
||||||
|
|
||||||
|
return Promise.reject(new Error(msg));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
return new Promise((resolve: any, reject: any): any => {
|
return new Promise((resolve: any, reject: any): any => {
|
||||||
const nexmo = new Nexmo({
|
const nexmo = new Nexmo({
|
||||||
apiKey: min.instance.smsKey,
|
apiKey: min.instance.smsKey,
|
||||||
|
@ -288,12 +320,14 @@ export class GBConversationalService {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async sendToMobile(min: GBMinInstance, mobile: string, message: string, conversationId) {
|
public async sendToMobile(min: GBMinInstance, mobile: string, message: string, conversationId) {
|
||||||
GBLog.info(`Sending message ${message} to ${mobile}...`);
|
GBLog.info(`Sending message ${message} to ${mobile}...`);
|
||||||
await min.whatsAppDirectLine.sendToDevice(mobile, message, conversationId);
|
await min.whatsAppDirectLine.sendToDevice(mobile, message, conversationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static async getAudioBufferFromText(text): Promise<string> {
|
public static async getAudioBufferFromText(text): Promise<string> {
|
||||||
return new Promise<string>(async (resolve, reject) => {
|
return new Promise<string>(async (resolve, reject) => {
|
||||||
const name = GBAdminService.getRndReadableIdentifier();
|
const name = GBAdminService.getRndReadableIdentifier();
|
||||||
|
@ -416,8 +450,7 @@ export class GBConversationalService {
|
||||||
let text = answer;
|
let text = answer;
|
||||||
|
|
||||||
// Calls language translator.
|
// Calls language translator.
|
||||||
if (user)
|
if (user) {
|
||||||
{
|
|
||||||
text = await min.conversationalService.translate(
|
text = await min.conversationalService.translate(
|
||||||
min,
|
min,
|
||||||
answer,
|
answer,
|
||||||
|
@ -948,8 +981,7 @@ export class GBConversationalService {
|
||||||
}
|
}
|
||||||
|
|
||||||
const analytics = new AnalyticsService();
|
const analytics = new AnalyticsService();
|
||||||
if (!user.conversation)
|
if (!user.conversation) {
|
||||||
{
|
|
||||||
user.conversation = await analytics.createConversation(user);
|
user.conversation = await analytics.createConversation(user);
|
||||||
}
|
}
|
||||||
analytics.createMessage(min.instance.instanceId, user.conversation, null, text);
|
analytics.createMessage(min.instance.instanceId, user.conversation, null, text);
|
||||||
|
|
Loading…
Add table
Reference in a new issue