未验证 提交 7f03af25 编写于 作者: D Dan Bader 提交者: GitHub

Merge pull request #16 from d3prof3t/aws-chalice

Serverless SMS Service
{
"version": "2.0",
"app_name": "serverless-sms-service",
"stages": {
"dev": {
"api_gateway_stage": "api",
"environment_variables": {
"ACCOUNT_SID": "<your-account-sid>",
"AUTH_TOKEN": "<your-auth-token>",
"FROM_NUMBER": "<from-number>",
"TO_NUMBER": "<to-number>"
}
}
}
}
.chalice/deployments/
.chalice/venv/
# Serverless SMS Sender Service
This repository is linked to https://realpython.com/aws-lambda-serverless-python/ post.
A step by step tutorial on how to build and deploy your very first serverless app. Multiple steps are divided in
3 tags where each tag serves as an incremental feature. Let's begin by cloning the repository and follow the steps from the post.
## Phase 1 - Hello World
A simple Hello World app showcasing the simplicity of how a serverless app can be deployed in no time.
## Phase 2 - SMS Sender Service
Evolving our Hello World app from the last section, to a more robust and real word SMS sender app, using Twilio APIs underneath.
## Phase 3 - Refactoring
Refactoring the code using few best practices pertaining to large serverless projects.
# core imports
from chalice import Chalice, Response
from twilio.base.exceptions import TwilioRestException
# app level imports
from chalicelib import sms
app = Chalice(app_name="sms-shooter")
@app.route("/")
def index():
return {"hello": "world"}
@app.route("/service/sms/send", methods=["POST"])
def send_sms():
request_body = app.current_request.json_body
if request_body:
try:
resp = sms.send(request_body)
if resp:
return Response(
status_code=201,
headers={"Content-Type": "application/json"},
body={
"status": "success",
"data": resp.sid,
"message": "SMS successfully sent",
},
)
else:
return Response(
status_code=200,
headers={"Content-Type": "application/json"},
body={
"status": "failure",
"message": "Please try again!!!",
},
)
except TwilioRestException as exc:
return Response(
status_code=400,
headers={"Content-Type": "application/json"},
body={"status": "failure", "message": exc.msg},
)
from os import environ as env
from twilio.rest import Client
# Twilio Config
ACCOUNT_SID = env.get("ACCOUNT_SID")
AUTH_TOKEN = env.get("AUTH_TOKEN")
FROM_NUMBER = env.get("FROM_NUMBER")
TO_NUMBER = env.get("TO_NUMBER")
# create a twilio client using account_sid and auth token
tw_client = Client(ACCOUNT_SID, AUTH_TOKEN)
def send(payload_params=None):
""" send sms to the specified number """
msg = tw_client.messages.create(
from_=FROM_NUMBER, body=payload_params["msg"], to=TO_NUMBER
)
if msg.sid:
return msg
attrs==17.4.0
botocore==1.12.30
chalice==1.6.0
click==6.7
docutils==0.14
enum-compat==0.0.2
jmespath==0.9.3
python-dateutil==2.7.3
six==1.11.0
typing==3.6.4
urllib3==1.24
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册