@@ -2,7 +2,7 @@ import { IHttp, IHttpRequest, IModify, IPersistence, IRead } from '@rocket.chat/
22import { IRoom } from '@rocket.chat/apps-engine/definition/rooms' ;
33import { createSign } from 'crypto' ;
44import { AppSetting } from '../config/Settings' ;
5- import { DialogflowJWT , DialogflowUrl , IDialogflowAccessToken , IDialogflowEvent , IDialogflowMessage , IDialogflowQuickReplies , LanguageCode } from '../enum/Dialogflow' ;
5+ import { DialogflowJWT , DialogflowRequestType , DialogflowUrl , IDialogflowAccessToken , IDialogflowEvent , IDialogflowMessage , IDialogflowQuickReplies , LanguageCode } from '../enum/Dialogflow' ;
66import { Headers } from '../enum/Http' ;
77import { Logs } from '../enum/Logs' ;
88import { base64urlEncode } from './Helper' ;
@@ -12,43 +12,27 @@ import { getAppSettingValue } from './Settings';
1212
1313class DialogflowClass {
1414 private jwtExpiration : Date ;
15- public async sendMessage ( http : IHttp ,
15+ public async sendRequest ( http : IHttp ,
1616 read : IRead ,
1717 modify : IModify ,
1818 sessionId : string ,
19- messageText : string ) : Promise < IDialogflowMessage > {
19+ request : IDialogflowEvent | string ,
20+ requestType : DialogflowRequestType ) : Promise < IDialogflowMessage > {
2021 const serverURL = await this . getServerURL ( read , modify , http , sessionId ) ;
2122
23+ const queryInput = requestType === DialogflowRequestType . EVENT ?
24+ { event : { ...( request as IDialogflowEvent ) } } :
25+ { text : { languageCode : LanguageCode . EN , text : request } } ;
2226 const httpRequestContent : IHttpRequest = createHttpRequest (
2327 { 'Content-Type' : Headers . CONTENT_TYPE_JSON , 'Accept' : Headers . ACCEPT_JSON } ,
24- { queryInput : { text : { languageCode : LanguageCode . EN , text : messageText } } } ,
28+ { queryInput } ,
2529 ) ;
2630
2731 try {
2832 const response = await http . post ( serverURL , httpRequestContent ) ;
2933 return this . parseRequest ( response . data ) ;
3034 } catch ( error ) {
31- throw new Error ( Logs . HTTP_REQUEST_ERROR ) ;
32- }
33- }
34-
35- public async sendEvent ( http : IHttp ,
36- read : IRead ,
37- modify : IModify ,
38- sessionId : string ,
39- event : IDialogflowEvent ) : Promise < IDialogflowMessage > {
40- const serverURL = await this . getServerURL ( read , modify , http , sessionId ) ;
41-
42- const httpRequestContent : IHttpRequest = createHttpRequest (
43- { 'Content-Type' : Headers . CONTENT_TYPE_JSON , 'Accept' : Headers . ACCEPT_JSON } ,
44- { queryInput : { event } } ,
45- ) ;
46-
47- try {
48- const response = await http . post ( serverURL , httpRequestContent ) ;
49- return this . parseRequest ( response . data ) ;
50- } catch ( error ) {
51- throw new Error ( Logs . HTTP_REQUEST_ERROR ) ;
35+ throw new Error ( `${ Logs . HTTP_REQUEST_ERROR } ` ) ;
5236 }
5337 }
5438
0 commit comments