@@ -8,14 +8,16 @@ import { OutboundService } from '../outbound/outbound.service';
8
8
import { XMessage , MessageType , MessageState } from '@samagra-x/xmessage' ;
9
9
import { CredentialService } from '../credentials/credentials.service' ;
10
10
import { UserService } from 'src/user/user.service' ;
11
+ import { FeedbackService } from '../feedback/feedback.service' ;
11
12
12
13
@Injectable ( )
13
14
export class GupshupWhatsappInboundService {
14
15
constructor (
15
- private configService : ConfigService ,
16
+ private readonly configService : ConfigService ,
16
17
private readonly userService : UserService ,
17
18
private readonly outboundService : OutboundService ,
18
- private readonly credentialService : CredentialService
19
+ private readonly credentialService : CredentialService ,
20
+ private readonly feedbackService : FeedbackService
19
21
) { }
20
22
private readonly logger = new Logger ( GupshupWhatsappInboundService . name ) ;
21
23
@@ -69,48 +71,98 @@ export class GupshupWhatsappInboundService {
69
71
70
72
async handleIncomingGsWhatsappMessage ( whatsappMessage : GSWhatsAppMessage ) {
71
73
const adapterCredentials = await this . getAdapterCredentials ( whatsappMessage . waNumber ) ;
72
- this . logger . log ( "Obtained Creds" )
74
+
73
75
try {
74
- //Handle Feedback First
75
- if ( 'interactive' in whatsappMessage ) {
76
- const interactiveInteraction = JSON . parse ( whatsappMessage . interactive ) ;
77
- if ( ( interactiveInteraction . type = 'button_reply' ) ) {
78
- //handle feedback
79
- this . logger . log ( 'Feedback is not being handled right now!' ) ;
80
- return ;
76
+ //Handle Feedback
77
+ const thumbsUpEmojis = [ '👍' , '👍🏻' , '👍🏼' , '👍🏽' , '👍🏾' , '👍🏿' ] ;
78
+ const thumbsDownEmojis = [ '👎' , '👎🏻' , '👎🏼' , '👎🏽' , '👎🏾' , '👎🏿' ] ;
79
+ if (
80
+ 'replyId' in whatsappMessage &&
81
+ 'text' in whatsappMessage &&
82
+ 'messageId' in whatsappMessage &&
83
+ ( thumbsUpEmojis . includes ( whatsappMessage . text ) || thumbsDownEmojis . includes ( whatsappMessage . text ) )
84
+ ) {
85
+ if ( thumbsUpEmojis . includes ( whatsappMessage . text ) ) {
86
+ this . feedbackService . givePositiveFeedback ( whatsappMessage . messageId ) ;
87
+ this . logger . log ( 'Message ID' , whatsappMessage . messageId ) ;
88
+ } else if ( thumbsDownEmojis . includes ( whatsappMessage . text ) ) {
89
+ this . feedbackService . giveNegativeFeedback ( whatsappMessage . messageId ) ;
90
+ this . logger . log ( 'Message ID' , whatsappMessage . messageId ) ;
81
91
}
92
+ return ;
82
93
}
83
94
84
95
const xMessagePayload : XMessage = await convertMessageToXMsg ( whatsappMessage ) ;
85
96
if ( xMessagePayload . messageType != MessageType . TEXT ) {
86
- throw new Error ( " Media Type Not Supported" ) ;
97
+ throw new Error ( ' Media Type Not Supported' ) ;
87
98
}
88
99
89
- this . logger . log ( " Converted Message:" , xMessagePayload )
100
+ this . logger . log ( ' Converted Message:' , xMessagePayload ) ;
90
101
xMessagePayload . from . userID = uuid4 ( ) ;
91
102
xMessagePayload . to . userID = uuid4 ( ) ;
92
103
xMessagePayload . messageId . Id = uuid4 ( ) ;
93
104
94
105
xMessagePayload . to . bot = true ;
95
106
xMessagePayload . to . meta = xMessagePayload . to . meta || new Map < string , string > ( ) ;
96
107
xMessagePayload . to . meta . set ( 'botMobileNumber' , whatsappMessage . waNumber ) ;
97
- console . log ( xMessagePayload )
108
+
109
+ xMessagePayload . from . bot = false ;
110
+ xMessagePayload . from . meta = xMessagePayload . from . meta || new Map < string , string > ( ) ;
111
+ xMessagePayload . from . meta . set ( 'mobileNumber' , whatsappMessage . mobile . substring ( 2 ) ) ;
112
+
113
+ //Send Template response before the actuar response because the actual response takes time
114
+ const templateResponse = this . convertApiResponseToXMessage (
115
+ {
116
+ adapterId : '7b0cf232-38a2-4f9b-8070-9b988ff94c2c' ,
117
+ messageType : MessageType . TEXT ,
118
+ messageId : { } ,
119
+ from : { userID : 'admin' } ,
120
+ channelURI : 'WhatsApp' ,
121
+ providerURI : 'gupshup' ,
122
+ timestamp : Date . now ( ) ,
123
+ messageState : MessageState . REPLIED ,
124
+ payload : {
125
+ text : 'Thank you for your question! Our chatbot is working diligently to provide you with the best possible answer. Generating responses may take a moment, so please be patient.'
126
+ }
127
+ } ,
128
+ whatsappMessage . mobile . substring ( 2 )
129
+ ) ;
130
+
131
+ const tempResp = await this . outboundService . handleOrchestratorResponse (
132
+ templateResponse ,
133
+ adapterCredentials
134
+ ) ;
98
135
99
136
const orchestratorServiceUrl = this . configService . get < string > ( 'ORCHESTRATOR_API_ENDPOINT' ) ;
100
- const resp = await axios . post ( `${ orchestratorServiceUrl } /prompt` , xMessagePayload , {
137
+
138
+
139
+ // Map<string,string>() cannot be sent in request body
140
+ const payload = JSON . parse ( JSON . stringify ( xMessagePayload ) ) ;
141
+
142
+ xMessagePayload . to . meta . forEach ( ( val : string , key : string ) => {
143
+ payload . to . meta [ key ] = val ;
144
+ } ) ;
145
+
146
+ xMessagePayload . from . meta . forEach ( ( val : string , key : string ) => {
147
+ payload . from . meta [ key ] = val ;
148
+ } ) ;
149
+ const resp = await axios . post ( `${ orchestratorServiceUrl } /prompt` , payload , {
101
150
headers : {
102
151
'Content-Type' : 'application/json'
103
152
}
104
153
} ) ;
105
- this . logger . log ( 'OrchestratorResponse' , resp . data )
154
+
155
+ this . logger . log ( 'OrchestratorResponse' , resp . data ) ;
156
+ //remove after this with active outbound
106
157
const xResponse = this . convertApiResponseToXMessage ( resp . data , whatsappMessage . mobile . substring ( 2 ) ) ;
107
- this . logger . log ( " OrchestratorResponse" , xResponse )
158
+ this . logger . log ( ' OrchestratorResponse' , xResponse ) ;
108
159
const sentResp = await this . outboundService . handleOrchestratorResponse ( xResponse , adapterCredentials ) ;
109
- this . logger . log ( "OutboundResponse" , sentResp )
160
+ this . logger . log ( 'OutboundResponse' , sentResp ) ;
161
+
110
162
} catch ( error ) {
111
- let errorText = 'Something went wrong. Please try again later'
112
- if ( error == 'Error: Media Type Not Supported' ) {
113
- errorText = `Sorry, I can only respond to text-based questions at the moment. Please type your question using regular text characters, and I'll be happy to help!\n\nThank you for your understanding!`
163
+ let errorText = 'Something went wrong. Please try again later' ;
164
+ if ( error == 'Error: Media Type Not Supported' ) {
165
+ errorText = `Sorry, I can only respond to text-based questions at the moment. Please type your question using regular text characters, and I'll be happy to help!\n\nThank you for your understanding!` ;
114
166
}
115
167
const errorResponse = this . convertApiResponseToXMessage (
116
168
{
0 commit comments