๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ ๋ฐฑ์—”๋“œ/SpringBoot

[์ธ์ฆ๋ฒˆํ˜ธ] sms ๋ฌธ์ž ๋ฐœ์†ก ๊ตฌํ˜„ - ์ฟจ์—์Šค์— ์—์Šค (2024)

by OR15A 2024. 2. 29.
  • ์ผ๊ฒฝํ—˜ ๋งˆ์ง€๋ง‰ API ๊ณผ์ œ ์ค‘ ํ•˜๋‚˜์˜€๋˜ ๋ฌธ์ž๋กœ ํšŒ์›๊ฐ€์ž… ์ธ์ฆ๋ฒˆํ˜ธ ๋ฐœ์†กํ•˜๊ธฐ 
  • ์—ฌ๋Ÿฌ ๋ฌธ์ž๋ฉ”์„ธ์ง€ ๋ฐœ์†ก ์‚ฌ์ดํŠธ์ค‘์— ๋ฌด๋ฃŒ 300ํฌ์ธํŠธ๋ฅผ ์ฃผ๋Š” ์ฟจ์—์Šค์— ์—์Šค๋ฅผ ์„ ํƒํ•จ
  • https://coolsms.co.kr/
  • ๊ตฌ๊ธ€๋ง์œผ๋กœ ์ฐพ์€ ์ฝ”๋“œ๋“ค์€ ํ˜„์žฌ ์ฟจ์—์Šค์— ์—์Šค์—์„œ ์˜ฌ๋ฆฐ ๊นƒ ์ฝ”๋“œ์™€ ์•ฝ๊ฐ„์˜ ์ฐจ์ด๊ฐ€ ์žˆ์–ด์„œ ์ฟจ์—์Šค์— ์—์Šค์˜ ๊นƒํ—ˆ๋ธŒ๋ฅผ ์ฐธ๊ณ ํ•œ ์ฝ”๋“œ๋กœ ๊ธ€์„ ์ž‘์„ฑํ•จ

 

์ฟจ์—์Šค์— ์—์Šค ํšŒ์›๊ฐ€์ž… ๋ฐ ๋ณธ์ธ์ธ์ฆ ํ•˜๊ธฐ
  • ๋ณธ์ธ์ธ์ฆ์‹œ 300ํฌ์ธํŠธ ๋ฌด๋ฃŒ ์ฆ์ •
  • ์นด์นด์˜คํ†ก ์•Œ๋ฆผ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•จ

์ฟจ์—์Šค์— ์—์Šค ๋Œ€์‹œ๋ณด๋“œ

 

 

API Key์™€ API ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธํ•˜๊ธฐ

API Key
API Secret

 

 

 

๋Œ€์‹œ๋ณด๋“œ - ๊ฐœ๋ฐœ ํƒญ์—์„œ SDK ์™€ git repository ํ™•์ธํ•˜๊ธฐ

SDK ์™€  git repository

        <dependency>
            <groupId>net.nurigo</groupId>
            <artifactId>sdk</artifactId>
            <version>4.3.0</version>
        </dependency>

 

 

 

์Šคํ”„๋ง ์ฝ”๋“œ
    private final DefaultMessageService messageService;


    /**
     * ๋‹จ์ผ ๋ฉ”์‹œ์ง€ ๋ฐœ์†ก
     */
    @PostMapping("/sendSMS")
    @ResponseBody
    public ResponseEntity<?> sendSMS(@RequestParam("phoneNumber") String phoneNumber) {

        if (phoneNumber == null || phoneNumber.trim().isEmpty()) {
            // ์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ, ์ ์ ˆํ•œ HTTP ์‘๋‹ต ๋ฐ˜ํ™˜
            return ResponseEntity.badRequest().body("์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.");
        }

        Message message = new Message();

        String numStr = makeRandomNumStr();

        message.setFrom("01011111111"); //์•Œ๋งž๊ฒŒ ์ˆ˜์ •ํ•˜๊ธฐ
        message.setTo(phoneNumber); //์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ์ „ํ™”๋ฒˆํ˜ธ(ํ…Œ์ŠคํŠธ๋Š” ๊ฐœ์ธ๋ฒˆํ˜ธ๋กœ)
        message.setText("์ธ์ฆ๋ฒˆํ˜ธ๋Š” [" + numStr + "] ์ž…๋‹ˆ๋‹ค.");

        //phoneNumber ์™€  numStr Repository์— ์ €์žฅํ•˜๊ธฐ
        confirmRepository.insertConfirmData(new ConfirmData("sms", phoneNumber, numStr));
        //์ด๋ฉ”์ผ ์ธ์ฆ๋ฒˆํ˜ธ๋„ ๊ฐ™์€ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ type ๋ช…์‹œ
        

        // ๋ฉ”์‹œ์ง€ ๋ฐœ์†ก ์‹œ๋„ ๋ฐ ์‘๋‹ต ์ฒ˜๋ฆฌ
        try {
            SingleMessageSentResponse response = this.messageService.sendOne(new SingleMessageSendingRequest(message));
            System.out.println(response);
            return ResponseEntity.ok(response); // ์„ฑ๊ณต์ ์œผ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒˆ์„ ๊ฒฝ์šฐ, ์‘๋‹ต ๋ฐ˜ํ™˜
        } catch (Exception e) {
            // ๋ฉ”์‹œ์ง€ ๋ฐœ์†ก ์ค‘ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ
            System.out.println(e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("๋ฉ”์‹œ์ง€ ๋ฐœ์†ก์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.");
        }
    }
    
    
    
        /**
     * ๋žœ๋ค6์ž๋ฆฌ ์ˆซ์žString
     * */
    private String makeRandomNumStr(){
        Random rand = new Random();
        String numStr = "";
        for (int i = 0; i < 6; i++) {
            String ran = Integer.toString(rand.nextInt(10));
            numStr += ran;
        }
        return numStr;
    }
  • ๋ฉ”์„ธ์ง€ ์ „์†ก ๊ด€๋ จ ์ฝ”๋“œ๋ฅผ ํ•œ ๋ˆˆ์— ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ์ปจํŠธ๋กค๋Ÿฌ์— ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์„œ ๊ธ€์„ ์ž‘์„ฑํ–ˆ์Œ
  • ๊ด€๋ จ ์ฝ”๋“œ๋ฅผ ์‹ค์ œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ฌธ์ž ๋‚ด์šฉ ์ƒ์„ฑ์ด๋‚˜, ๋ฉ”์„ธ์ง€ ์ดˆ๊ธฐ์ •๋ณด ์„ธํŒ…์€ ์„œ๋น„์Šค ๋ฉ”์„œ๋“œ๋กœ ๋นผ๊ธฐ๋ฅผ ๊ถŒ์žฅํ•œ๋‹ค

 

 

 

์ „์†ก ๊ฒฐ๊ณผ

์ฝ˜์†”.log

  • ๋‚ด ์—ฐ๋ฝ์ฒ˜์˜ ๋ถ€๊ฐ€์ ์ธ ๋ณด์•ˆ๊ด€๋ จ ์„ค์ •์œผ๋กœ ์ธํ•ด์„œ ๊ฒฐ๊ณผ ํ™•์ธ์€ ๋ชปํ–ˆ์Œ
  • ์ธ์ฆ๊ด€๋ จ ๋ฉ”์„ธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜์‹ ๋ฒˆํ˜ธ๋กœ ์ „์†กํ•˜๋ ค๋˜ ์‹œ๋„์˜ ๊ฒฐ๊ณผ๋ฅผ ๋Œ€์‹œ๋ณด๋“œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค

๋ฉ”์„ธ์ง€ ์ „์†ก ๊ฒฐ๊ณผ

  • ๋ณด์•ˆ ์„ค์ •๋งŒ ์•„๋‹ˆ์—ˆ์œผ๋ฉด ๋ฐœ์†ก๋๊ฒ ์ง€๋งŒ, ๋ถ€๊ฐ€์„œ๋น„์Šค๋ฅผ ๋ณ€๊ฒฝํ•  ๋งˆ์Œ์€ ์—†๋‹ค
  • ๋ฌธ์ž๋ฉ”์„ธ์ง€ ์ธ์ฆ ๊ธฐ๋Šฅ ๊ตฌํ˜„์€ ์—ฌ๊ธฐ๊นŒ์ง€!