Built motion from commit 9d0783e6.|2.6.13
[motion2.git] / server / api / whatsappMessage / whatsappMessage.model.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0xf462=['message_id','err','WhatsappMessage,\x20%s','finally','utf8mb4','lodash','../../config/logger','api','bluebird','node-fetch','path','rimraf','../../config/environment','exports','define','WhatsappMessage','whatsapp_messages','messageId','fti_whatsapp_messages','FULLTEXT','get','direction','toLowerCase','out','isNil','UserId','WhatsappInteraction','findOne','WhatsappInteractionId','then','update','createdAt','catch','error','secret','WhatsappAccount','find','WhatsappAccountId','type','twilio','accountSid','authToken','create','whatsapp:','phone','remote','/api/whatsapp/messages/','/status?token=','token','WhatsappMessage,\x20%s,\x20%s,\x20%s','afterCreate','inspect','Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken','sinch','baseUrl','/whatsapp/v1/','/messages/','body','POST','statuses'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0xf462,0xb9));var _0x2f46=function(_0x2ffc46,_0x35ce3c){_0x2ffc46=_0x2ffc46-0x0;var _0x1eda8f=_0xf462[_0x2ffc46];return _0x1eda8f;};'use strict';var _=require(_0x2f46('0x0'));var util=require('util');var logger=require(_0x2f46('0x1'))(_0x2f46('0x2'));var moment=require('moment');var BPromise=require(_0x2f46('0x3'));var rp=require('request-promise');var fetch=require(_0x2f46('0x4'));var fs=require('fs');var path=require(_0x2f46('0x5'));var rimraf=require(_0x2f46('0x6'));var config=require(_0x2f46('0x7'));var attributes=require('./whatsappMessage.attributes');module[_0x2f46('0x8')]=function(_0x11a386,_0x425fe5){return _0x11a386[_0x2f46('0x9')](_0x2f46('0xa'),attributes,{'tableName':_0x2f46('0xb'),'paranoid':![],'indexes':[{'name':_0x2f46('0xc'),'fields':[_0x2f46('0xc')]},{'name':_0x2f46('0xd'),'fields':['body'],'type':_0x2f46('0xe')}],'timestamps':!![],'hooks':{'afterCreate':function(_0x18369a,_0x3d4bcf,_0x450be0){var _0x4663be=_0x11a386['models'];var _0x418d64=_0x18369a[_0x2f46('0xf')]({'plain':!![]});if(_0x418d64[_0x2f46('0x10')][_0x2f46('0x11')]()==='in'||_0x418d64['direction'][_0x2f46('0x11')]()===_0x2f46('0x12')&&!_0x418d64['secret']&&!_[_0x2f46('0x13')](_0x418d64[_0x2f46('0x14')])){_0x4663be[_0x2f46('0x15')][_0x2f46('0x16')]({'where':{'id':_0x418d64[_0x2f46('0x17')]}})[_0x2f46('0x18')](function(_0x5f8899){if(_0x5f8899){_0x5f8899[_0x2f46('0x19')]({'lastMsgAt':_0x418d64[_0x2f46('0x1a')],'lastMsgDirection':_0x418d64[_0x2f46('0x10')][_0x2f46('0x11')]()});}})[_0x2f46('0x1b')](function(_0x4974f6){console[_0x2f46('0x1c')](_0x4974f6);});}if(_0x418d64[_0x2f46('0x10')]===_0x2f46('0x12')&&!_0x418d64[_0x2f46('0x1d')]){var _0x1bd70a;_0x4663be[_0x2f46('0x1e')][_0x2f46('0x1f')]({'where':{'id':_0x418d64[_0x2f46('0x20')]},'raw':!![]})[_0x2f46('0x18')](function(_0xff1448){_0x1bd70a=_0xff1448;if(_0x1bd70a&&_0x1bd70a[_0x2f46('0x21')]){switch(_0x1bd70a[_0x2f46('0x21')]){case'twilio':var _0x28beec=require(_0x2f46('0x22'));if(_0x1bd70a[_0x2f46('0x23')]&&_0x1bd70a[_0x2f46('0x24')]&&_0x1bd70a['phone']){var _0x50c666=new _0x28beec(_0x1bd70a[_0x2f46('0x23')],_0x1bd70a[_0x2f46('0x24')]);_0x50c666['messages'][_0x2f46('0x25')]({'body':_0x418d64['body'],'to':_0x2f46('0x26')+_0x418d64[_0x2f46('0x27')],'from':_0x2f46('0x26')+_0x1bd70a['phone'],'statusCallback':_0x1bd70a[_0x2f46('0x28')]+_0x2f46('0x29')+_0x418d64['id']+_0x2f46('0x2a')+_0x1bd70a[_0x2f46('0x2b')]})[_0x2f46('0x18')](function(_0x22fbb6){if(_0x22fbb6['sid']){return _0x18369a[_0x2f46('0x19')]({'messageId':_0x22fbb6['sid']});}})[_0x2f46('0x1b')](function(_0x4009a9){logger['error'](_0x2f46('0x2c'),_0x2f46('0x2d'),_0x2f46('0x22'),util[_0x2f46('0x2e')](_0x4009a9,{'showHidden':![],'depth':null}));});}else{logger[_0x2f46('0x1c')](_0x2f46('0x2c'),'afterCreate',_0x2f46('0x22'),_0x2f46('0x2f'));}break;case _0x2f46('0x30'):var _0x53fa54=_0x1bd70a['accountSid'];var _0xbdf362=_0x1bd70a[_0x2f46('0x24')];var _0x2f4003=_0x1bd70a[_0x2f46('0x31')]+_0x2f46('0x32')+_0x53fa54+_0x2f46('0x33');var _0x27ea04={'to':[_0x418d64[_0x2f46('0x27')]],'message':{'type':'text','text':_0x418d64[_0x2f46('0x34')]}};var _0x28cd8f={'method':_0x2f46('0x35'),'uri':_0x2f4003,'headers':{'Authorization':'Bearer\x20'+_0xbdf362,'Content-Type':'application/json'},'body':_0x27ea04,'json':!![]};rp(_0x28cd8f)[_0x2f46('0x18')](function(_0x572c72){if(_0x572c72[_0x2f46('0x36')][0x0]['message_id']){return _0x18369a[_0x2f46('0x19')]({'messageId':_0x572c72[_0x2f46('0x36')][0x0][_0x2f46('0x37')]});}})[_0x2f46('0x1b')](function(_0x3c09c8){logger[_0x2f46('0x38')](_0x2f46('0x39'),_0x3c09c8);});break;}}})[_0x2f46('0x3a')](function(){_0x450be0();});}else{_0x450be0();}}},'charset':_0x2f46('0x3b'),'collate':'utf8mb4_unicode_ci'});};