Simplify Twilio integration

This commit is contained in:
coolneng 2020-09-17 17:36:15 +02:00
parent 5a5486b751
commit 8d98bbd9f6
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
2 changed files with 7 additions and 14 deletions

View File

@ -1,25 +1,18 @@
from twilio.rest import Client
from secrets import randbits from secrets import randbits
from constants import ACCOUNT_ID, TOKEN, SMS_SENDER from twilio.rest import Client
from constants import ACCOUNT_ID, SMS_SENDER, TOKEN
from database.crud import save_otp from database.crud import save_otp
def connect_api(): def create_twilio_client(account_sid, auth_token):
account_sid = ACCOUNT_ID
auth_token = TOKEN
client = Client(account_sid, auth_token) client = Client(account_sid, auth_token)
return client return client
def generate_code():
bits = 16
code = randbits(bits)
return code
def send_otp(receiver): def send_otp(receiver):
client = connect_api() client = create_twilio_client(account_sid=ACCOUNT_ID, auth_token=TOKEN)
code = generate_code() code = randbits(k=16)
message = "Your OTP code is {0}".format(code) message = "Your OTP code is {0}".format(code)
client.messages.create(to=receiver, from_=SMS_SENDER, body=message) client.messages.create(to=receiver, from_=SMS_SENDER, body=message)
save_otp(receiver, code) save_otp(receiver, code)

View File

@ -2,7 +2,7 @@ from fastapi import APIRouter, Depends, HTTPException, Response
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app.schemas import * from app.schemas import *
from app.twilio import send_otp from app.external_services import send_otp
from database.crud import get_db, insert_data, verify_otp from database.crud import get_db, insert_data, verify_otp
router = APIRouter() router = APIRouter()