Languages/Python

[워니 파이썬 기초] #6 휴대폰으로 문자 보내기

성중 2021. 2. 19. 20:32

파이썬 프로그래밍 – 핸드폰 문자 보내기

활용: 라이브러리(공개된 패키지), API(코드를 써서 데이터를 가져오는 경로)

 

먼저 웹 사이트에 들어가자.

https://twilio.com

 

무료 전화번호를 받자.

-> +12055579098

 

dashboard에서

ACCOUNT SID와 AUTH TOKEN을 체크하자. (API의 아이디와 패스워드 개념)

ACCOUNT SID: ACdc39f210dca99f1bad66f0945afe586e

AUTH TOKEN: a7bd1f55eb5d4b2a635b6cbf2295dbc8

 

이제 코딩으로 넘어가 twilio의 API를 사용해보자.

repl의 패키지에 twilio를 검색, ‘+’버튼을 눌러 다운받자.

 

이후 다시 사이트에서 SMS Quickstart -> Python을 들어가 예제 코드를 복사한다.

# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client


# Your Account Sid and Auth Token from twilio.com/console
# DANGER! This is insecure. See http://twil.io/secure
account_sid = 'ACdc39f210dca99f1bad66f0945afe586e'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

message = client.messages \
                .create(
                     body="Join Earth's mightiest heroes. Like Kevin Bacon.",
                     from_='+15017122661',
                     to='+15558675310'
                 )

print(message.sid)

ACCOUNT SID와 AUTH TOKEN을 알맞게 넣어주자.

 

이후 세부 설정

body -> 메시지 내용

from -> 발신인 (발급받은 무료 전화번호)

to -> 수신인 (국가번호 +82 / 앞의 0을 뺀 핸드폰 번호) ex) +821012341234

 

코드를 실행시키면 메시지가 발송된다. (인증에 사용한 본인 번호로만 발송 가능)