From 60c952c5766afe39893cc8885916a22b19812d8c Mon Sep 17 00:00:00 2001 From: AmruthVamshi Date: Thu, 12 Dec 2024 18:16:21 +0530 Subject: [PATCH 1/2] updated gupshup API for international numbers --- src/api/api.controller.ts | 2 +- .../sms/gupshupWhatsapp/gupshupWhatsapp.service.ts | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/api/api.controller.ts b/src/api/api.controller.ts index 2d02fb1..97f1926 100644 --- a/src/api/api.controller.ts +++ b/src/api/api.controller.ts @@ -120,7 +120,7 @@ export class ApiController { template: null, type: null, params: null - }); + },countryCode); } else { status = await this.otpService.sendOTP(params.phone); } diff --git a/src/api/sms/gupshupWhatsapp/gupshupWhatsapp.service.ts b/src/api/sms/gupshupWhatsapp/gupshupWhatsapp.service.ts index b92563b..36e7470 100644 --- a/src/api/sms/gupshupWhatsapp/gupshupWhatsapp.service.ts +++ b/src/api/sms/gupshupWhatsapp/gupshupWhatsapp.service.ts @@ -12,7 +12,7 @@ export class GupshupWhatsappService { ) { } - async sendWhatsappOTP(smsData: SMSData): Promise { + async sendWhatsappOTP(smsData: SMSData, countryCode: string): Promise { const status: SMSResponse = { providerResponseCode: null, status: SMSResponseStatus.failure, @@ -26,7 +26,6 @@ export class GupshupWhatsappService { // Generate 4 digit OTP const otp = Math.floor(1000 + Math.random() * 9000); - try { // First opt-in the user const optInParams = new URLSearchParams(); @@ -34,7 +33,7 @@ export class GupshupWhatsappService { optInParams.append("format", "text"); optInParams.append("userid", process.env.GUPSHUP_WHATSAPP_USERID); optInParams.append("password", process.env.GUPSHUP_WHATSAPP_PASSWORD); - optInParams.append("phone_number", `91${smsData.phone}`); + optInParams.append("phone_number", `${countryCode}${smsData.phone}`); optInParams.append("v", "1.1"); optInParams.append("auth_scheme", "plain"); optInParams.append("channel", "WHATSAPP"); @@ -48,19 +47,19 @@ export class GupshupWhatsappService { }); // Then send OTP message - const otpMessage = `${otp} is your verification code. For your security, do not share this code.`; + const otpMessage = process.env.GUPSHUP_WHATSAPP_OTP_TEMPLATE.replace('%otp%',`${otp}`); const sendOtpParams = new URLSearchParams(); sendOtpParams.append("method", "SENDMESSAGE"); sendOtpParams.append("userid", process.env.GUPSHUP_WHATSAPP_USERID); sendOtpParams.append("password", process.env.GUPSHUP_WHATSAPP_PASSWORD); - sendOtpParams.append("send_to", smsData.phone); + sendOtpParams.append("send_to", `${countryCode}${smsData.phone}`); sendOtpParams.append("v", "1.1"); sendOtpParams.append("format", "json"); sendOtpParams.append("msg_type", "TEXT"); sendOtpParams.append("msg", otpMessage); sendOtpParams.append("isTemplate", "true"); - sendOtpParams.append("footer", "This code expires in 30 minute."); + sendOtpParams.append("footer", process.env.GUPSHUP_WHATSAPP_OTP_TEMPLATE_FOOTER); let sendOtpURL = process.env.GUPSHUP_WHATSAPP_BASEURL + '?' + sendOtpParams.toString(); @@ -76,7 +75,6 @@ export class GupshupWhatsappService { status.providerSuccessResponse = JSON.stringify(response.data); status.status = SMSResponseStatus.success; - status.messageID = otp.toString(); } return status; From 24d50ef26fa1fb4d1e2eba4c5f06262199de48f7 Mon Sep 17 00:00:00 2001 From: AmruthVamshi Date: Thu, 12 Dec 2024 18:19:16 +0530 Subject: [PATCH 2/2] added event log for sendOTP --- src/api/api.controller.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api/api.controller.ts b/src/api/api.controller.ts index 97f1926..2d0b6d5 100644 --- a/src/api/api.controller.ts +++ b/src/api/api.controller.ts @@ -132,7 +132,8 @@ export class ApiController { orgId: params.orgId, timeTaken: Date.now() - startTime, createdAt: Math.floor(new Date().getTime() / 1000), - phoneNumber: params.phone + phoneNumber: params.phone, + eventLog: `Response from OTP provider - ${status.providerSuccessResponse}` }, 'E117', 'Send OTP',