CoolFace
Apppublic

sakthivikram/geo-location

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
Fast2SmsService.java62 linesDownload Raw Back to service
1package com.georeport.service;2 3import org.slf4j.Logger;4import org.slf4j.LoggerFactory;5import org.springframework.beans.factory.annotation.Autowired;6import org.springframework.beans.factory.annotation.Value;7import org.springframework.context.annotation.Primary;8import org.springframework.stereotype.Service;9import org.springframework.web.client.RestTemplate;10import org.springframework.http.ResponseEntity;11 12import java.util.regex.Matcher;13import java.util.regex.Pattern;14 15@Service16@Primary17public class Fast2SmsService implements SmsService {18    private static final Logger logger = LoggerFactory.getLogger(Fast2SmsService.class);19 20    @Value("${fast2sms.api.key:YOUR_FAST2SMS_API_KEY}")21    private String apiKey;22 23    @Autowired24    private SmsBroadcastService smsBroadcastService;25    26    private final RestTemplate restTemplate = new RestTemplate();27 28    @Override29    public void sendSms(String to, String messageContent) {30        // Always broadcast for Dev Dashboard testing31        smsBroadcastService.broadcastSms(to, messageContent);32        33        if (apiKey == null || apiKey.isEmpty() || apiKey.equals("YOUR_FAST2SMS_API_KEY") || apiKey.contains("YOUR_")) {34            logger.info("FAST2SMS SIMULATION: To: {}, Content: {}", to, messageContent);35            return;36        }37 38        try {39            // Clean phone number (expecting exactly 10 digits for India without +91)40            String cleanPhone = to.replaceAll("[^0-9]", "");41            if (cleanPhone.length() > 10) {42                cleanPhone = cleanPhone.substring(cleanPhone.length() - 10);43            }44            45            // Extract the exact 6 digit OTP from the messageContent to send via OTP route46            Matcher m = Pattern.compile("\\b(\\d{6})\\b").matcher(messageContent);47            String otpCode = m.find() ? m.group(1) : "123456";48 49            // Using route=otp (best chance to deliver securely via Fast2SMS)50            String url = "https://www.fast2sms.com/dev/bulkV2?authorization=" + apiKey + 51                         "&route=otp&variables_values=" + otpCode + "&flash=0&numbers=" + cleanPhone;52 53            // Simple GET Request using Spring RestTemplate54            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);55            logger.info("Fast2SMS Response: {}", response.getBody());56 57        } catch (Exception e) {58            logger.error("Failed to send Fast2SMS to {}: {}", to, e.getMessage());59        }60    }61}62