POPBill Developers
가이드

튜토리얼

Python 개발환경에서 팝빌 SDK를 추가하여 단문 문자 메시지 전송 (SendSMS) 함수를 구현하는 예시입니다.

1. POPBiLL SDK 추가

팝빌 Python SDK를 추가하기 위해 pip install 명령어를 사용하여 패키지를 설치합니다.

$ pip install popbill

2. POPBiLL SDK 설정

Django 프로젝트의 settings.py 파일에 연동신청시 발급받은 인증정보와 연동환경 설정값을 선언합니다.

# -*- coding: utf-8 -*-
import os
import sys
import imp
imp.reload(sys)

try:
    sys.setdefaultencoding('UTF8')
except Exception as E:
    pass

#생략
#...
#...

# 링크아이디
LinkID = "TESTER"

# 발급받은 비밀키, 유출에 주의하시기 바랍니다.
SecretKey = "SwWxqU+0TErBXy/9TVjIPEnI0VTUMMSQZtJf3Ed8q3I="

# 연동환경 설정값, 개발용(True), 상업용(False)
IsTest = True

# 인증토큰 IP제한기능 사용여부, 권장(True)
IPRestrictOnOff = True

# 팝빌 API 서비스 고정 IP 사용여부, true-사용, false-미사용, 기본값(false)
UseStaticIP = False

#로컬시스템 시간 사용여부, 권장(True)
UseLocalTimeYN = True

3. SendSMS 기능 구현

Django App에 생성된 views.py 파일에 문자 서비스 클래스 객체 생성과 단문 문자 메시지 전송 (SendSMS) 함수 호출 코드를 추가합니다.

# -*- coding: utf-8 -*-
from django.shortcuts import render
from popbill import PopbillException, ContactInfo, CorpInfo, JoinForm, MessageService, \
    MessageReceiver

from config import settings

# settings.py 작성한 LinkID, SecretKey를 이용해 MessageService 객체 생성
messageService = MessageService(settings.LinkID, settings.SecretKey)

# 연동환경 설정값, 개발용(True), 상업용(False)
messageService.IsTest = settings.IsTest

# 인증토큰 IP제한기능 사용여부, 권장(True)
messageService.IPRestrictOnOff = settings.IPRestrictOnOff

# 팝빌 API 서비스 고정 IP 사용여부, true-사용, false-미사용, 기본값(false)
messageService.UseStaticIP = settings.UseStaticIP

#로컬시스템 시간 사용여부, 권장(True)
messageService.UseLocalTimeYN = settings.UseLocalTimeYN

def sendSMS(request):
    try:
        # 팝빌회원 사업자번호
        CorpNum = "1234567890"

        # 팝빌회원 아이디
        UserID = "testkorea"

        # 발신번호
        Sender = "07043042991"

        # 발신자명
        SenderName = "발신자명"

        # 수신번호
        ReceiverNum = "010111222"

        # 수신자명
        ReceiverName = "수신자명"

        # 단문메시지 내용, 90Byte 초과시 길이가 조정되 전송됨
        Contents = "문자 API 단건전송 테스트"

        # 예약전송시간, 작성형식:yyyyMMddHHmmss, 공백 기재시 즉시전송
        reserveDT = ""

        # 광고문자 전송여부
        adsYN = False

        # 요청번호
        # 파트너가 전송 건에 대해 관리번호를 구성하여 관리하는 경우 사용.
        # 1~36자리로 구성. 영문, 숫자, 하이픈(-), 언더바(_)를 조합하여 팝빌 회원별로 중복되지 않도록 할당.
        RequestNum = ""

        receiptNum = messageService.sendSMS(CorpNum, Sender, ReceiverNum, ReceiverName,
                                            Contents, reserveDT, adsYN, UserID, SenderName, RequestNum)

        return render(request, 'response.html', {'receiptNum': receiptNum})
    except PopbillException as PE:
        return render(request, 'exception.html', {'code': PE.code, 'message': PE.message})
	

함수 호출결과 코드와 메시지를 출력하는 "/response.html" 파일을 추가합니다.

<html xmlns=" http://www.w3.org/1999/xhtml ">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Popbill API Test.</title>
  </head>
  <body>
    <div id="content">
        <fieldset class="fieldset1">
            <legend>문자 접수번호</legend>
            <ul>
                <li> receiptNum (접수번호) : {{ receiptNum }} </li>
            </ul>
        </fieldset>
    </div>
  </body>
</html>

4. 결과 확인

함수 호출 반환 결과는 아래와 같습니다.
- 성공 : Response code 로 숫자 1 반환
- 실패 : PopbillException 으로 음의 정수 8자리 숫자값 오류코드와 오류메시지 반환 [오류코드]