Built motion from commit 6db1674d.|2.6.23
[motion2.git] / server / services / agi / scripts / index.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 _0xf28b=['googleASR','uri','results','map','alternatives','transcript','join','confidence','googleASR\x20confidence','[%s]\x20%s:%s','message','googleDialogflow','result','source','action','actionIncomplete','fulfillment','speech','code','metadata','replace','SessionsClient','sessionPath','Dialogflow\x20V2\x20response:','queryResult','allRequiredParamsPresent','fields','boolValue','fulfillmentText','intent','isFallback','responseId','intentDetectionConfidence','tildeASR','asr','Tilde\x20ASR\x20confidence','awsPolly','polly','awsLex','lex','ispeechTTS','format','%s?apikey=%s&action=convert&text=%s&voice=%s&speed=%s&format=mp3','ispeechASR','model=%s','freeform=3','parse','nda','channelData','CustomAction','AudioFromTts','dirName','apiai-promise','bluebird','dialogflow','http','https','sox','google-tts-api','url','util','./aws','./google','./tilde','../../../config/logger','agi','https://api.ispeech.org/api/rest','protocol','host','path','WHAT_EVER','get','statusCode','\x20failed,\x20status\x20code\x20=\x20','statusMessage','createWriteStream','finish','info','finish\x20download\x20file','close','error','unlink','end','request\x20to\x20','data','error\x20convert','finish\x20to\x20convert','start','resolve','tmp','generate','.wav','writeFileSync','base64','then','chmodSync','777','split','readFileSync','toString','googleCloudTTS','https://texttospeech.googleapis.com/v1/','?key=','input','voice','audioConfig','MP3','text:synthesize','from','audioContent','writeFile','googleCloudTTS\x20path','googleTTS','https://translate.google.com/_/TranslateWebserverUi/data/batchexecute','utf8','POST','stringify','jQ1olc','generic','length','text\x20length\x20limit\x20reached','lang\x20\x22','\x22\x20might\x20not\x20exist','parse\x20response\x20failed:\x0a','.mp3','goggleTTS\x20path'];(function(_0x5d4345,_0x545967){var _0x5dec3c=function(_0x2d8b6b){while(--_0x2d8b6b){_0x5d4345['push'](_0x5d4345['shift']());}};_0x5dec3c(++_0x545967);}(_0xf28b,0x130));var _0xbf28=function(_0x3f69fe,_0x75b260){_0x3f69fe=_0x3f69fe-0x0;var _0x420267=_0xf28b[_0x3f69fe];return _0x420267;};'use strict';var apiai=require(_0xbf28('0x0'));var BPromise=require(_0xbf28('0x1'));var dialogflow=require(_0xbf28('0x2'));var fs=require('fs');var http=require(_0xbf28('0x3'));var https=require(_0xbf28('0x4'));var md5=require('md5');var path=require('path');var querystring=require('querystring');var rp=require('request-promise');var rs=require('randomstring');var sox=require(_0xbf28('0x5'));var tts=require(_0xbf28('0x6'));var urlParse=require(_0xbf28('0x7'))['parse'];var util=require(_0xbf28('0x8'));var aws=require(_0xbf28('0x9'));var google=require(_0xbf28('0xa'));var sestek=require('./sestek');var tilde=require(_0xbf28('0xb'));var logger=require(_0xbf28('0xc'))(_0xbf28('0xd'));var ISPEECH_API=_0xbf28('0xe');function downloadFile(_0x4a653e,_0xd07f74){return new BPromise(function(_0x144c1f,_0x5c6166){var _0x5887b2=urlParse(_0x4a653e);var _0x2e3f6e=_0x5887b2[_0xbf28('0xf')]==='https:'?https:http;var _0x573b59={'host':_0x5887b2[_0xbf28('0x10')],'path':_0x5887b2[_0xbf28('0x11')],'headers':{'user-agent':_0xbf28('0x12')}};_0x2e3f6e[_0xbf28('0x13')](_0x573b59,function(_0x337296){if(_0x337296[_0xbf28('0x14')]!==0xc8){logger['error']('request\x20to\x20'+_0x4a653e+_0xbf28('0x15')+_0x337296[_0xbf28('0x14')]+'\x20('+_0x337296[_0xbf28('0x16')]+')');_0x5c6166(new Error('request\x20to\x20'+_0x4a653e+_0xbf28('0x15')+_0x337296['statusCode']+'\x20('+_0x337296[_0xbf28('0x16')]+')'));return;}var _0x10b8bb=fs[_0xbf28('0x17')](_0xd07f74);_0x10b8bb['on'](_0xbf28('0x18'),function(){logger[_0xbf28('0x19')](_0xbf28('0x1a'));_0x10b8bb[_0xbf28('0x1b')](_0x144c1f);});_0x10b8bb['on'](_0xbf28('0x1c'),function(_0x4753db){fs[_0xbf28('0x1d')](_0xd07f74);logger['error']('download\x20error',_0x4753db);_0x5c6166(_0x4753db);});_0x337296['pipe'](_0x10b8bb);})['on'](_0xbf28('0x1c'),function(_0x33dac7){logger[_0xbf28('0x1c')](_0x33dac7);_0x5c6166(_0x33dac7);})[_0xbf28('0x1e')]();});}function uploadFile(_0x354e88){return new BPromise(function(_0x5a5776,_0x2640d4){var _0x385265=urlParse(_0x354e88);var _0x44fff3=_0x385265['protocol']==='https:'?https:http;var _0x4226ca={'host':_0x385265['host'],'path':_0x385265[_0xbf28('0x11')],'headers':{'user-agent':_0xbf28('0x12')}};_0x44fff3[_0xbf28('0x13')](_0x4226ca,function(_0x22041c){if(_0x22041c[_0xbf28('0x14')]!==0xc8){_0x2640d4(new Error(_0xbf28('0x1f')+_0x354e88+_0xbf28('0x15')+_0x22041c[_0xbf28('0x14')]+'\x20('+_0x22041c[_0xbf28('0x16')]+')'));return;}var _0x70aada='';_0x22041c['on'](_0xbf28('0x20'),function(_0x1abc31){_0x70aada+=_0x1abc31;});_0x22041c['on']('end',function(){_0x5a5776(_0x70aada);});})['on'](_0xbf28('0x1c'),function(_0x51e632){_0x2640d4(_0x51e632);})[_0xbf28('0x1e')]();});}function convertFile(_0x155533,_0x21bde7){return new BPromise(function(_0x3ed9fd,_0x4957c7){var _0x198930=sox['transcode'](_0x155533,_0x21bde7,{'sampleRate':0x1f40,'format':'wav','channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x198930['on'](_0xbf28('0x1c'),function(_0x3df6b9){logger['error'](_0xbf28('0x21'),_0x3df6b9);_0x4957c7(_0x3df6b9);});_0x198930['on'](_0xbf28('0x1e'),function(){logger[_0xbf28('0x19')](_0xbf28('0x22'));_0x3ed9fd();});_0x198930[_0xbf28('0x23')]();});}function base64Decode(_0x5279f5,_0x1d8b96){var _0x4090bc=path[_0xbf28('0x24')](__dirname,_0xbf28('0x25'),rs[_0xbf28('0x26')](0x4)+_0xbf28('0x27'));fs[_0xbf28('0x28')](_0x4090bc,_0x5279f5,_0xbf28('0x29'));return convertFile(_0x4090bc,_0x1d8b96)[_0xbf28('0x2a')](function(){try{fs[_0xbf28('0x1d')](_0x4090bc);fs[_0xbf28('0x2b')](_0x1d8b96,_0xbf28('0x2c'));}catch(_0x23efd3){logger['error'](_0x23efd3);}return _0x1d8b96[_0xbf28('0x2d')]('.')[0x0];});}function base64Encode(_0x34c157){var _0x318035=fs[_0xbf28('0x2e')](_0x34c157);return new Buffer(_0x318035)[_0xbf28('0x2f')](_0xbf28('0x29'));}exports[_0xbf28('0x30')]=function(_0x3919d4,_0x64a370,_0x50d0f1,_0x5ae302,_0x254341,_0x193f3c){var _0x3d2bbd,_0x352477;var _0x32dd41=_0xbf28('0x31');var _0x55a2db=_0xbf28('0x32')+_0x3919d4;var _0x54f8dd={};if(_0x64a370==='ssml'){_0x54f8dd[_0xbf28('0x33')]={'ssml':_0x50d0f1};}else{_0x54f8dd[_0xbf28('0x33')]={'text':_0x50d0f1};};_0x54f8dd[_0xbf28('0x34')]={'languageCode':_0x5ae302,'ssmlGender':_0x254341};_0x54f8dd[_0xbf28('0x35')]={'audioEncoding':_0xbf28('0x36'),'sampleRateHertz':0x1f40};var _0xe60a40={'url':_0x32dd41+_0xbf28('0x37')+_0x55a2db,'method':'POST','json':!![],'body':_0x54f8dd};return rp(_0xe60a40)['then'](function(_0x195a4c){return new BPromise(function(_0x51ebe0,_0x5e7c75){_0x3d2bbd=path[_0xbf28('0x24')](__dirname,_0xbf28('0x25'),rs['generate'](0x4)+'.mp3');var _0x4e42fa=Buffer[_0xbf28('0x38')](_0x195a4c[_0xbf28('0x39')],_0xbf28('0x29'));return fs[_0xbf28('0x3a')](_0x3d2bbd,_0x4e42fa,function(_0xdc2dfd){_0x5e7c75(_0xdc2dfd);},function(_0x37d836){fs[_0xbf28('0x2b')](_0x3d2bbd,_0xbf28('0x2c'));_0x51ebe0(_0x37d836);});});})['then'](function(_0x488c45){_0x352477=path['resolve'](__dirname,_0xbf28('0x25'),rs['generate'](0x4)+_0xbf28('0x27'));return convertFile(_0x3d2bbd,_0x352477);})['then'](function(){try{fs[_0xbf28('0x1d')](_0x3d2bbd);fs[_0xbf28('0x2b')](_0x352477,'777');}catch(_0x2c009c){logger[_0xbf28('0x1c')](_0x2c009c);}logger[_0xbf28('0x19')](_0xbf28('0x3b'),_0x352477[_0xbf28('0x2d')]('.')[0x0]);return _0x352477[_0xbf28('0x2d')]('.')[0x0];});};exports[_0xbf28('0x3c')]=function(_0x114eb9,_0x512a40){var _0x43a104=_0xbf28('0x3d');var _0x114222,_0x1e6aab;var _0x16732c;var _0x4f495c={'encoding':_0xbf28('0x3e'),'method':_0xbf28('0x3f'),'timeout':0x2710,'url':_0x43a104,'headers':{'Content-Type':'application/x-www-form-urlencoded'},'body':'f.req='+encodeURIComponent(JSON[_0xbf28('0x40')]([[[_0xbf28('0x41'),JSON[_0xbf28('0x40')]([_0x114eb9,_0x512a40||'en',![],'null']),null,_0xbf28('0x42')]]]))};return rp(_0x4f495c)['then'](function(_0x319e8a){try{_0x16732c=eval(_0x319e8a['slice'](0x5))[0x0][0x2];}catch(_0x581eb3){throw new Error('parse\x20response\x20failed:\x0a'+_0x319e8a[_0xbf28('0x20')]);}if(!_0x16732c){if(_0x114eb9[_0xbf28('0x43')]>0xc8){throw new Error(_0xbf28('0x44'));}else{throw new Error(_0xbf28('0x45')+_0x512a40+_0xbf28('0x46'));}}try{_0x16732c=eval(_0x16732c)[0x0];}catch(_0x248716){throw new Error(_0xbf28('0x47')+res['data']);}const _0x326cb2=_0x16732c;_0x114222=path['resolve'](__dirname,_0xbf28('0x25'),rs[_0xbf28('0x26')](0x4)+_0xbf28('0x48'));fs[_0xbf28('0x28')](_0x114222,_0x16732c,{'encoding':_0xbf28('0x29')});_0x1e6aab=path[_0xbf28('0x24')](__dirname,_0xbf28('0x25'),rs[_0xbf28('0x26')](0x4)+'.wav');return convertFile(_0x114222,_0x1e6aab);})[_0xbf28('0x2a')](function(){try{fs[_0xbf28('0x1d')](_0x114222);fs['chmodSync'](_0x1e6aab,_0xbf28('0x2c'));}catch(_0x607ef3){logger[_0xbf28('0x1c')](_0x607ef3);}logger[_0xbf28('0x19')](_0xbf28('0x49'),_0x1e6aab[_0xbf28('0x2d')]('.')[0x0]);return _0x1e6aab[_0xbf28('0x2d')]('.')[0x0];});};exports[_0xbf28('0x4a')]=function(_0x35fe0d,_0x5338c6,_0x708b87){return google['asr'](_0x35fe0d,_0x5338c6,_0x708b87)[_0xbf28('0x2a')](function(_0x366da5){logger[_0xbf28('0x19')](_0x366da5[_0xbf28('0x4b')]);return rp(_0x366da5);})['then'](function(_0x2cf024){var _0x2e467c='';var _0x45b958=0x0;try{fs['unlink'](_0x35fe0d);}catch(_0x2dfb04){logger[_0xbf28('0x1c')](_0x2dfb04);}if(_0x2cf024&&_0x2cf024['results']){_0x2e467c=_0x2cf024[_0xbf28('0x4c')][_0xbf28('0x4d')](function(_0x3a7229){return _0x3a7229&&_0x3a7229[_0xbf28('0x4e')][0x0]&&_0x3a7229[_0xbf28('0x4e')][0x0][_0xbf28('0x4f')]?_0x3a7229[_0xbf28('0x4e')][0x0][_0xbf28('0x4f')]:'';})[_0xbf28('0x50')]('\x0a');_0x45b958=_0x2cf024['results']['map'](function(_0x197c69){return _0x197c69&&_0x197c69[_0xbf28('0x4e')][0x0]&&_0x197c69[_0xbf28('0x4e')][0x0]['confidence']?_0x197c69[_0xbf28('0x4e')][0x0][_0xbf28('0x51')]:0x0;})[_0xbf28('0x50')]('\x0a');}logger[_0xbf28('0x19')]('googleASR\x20transcription',_0x2e467c);logger['info'](_0xbf28('0x52'),_0x45b958);return{'transcript':_0x2e467c,'confidence':_0x45b958};})['catch'](function(_0x1d55b9){logger[_0xbf28('0x1c')](_0xbf28('0x53'),_0x1d55b9['statusCode'],_0x1d55b9['name'],_0x1d55b9[_0xbf28('0x54')]);});};exports[_0xbf28('0x55')]=function(_0x4c18b7,_0x9ae2c,_0x32ea21,_0x4a3253){var _0x151fc9=apiai(_0x9ae2c,{'language':_0x4a3253||'en'});return _0x151fc9['textRequest'](_0x32ea21,{'sessionId':md5(_0x4c18b7)})[_0xbf28('0x2a')](function(_0x586662){logger[_0xbf28('0x19')](JSON[_0xbf28('0x40')](_0x586662));return{'source':_0x586662[_0xbf28('0x56')][_0xbf28('0x57')],'resolvedQuery':_0x586662[_0xbf28('0x56')]['resolvedQuery'],'action':_0x586662[_0xbf28('0x56')][_0xbf28('0x58')],'actionIncomplete':_0x586662['result'][_0xbf28('0x59')],'speech':_0x586662[_0xbf28('0x56')][_0xbf28('0x5a')][_0xbf28('0x5b')],'score':_0x586662[_0xbf28('0x56')]['score'],'statusCode':_0x586662['status'][_0xbf28('0x5c')],'endConversation':_0x586662['result'][_0xbf28('0x5d')]['endConversation']||![],'isFallbackIntent':_0x586662['result']['metadata']['isFallbackIntent']};});};exports['googleDialogflowV2']=function(_0x37685f,_0x8e42e1,_0x582fff,_0x54e3f1,_0x597af8,_0x5839a2){var _0x4c5901=_0x54e3f1[_0xbf28('0x5e')](/\\n/g,'\x0a');var _0x484583={'credentials':{'private_key':_0x4c5901,'client_email':_0x582fff}};var _0x1b043d=new dialogflow[(_0xbf28('0x5f'))](_0x484583);var _0x20c65d=_0x1b043d[_0xbf28('0x60')](_0x8e42e1,md5(_0x37685f));var _0xfd63e1={'session':_0x20c65d,'queryInput':{'text':{'text':_0x597af8,'languageCode':_0x5839a2}}};return _0x1b043d['detectIntent'](_0xfd63e1)[_0xbf28('0x2a')](function(_0x24977a){if(_0x24977a&&_0x24977a['length']){logger['info'](_0xbf28('0x61'),JSON[_0xbf28('0x40')](_0x24977a));var _0x5ab423=_0x24977a[0x0];if(_0x5ab423){var _0x1758bd={'action':_0x5ab423[_0xbf28('0x62')][_0xbf28('0x58')],'allRequiredParamsPresent':_0x5ab423['queryResult'][_0xbf28('0x63')],'endConversation':_0x5ab423['queryResult']['diagnosticInfo']&&_0x5ab423[_0xbf28('0x62')]['diagnosticInfo']['fields']?_0x5ab423[_0xbf28('0x62')]['diagnosticInfo'][_0xbf28('0x64')]['end_conversation'][_0xbf28('0x65')]:![],'fulfillmentText':_0x5ab423[_0xbf28('0x62')][_0xbf28('0x66')],'intentName':_0x5ab423[_0xbf28('0x62')][_0xbf28('0x67')]['name'],'intentDisplayName':_0x5ab423[_0xbf28('0x62')][_0xbf28('0x67')]['displayName'],'isFallbackIntent':_0x5ab423[_0xbf28('0x62')]['intent'][_0xbf28('0x68')],'languageCode':_0x5ab423[_0xbf28('0x62')]['languageCode'],'queryText':_0x5ab423['queryResult']['queryText'],'responseId':_0x5ab423[_0xbf28('0x69')],'score':_0x5ab423['queryResult'][_0xbf28('0x6a')],'speech':_0x5ab423[_0xbf28('0x62')][_0xbf28('0x66')],'providerResponse':JSON['stringify'](_0x24977a)};return _0x1758bd;}}});};exports[_0xbf28('0x6b')]=function(_0x12504a,_0x5839d4,_0x1dd900,_0x450db0){return tilde[_0xbf28('0x6c')](_0x12504a,_0x5839d4,_0x1dd900,_0x450db0)[_0xbf28('0x2a')](function(_0x3cb2ab){try{fs[_0xbf28('0x1d')](_0x12504a);}catch(_0x354470){logger[_0xbf28('0x1c')](_0x354470);}logger[_0xbf28('0x19')]('Tilde\x20ASR\x20transcription',_0x3cb2ab[_0xbf28('0x4f')]);logger[_0xbf28('0x19')](_0xbf28('0x6d'),_0x3cb2ab[_0xbf28('0x51')]);return _0x3cb2ab;});};exports[_0xbf28('0x6e')]=function(_0x1405c1,_0x5a7a04,_0x3bbea3,_0x5340ae,_0x590336,_0x1fa3f8){var _0x214f3e=path[_0xbf28('0x24')](__dirname,_0xbf28('0x25'),rs[_0xbf28('0x26')](0x4)+_0xbf28('0x48'));var _0x3bbfb7;return aws[_0xbf28('0x6f')](_0x1405c1,_0x5a7a04,_0x3bbea3,_0x5340ae,_0x590336,_0x1fa3f8,_0x214f3e)[_0xbf28('0x2a')](function(){_0x3bbfb7=path[_0xbf28('0x24')](__dirname,_0xbf28('0x25'),rs[_0xbf28('0x26')](0x4)+_0xbf28('0x27'));return convertFile(_0x214f3e,_0x3bbfb7);})[_0xbf28('0x2a')](function(){try{fs[_0xbf28('0x1d')](_0x214f3e);fs['chmodSync'](_0x3bbfb7,_0xbf28('0x2c'));}catch(_0x51a2d9){logger[_0xbf28('0x1c')](_0x51a2d9);}return _0x3bbfb7[_0xbf28('0x2d')]('.')[0x0];});};exports[_0xbf28('0x70')]=function(_0x246c6d,_0x186797,_0x1cb16e,_0x484965,_0xe20bf7,_0xc67b79){return aws[_0xbf28('0x71')](_0x246c6d,_0x186797,_0x1cb16e,_0x484965,_0xe20bf7,_0xc67b79)[_0xbf28('0x2a')](function(_0x5b2214){logger[_0xbf28('0x19')](JSON[_0xbf28('0x40')](_0x5b2214));return _0x5b2214;});};exports[_0xbf28('0x72')]=function(_0x42a361,_0x175aaf,_0x437a60,_0x3f829a){var _0x506658=util[_0xbf28('0x73')](_0xbf28('0x74'),ISPEECH_API,_0x42a361,encodeURIComponent(_0x175aaf),_0x437a60,_0x3f829a);var _0x49f42c=path[_0xbf28('0x24')](__dirname,'tmp',rs['generate'](0x4)+'.mp3');var _0x2d8d6e;return downloadFile(_0x506658,_0x49f42c)[_0xbf28('0x2a')](function(){_0x2d8d6e=path[_0xbf28('0x24')](__dirname,'tmp',rs[_0xbf28('0x26')](0x4)+_0xbf28('0x27'));return convertFile(_0x49f42c,_0x2d8d6e);})[_0xbf28('0x2a')](function(){try{fs[_0xbf28('0x1d')](_0x49f42c);fs[_0xbf28('0x2b')](_0x2d8d6e,_0xbf28('0x2c'));}catch(_0x12a97a){logger[_0xbf28('0x1c')](_0x12a97a);}return _0x2d8d6e[_0xbf28('0x2d')]('.')[0x0];});};exports[_0xbf28('0x75')]=function(_0xe6df50,_0x14f7e2,_0x57d34b,_0x24f719){var _0x28efec=util[_0xbf28('0x73')]('%s?apikey=%s&action=recognize&locale=%s&content-type=audio/x-wav&audio=%s&%s',ISPEECH_API,_0x14f7e2,_0x57d34b,base64Encode(_0xe6df50),_0x24f719?util['format'](_0xbf28('0x76'),encodeURIComponent(_0x24f719)):_0xbf28('0x77'));return uploadFile(_0x28efec)['then'](function(_0x5d1505){return querystring[_0xbf28('0x78')](_0x5d1505);});};exports['sestekNDA']=function(_0x5c7a65,_0x42273a){return sestek[_0xbf28('0x79')](_0x5c7a65,_0x42273a)['then'](function(_0x36df38){logger['info'](JSON[_0xbf28('0x40')](_0x36df38));var _0x70bca0={'speech':_0x36df38['text'],'action':_0x36df38[_0xbf28('0x7a')]?_0x36df38['channelData'][_0xbf28('0x7b')]:undefined,'actiondata':_0x36df38[_0xbf28('0x7a')]?_0x36df38[_0xbf28('0x7a')]['CustomActionData']:undefined};if(_0x36df38[_0xbf28('0x7a')]&&_0x36df38[_0xbf28('0x7a')][_0xbf28('0x7c')]){return base64Decode(_0x36df38[_0xbf28('0x7a')]['AudioFromTts']['Data'],path[_0xbf28('0x24')](__dirname,_0xbf28('0x25'),rs[_0xbf28('0x26')](0x4)+_0xbf28('0x27')))[_0xbf28('0x2a')](function(_0xd51850){_0x70bca0['audiofile']=_0xd51850;return _0x70bca0;});}return _0x70bca0;});};exports[_0xbf28('0x7d')]=function(){return __dirname;};