a424238906b425ce0f7716626429ae2f3efdbfd8
[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 _0xf3cc=['node-fetch','rimraf','../../config/environment','exports','define','WhatsappMessage','whatsapp_messages','messageId','fti_whatsapp_messages','body','models','get','direction','toLowerCase','out','isNil','WhatsappInteractionId','update','catch','error','secret','WhatsappAccount','WhatsappAccountId','then','type','twilio','accountSid','authToken','phone','create','whatsapp:','remote','/api/whatsapp/messages/','/status?token=','token','sid','WhatsappMessage,\x20%s,\x20%s,\x20%s','afterCreate','inspect','sinch','/whatsapp/v1/','/messages/','POST','application/json','message_id','statuses','err','finally','utf8mb4_unicode_ci','lodash','util','../../config/logger','api','bluebird','request-promise'];(function(_0x59ee6f,_0x450239){var _0x4c0979=function(_0x565e92){while(--_0x565e92){_0x59ee6f['push'](_0x59ee6f['shift']());}};_0x4c0979(++_0x450239);}(_0xf3cc,0x10d));var _0xcf3c=function(_0x38ecda,_0x3b551e){_0x38ecda=_0x38ecda-0x0;var _0x3fa8f3=_0xf3cc[_0x38ecda];return _0x3fa8f3;};'use strict';var _=require(_0xcf3c('0x0'));var util=require(_0xcf3c('0x1'));var logger=require(_0xcf3c('0x2'))(_0xcf3c('0x3'));var moment=require('moment');var BPromise=require(_0xcf3c('0x4'));var rp=require(_0xcf3c('0x5'));var fetch=require(_0xcf3c('0x6'));var fs=require('fs');var path=require('path');var rimraf=require(_0xcf3c('0x7'));var config=require(_0xcf3c('0x8'));var attributes=require('./whatsappMessage.attributes');module[_0xcf3c('0x9')]=function(_0x2b5cae,_0x35cb18){return _0x2b5cae[_0xcf3c('0xa')](_0xcf3c('0xb'),attributes,{'tableName':_0xcf3c('0xc'),'paranoid':![],'indexes':[{'name':_0xcf3c('0xd'),'fields':[_0xcf3c('0xd')]},{'name':_0xcf3c('0xe'),'fields':[_0xcf3c('0xf')],'type':'FULLTEXT'}],'timestamps':!![],'hooks':{'afterCreate':function(_0x58fe7b,_0x2c2b70,_0xd4d142){var _0x3022f2=_0x2b5cae[_0xcf3c('0x10')];var _0x46e82c=_0x58fe7b[_0xcf3c('0x11')]({'plain':!![]});if(_0x46e82c[_0xcf3c('0x12')][_0xcf3c('0x13')]()==='in'||_0x46e82c['direction'][_0xcf3c('0x13')]()===_0xcf3c('0x14')&&!_0x46e82c['secret']&&!_[_0xcf3c('0x15')](_0x46e82c['UserId'])){_0x3022f2['WhatsappInteraction']['findOne']({'where':{'id':_0x46e82c[_0xcf3c('0x16')]}})['then'](function(_0x2f387b){if(_0x2f387b){_0x2f387b[_0xcf3c('0x17')]({'lastMsgAt':_0x46e82c['createdAt'],'lastMsgDirection':_0x46e82c[_0xcf3c('0x12')]['toLowerCase']()});}})[_0xcf3c('0x18')](function(_0x1db425){console[_0xcf3c('0x19')](_0x1db425);});}if(_0x46e82c['direction']===_0xcf3c('0x14')&&!_0x46e82c[_0xcf3c('0x1a')]){var _0x395159;_0x3022f2[_0xcf3c('0x1b')]['find']({'where':{'id':_0x46e82c[_0xcf3c('0x1c')]},'raw':!![]})[_0xcf3c('0x1d')](function(_0x25ebf3){_0x395159=_0x25ebf3;if(_0x395159&&_0x395159[_0xcf3c('0x1e')]){switch(_0x395159[_0xcf3c('0x1e')]){case _0xcf3c('0x1f'):var _0xf22c4e=require(_0xcf3c('0x1f'));if(_0x395159[_0xcf3c('0x20')]&&_0x395159[_0xcf3c('0x21')]&&_0x395159[_0xcf3c('0x22')]){var _0x15e346=new _0xf22c4e(_0x395159['accountSid'],_0x395159[_0xcf3c('0x21')]);_0x15e346['messages'][_0xcf3c('0x23')]({'body':_0x46e82c[_0xcf3c('0xf')],'to':'whatsapp:'+_0x46e82c[_0xcf3c('0x22')],'from':_0xcf3c('0x24')+_0x395159[_0xcf3c('0x22')],'statusCallback':_0x395159[_0xcf3c('0x25')]+_0xcf3c('0x26')+_0x46e82c['id']+_0xcf3c('0x27')+_0x395159[_0xcf3c('0x28')]})[_0xcf3c('0x1d')](function(_0x58b8e5){if(_0x58b8e5[_0xcf3c('0x29')]){return _0x58fe7b['update']({'messageId':_0x58b8e5[_0xcf3c('0x29')]});}})[_0xcf3c('0x18')](function(_0x42ce14){logger[_0xcf3c('0x19')](_0xcf3c('0x2a'),_0xcf3c('0x2b'),'twilio',util[_0xcf3c('0x2c')](_0x42ce14,{'showHidden':![],'depth':null}));});}else{logger['error'](_0xcf3c('0x2a'),_0xcf3c('0x2b'),_0xcf3c('0x1f'),'Missing\x20parameters:\x20phone,\x20accountSid\x20or\x20authToken');}break;case _0xcf3c('0x2d'):var _0x54174a=_0x395159[_0xcf3c('0x20')];var _0xe22e99=_0x395159[_0xcf3c('0x21')];var _0x5bdae3=_0x395159['baseUrl']+_0xcf3c('0x2e')+_0x54174a+_0xcf3c('0x2f');var _0x1f53ee={'to':[_0x46e82c[_0xcf3c('0x22')]],'message':{'type':'text','text':_0x46e82c[_0xcf3c('0xf')]}};var _0x2cf47e={'method':_0xcf3c('0x30'),'uri':_0x5bdae3,'headers':{'Authorization':'Bearer\x20'+_0xe22e99,'Content-Type':_0xcf3c('0x31')},'body':_0x1f53ee,'json':!![]};rp(_0x2cf47e)[_0xcf3c('0x1d')](function(_0x196b02){if(_0x196b02['statuses'][0x0][_0xcf3c('0x32')]){return _0x58fe7b[_0xcf3c('0x17')]({'messageId':_0x196b02[_0xcf3c('0x33')][0x0][_0xcf3c('0x32')]});}})[_0xcf3c('0x18')](function(_0x534f56){logger[_0xcf3c('0x34')]('WhatsappMessage,\x20%s',_0x534f56);});break;}}})[_0xcf3c('0x35')](function(){_0xd4d142();});}else{_0xd4d142();}}},'charset':'utf8mb4','collate':_0xcf3c('0x36')});};