Cloud Code Functions
Integrations
Twilio API와 클라우드 코드 활용: SMS 발송 구현하기
9 분
클라우드 기능과 twilio api를 사용하여 문자 메시지 보내기 소개 이 가이드는 twilio rest api를 사용하여 sms를 보내는 방법을 설명합니다 이 단계별 튜토리얼을 완료한 후, 클라우드 코드 기능을 사용하여 장치에 sms를 보낼 수 있습니다 전제 조건 이 튜토리얼을 완료하려면 다음이 필요합니다 back4app에서 생성된 앱 새 앱 만들기 튜토리얼 을 따라 back4app에서 앱을 만드는 방법을 배우세요 프로젝트에 구성된 back4app 명령줄 클라우드 코드 설정 튜토리얼 을 따라 프로젝트에 대한 클라우드 코드를 설정하는 방법을 배우세요 twilio 에서 계정을 생성하세요 시작해봅시다! 사용자와 전화번호에 sms를 보내는 함수를 작성할 때 따라야 할 몇 가지 단계가 아래에 있습니다 twilio에서 계정을 생성하거나 액세스하는 방법을 배우려면 아래의 링크를 확인하세요 새 계정 만들기 계정에 로그인하기 1 전화번호 활성화 로그인하거나 새 계정을 만든 후, 프로젝트로 리디렉션됩니다 거기서 왼쪽에 있는 #전화번호 #전화번호 를 클릭해야 합니다 다음으로, 마지막 링크인 ‘시작하기’를 탭한 후, ‘첫 번째 twilio 전화번호 받기’ 버튼을 클릭합니다 아래와 같이 표시됩니다 그 후, twilio 계정에 대한 첫 번째 전화번호를 받게 됩니다 전화번호를 찾을 수 없는 경우, #전화번호로 가서 번호 관리로 이동하세요 2 계정 sid 및 인증 토큰 가져오기 당신의 계정 sid 계정 sid 와 인증 토큰 인증 토큰 , 계정에 로그인하고 대시보드로 가서 설정을 클릭하세요 프로젝트에 대한 모든 중요한 정보는 해당 섹션에서 확인할 수 있습니다; 아래 이미지와 같이 이제 cloud code에 대한 sid 및 인증 토큰을 복사할 수 있습니다 3 twilio에서 모듈 설치 컴퓨터에서 명령줄 인터페이스를 위한 환경을 구성한 후, package json이라는 파일을 만들고, 이 파일 안에 twilio 모듈을 설치해야 합니다 예를 들어 1 { 2 "dependencies" { 3 "twilio" " " 4 } 5 } 4 cloud code 구현 parse server 3 x 1 parse cloud define("sendsms", async(request) => { 2 3 // requiring the values to send 4 let 5 getmessage = request params message, 6 getphoneto = '+target test phone number', 7 getphonefrom = "+your first phone number", 8 accountsid = 'accountsid', 9 authtoken = 'authtoken'; 10 11 //require the twilio module and create a rest client 12 let client = require('twilio')(accountsid, authtoken); 13 14 return await client messages 15 create({ 16 body getmessage, // any number twilio can deliver to 17 from getphonefrom, // a number you bought from twilio and can use for outbound communication 18 to getphoneto // body of the sms message 19 }); 20 }); parse server 2 x 1 parse cloud define("sendsms",function(request,response){ 2 3 // requiring the values to send 4 var 5 getmessage = request params message, 6 getphoneto = '+target test phone number', 7 getphonefrom = "+your first phone number", 8 accountsid = 'accountsid', 9 authtoken = 'authtoken'; 10 11 12 //require the twilio module and create a rest client 13 var client = require('twilio')(accountsid, authtoken); 14 15 client messages 16 create({ 17 body getmessage, // any number twilio can deliver to 18 from getphonefrom, // a number you bought from twilio and can use for outbound communication 19 to getphoneto // body of the sms message 20 }) 21 then(function(results) { 22 response success(results sid); 23 }) 24 catch(function(error) { 25 response error(error); 26 }) 27 }); 5 함수 “sendsms” 테스트 클라이언트 sdk에서 기능을 테스트할 수도 있지만, 지금은 rest api 명령을 사용하여 전송하겠습니다 curl x post \\ h "x parse application id app id" \\ h "x parse rest api key rest key" \\ h "content type application/json" \\ d '{ "message" "now, i can send sms from cloud code using twilio", "phone" "+target test phone number" }' \\ https //parseapi back4app com/functions/sendsms 결과는 다음과 같을 것입니다 6 완료되었습니다! 위에서 설명한 가이드를 통해 back4app에서 cloud code 기능과 함께 twilio를 사용하여 고객에게 sms를 보낼 수 있습니다! 도움이 필요하시거나 기능/링크가 작동하지 않는 경우, 채팅을 통해 저희 팀에 문의해 주세요!