odapi/app/twilio.py

26 lines
593 B
Python
Raw Normal View History

2020-03-18 20:56:06 +01:00
from twilio.rest import Client
from secrets import randbits
2020-03-23 21:17:16 +01:00
from constants import account_id, token, sms_sender
2020-04-19 21:30:41 +02:00
from database.crud import save_otp
2020-03-18 20:56:06 +01:00
def connect_api():
account_sid = account_id
auth_token = token
client = Client(account_sid, auth_token)
return client
def generate_code():
bits = 16
2020-03-23 21:17:16 +01:00
code = randbits(bits)
2020-03-18 20:56:06 +01:00
return code
2020-03-23 21:17:16 +01:00
def send_otp(receiver):
2020-03-18 20:56:06 +01:00
client = connect_api()
code = generate_code()
message = "Your OTP code is {0}".format(code)
2020-04-19 21:30:41 +02:00
client.messages.create(to=receiver, from_=sms_sender, body=message)
save_otp(receiver, code)