Skip to content

Commit

Permalink
Merge pull request #109 from Amruth-Vamshi/feature/WA-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ChakshuGautam authored Dec 12, 2024
2 parents 2f35da9 + 24d50ef commit 2156d3d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/api/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class ApiController {
template: null,
type: null,
params: null
});
},countryCode);
} else {
status = await this.otpService.sendOTP(params.phone);
}
Expand All @@ -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',
Expand Down
12 changes: 5 additions & 7 deletions src/api/sms/gupshupWhatsapp/gupshupWhatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class GupshupWhatsappService {
) {
}

async sendWhatsappOTP(smsData: SMSData): Promise<SMSResponse> {
async sendWhatsappOTP(smsData: SMSData, countryCode: string): Promise<SMSResponse> {
const status: SMSResponse = {
providerResponseCode: null,
status: SMSResponseStatus.failure,
Expand All @@ -26,15 +26,14 @@ 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();
optInParams.append("method", "OPT_IN");
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");
Expand All @@ -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();

Expand All @@ -76,7 +75,6 @@ export class GupshupWhatsappService {

status.providerSuccessResponse = JSON.stringify(response.data);
status.status = SMSResponseStatus.success;
status.messageID = otp.toString();
}

return status;
Expand Down

0 comments on commit 2156d3d

Please sign in to comment.