본문 바로가기
FrameWork

[coolSMS] 문자보내기 서비스 이용

by mansoorrr 2024. 9. 29.

웹 서비스나 어플 서비스에서 문자를 보내야 하는 경우가 있다.(회원가입 인증, 이메일 확인 요청 등)

대표적으로 많이 사용 하는 플랫폼이 coolSMS이다.

 

coolsms에서는 문자를 보내는 방법을 restapi방식과 oauth2 방식 으로 구분하여 제공한다.

그중 restapi방식을 알아본다.

 

 

COOLSMS - 알림톡과 문자메시지 발송, 최고의 안정성

 

console.coolsms.co.kr

 

[사용방법]

1. 회원가입

 

2. apikey 발급

  • 상위 navigation bar에서 개발/연동 > apikey 관리로 들어간다
  • apikey 신청하여 발급 받음
  • apikey와 api-secretkey를 확인 및 별도 저장

 

3. 적용(spring 기준으로 설명)

  • apikey를 발급받기 위한 nav바에서 개발/연동 > API개발문서로 들어감
  • DEVELOPMENT-KITS에서 JAVA 로 이동
  • 해당 github으로 이동
 

GitHub - coolsms/coolsms-java-examples: CoolSMS Java/Kotlin SDK 예제 목록

CoolSMS Java/Kotlin SDK 예제 목록. Contribute to coolsms/coolsms-java-examples development by creating an account on GitHub.

github.com

 

  • 의존성 추가: github에 나와있음(gradle, maven 에 따라서 어떻게 하는지 설명 되어 있음) gradle 기준으로 설명

 

  • github에서 제공하는 실제 사용 예제(ExampleController)들 중 필요한 부분 골라서 class로 만들어서 사용(메세지 보내는 부분만 설명)
    • apikey, apisecretkey를 application.properties에 명시해 줄것
    • 새로 선언하는 controller에서 apikey, apisecretkey를 불러와서 사용
@RestController
public class ExampleController {

    final DefaultMessageService messageService;


    // application.properties에 선언한 apikey와, apiSecretKey를 받아 사용
    public ExampleController(
    		@Value("${apiKey}") String apiKey, 
            @Value("${apiSecretKey}") String apiSecretKey
    	) {
        // 반드시 계정 내 등록된 유효한 API 키, API Secret Key를 입력해주셔야 합니다!
        this.messageService = NurigoApp.INSTANCE.initialize(apiKey, apiSecretKey, "https://api.coolsms.co.kr");
    }
    
   
    @PostMapping("/send-one")
    public SingleMessageSentResponse sendOne() {
        Message message = new Message();        
        message.setFrom("발신번호 입력"); // 발신번호 및 수신번호는 반드시 01012345678 형태로 입력되어야 합니다.
        message.setTo("수신번호 입력");
        message.setText("한글 45자, 영자 90자 이하 입력되면 자동으로 SMS타입의 메시지가 추가됩니다.");

        SingleMessageSentResponse response = this.messageService.sendOne(new SingleMessageSendingRequest(message));
        System.out.println(response);

        return response;
    }    
}

 

  • 랜덤코드 생성: 랜덤코드 생성해서 유저에게 휴대폰인증 등을 할 수 있음
public static String RandomNumber() {
	Random random = new Random();
    int randomNum = random.nextInt(1000000); // 1000000 미만인 숫자
    DecimalFormat format = new DecimalFormat("000000"); // 숫자 포맷 지정
    return format.format(randomNum);
}

'FrameWork' 카테고리의 다른 글

[이미지업로드] 미리보기  (0) 2024.10.27
[동기&비동기] async await  (2) 2024.10.11
[firebase] 파일 업로드  (3) 2024.09.29