1
1
import { Injectable , Logger } from '@nestjs/common' ;
2
- import { GSWhatsAppMessage , convertMessageToXMsg , convertXMessageToMsg , gupshupWhatsappAdapterServiceConfig } from '@samagra-x/gupshup-whatsapp-adapter' ;
2
+ import {
3
+ GSWhatsAppMessage ,
4
+ convertMessageToXMsg ,
5
+ convertXMessageToMsg ,
6
+ gupshupWhatsappAdapterServiceConfig
7
+ } from '@samagra-x/gupshup-whatsapp-adapter' ;
3
8
import { ConfigService } from '@nestjs/config' ;
4
9
import axios from 'axios' ;
5
- import { url } from 'inspector' ;
10
+ import { v4 as uuid4 } from 'uuid' ;
11
+
12
+ import { OutboundService } from '../outbound/outbound.service' ;
13
+ import { XMessage , MessageType , MessageState } from '@samagra-x/xmessage' ;
6
14
7
15
@Injectable ( )
8
16
export class InboundService {
9
- constructor ( private configService : ConfigService ) { }
10
- private readonly logger = new Logger ( InboundService . name )
17
+ constructor (
18
+ private configService : ConfigService ,
19
+ private readonly outboundService : OutboundService
20
+ ) { }
21
+ private readonly logger = new Logger ( InboundService . name ) ;
22
+
23
+ convertApiResponseToXMessage ( data : any , phoneNumber ) : XMessage {
24
+ return {
25
+ adapterId : '7b0cf232-38a2-4f9b-8070-9b988ff94c2c' ,
26
+ messageType : data . messageType ,
27
+ messageId : data . messageId ,
28
+ to : { userID : phoneNumber } ,
29
+ from : { userID : 'admin' } ,
30
+ channelURI : data . channelURI ,
31
+ providerURI : data . providerURI ,
32
+ timestamp : data . timestamp ,
33
+ messageState : MessageState . REPLIED ,
34
+ payload : data . payload
35
+ } ;
36
+ }
37
+
11
38
async handleIncomingGsWhatsappMessage ( whatsappMessage : GSWhatsAppMessage ) {
12
39
gupshupWhatsappAdapterServiceConfig . setConfig ( {
13
40
baseUrl : this . configService . get < string > ( 'BASE_URL' ) ,
14
41
adminToken : this . configService . get < string > ( 'ADAPTER_ADMIN_TOKEN' ) ,
15
42
vaultServiceToken : this . configService . get < string > ( 'VAULT_SERVICE_TOKEN' ) ,
16
43
vaultServiceUrl : this . configService . get < string > ( 'VAULT_SERVICE_URL' ) ,
17
44
gupshupUrl : this . configService . get < string > ( 'GUPSHUP_API_ENDPOINT' )
18
- } )
19
-
20
- const xMessagePayload = await convertMessageToXMsg ( whatsappMessage )
21
- this . logger . log ( xMessagePayload )
22
- const orchestratorServiceUrl = this . configService . get < string > ( 'ORCHESTRATOR_API_ENDPOINT' )
23
- const resp = await axios . post ( orchestratorServiceUrl , xMessagePayload , {
24
- headers : {
25
- 'Content-Type' : 'application/json' ,
45
+ } ) ;
46
+ try {
47
+ if ( "interactive" in whatsappMessage ) {
48
+ const interactiveInteraction = JSON . parse ( whatsappMessage . interactive )
49
+ if ( interactiveInteraction . type = 'button_reply' ) {
50
+ //handle feedback
51
+ this . logger . log ( "Feedback is not being handled right now!" )
52
+ return
26
53
}
27
- } )
28
- //log to supabase
29
- //handle errors
30
- }
54
+ }
55
+ const xMessagePayload = await convertMessageToXMsg ( whatsappMessage ) ;
56
+ xMessagePayload . from . userID = uuid4 ( ) ;
57
+ xMessagePayload . to . userID = uuid4 ( ) ;
58
+ xMessagePayload . messageId . Id = uuid4 ( ) ;
59
+
60
+ const orchestratorServiceUrl = this . configService . get < string > ( 'ORCHESTRATOR_API_ENDPOINT' ) ;
61
+ const resp = await axios . post ( orchestratorServiceUrl , xMessagePayload , {
62
+ headers : {
63
+ 'Content-Type' : 'application/json'
64
+ }
65
+ } ) ;
31
66
32
- }
67
+ const xResponse = this . convertApiResponseToXMessage ( resp . data , whatsappMessage . mobile . substring ( 2 ) ) ;
68
+ const sentResp = await this . outboundService . handleOrchestratorResponse ( xResponse ) ;
69
+ } catch ( error ) {
70
+ const errorResponse = this . convertApiResponseToXMessage (
71
+ {
72
+ adapterId : '7b0cf232-38a2-4f9b-8070-9b988ff94c2c' ,
73
+ messageType : MessageType . TEXT ,
74
+ messageId : { } ,
75
+ from : { userID : 'admin' } ,
76
+ channelURI : 'WhatsApp' ,
77
+ providerURI : 'gupshup' ,
78
+ timestamp : Date . now ( ) ,
79
+ messageState : MessageState . REPLIED ,
80
+ payload : { text : 'Something went wrong. Please try again later.' }
81
+ } ,
82
+ whatsappMessage . mobile . substring ( 2 )
83
+ ) ;
84
+ await this . outboundService . handleOrchestratorResponse ( errorResponse ) ;
85
+ }
86
+ }
87
+ }
0 commit comments