개발

API 연동으로 문자 보내기 메시지 전송 문자 발송

플라티비 2023. 11. 14. 11:56

플랫폼 개발 중 SMS, LMS, MMS, 알림톡 연동 사 교체가 필요하여 테스트를 해본경험을 공유 합니다. 

 

메시지 발송 업체는 SK C&C에서 서비스중인 메시지투고를 이용했습니다. 최근 런칭했는데 비용이 매리트가 있어 교체하게 되었습니다. 기존 사용중인 메시지 비용이 생각보다 비싼 감이 있었습니다. 최근 메시지 사용량이 대폭 증가하는 서비스가 있었느데 비용절감에 도움이 되었습니다. 

 

https://message.to-go.io

 

메시지투고

통합 메세징 솔루션, 심플하고 빠른 기업 메시징 솔루션, 문자 발송, 메시지 발송

message.to-go.io

 

1. API Key 발급

메시지투고 회원가입을 하고나면 API Key 를 발급받을 수 있습니다. 결제나 별도 절차 없이 테스트 문자 발송이 가능합니다. 추후 상용서비스 시 사업자등록증을 등록하고 발신번호를 등록하고나면 원하는대로 메시지 발송이 가능합니다. 

 

메시지투고 내 정보

회원가입 후 각종 약관동의를 한뒤 내 정보로 가면 API Key를 만들 수 있습니다. 
Tenant ID, API Key 가 발급되는데 메시지 발송 API 요청시 해당값으로 인증을 받을 수 있습니다. 

메시지투고 테스트 문자 발송 페이지

물론 웹페이지에서도 문자 발송을 할 수 있습니다. 앞서 이야기한바와 같이 사업자 등록전에는 테스트 메시지 발송만 할 수 있습니다. 메시지투고 전화번호로 회원가입시 본인인증한 내 번호로 테스트 해 볼 수 있습니다. 

2. Postman 으로 API 테스트하기

API 연동 가이드는 사이트의 화면 좌측하단 설정메뉴에서 다운로드 받을 수 있습니다.

메시지투고 설정메뉴

가이드에 따라 key, id, tenant_id 를 설정하고, SMS 를 전송하기 위해 아래 와 같이 구성 했습니다. 

메시지 투고 API Key 설정

 

전송 성공이 확인 되었습니다. 실제 제 폰으로 메시지투고 번호로 발송이 되는군요. 

참고하시라고 curl 전문으로 공유합니다. 

curl --location 'https://api.message.to-go.io/message?tenant_id=[tenant_id]&user_id=platib' \
--header 'Content-Type: text/plain' \
--header 'Authorization: [API Key]' \
--data '{
    "from_phone_number":"****",
    "from_country_code":"82",
    "to_country_code":"82",
    "to_phone_numbers": [
      {
        "phone": "[수신번호]"
      }
    ],
    "next_method_on_fail":"kakao",
    "contents":"메시지투고 발송 테스트 문자",
    "send_request_dtm":"",
    "secondary_customer_reference_id": "",
    "primary_customer_reference_id": "",
    "send_type":"sms",
    "send_status":"request"
}'

 

 

3. REST API 작성

Spring boot 로 구성되어있는 백엔드에 추가하기위하여 아래와 같이 코드를 만들었습니다. 

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
@RequestMapping("/message")
public class ApiController {

    @PostMapping("/sendSMS")
    public ResponseEntity<String> sendSMS(@RequestBody SMSRequest requestBody) {
        String apiUrl = "https://api.message.to-go.io/message?tenant_id=**&user_id=*****";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Key ****");

        HttpEntity<SMSRequest> entity = new HttpEntity<>(requestBody, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class);

        return response;
    }
}
public class SMSRequest {
    private String from_phone_number;
    private String from_country_code;
    private String to_country_code;
    private List<PhoneNumber> to_phone_numbers;
    private String next_method_on_fail;
    private String contents;
    private String send_request_dtm;
    private String secondary_customer_reference_id;
    private String primary_customer_reference_id;
    private String send_type;
    private String send_status;

    // getters and setters for the above fields
}

public class PhoneNumber {
    private String phone;

    // getters and setters for the 'phone' field
}