0% found this document useful (0 votes)
14 views1 page

Code Python

This document provides a Python script that uses the Twilio API to send a One-Time Password (OTP) via SMS. It includes functions to generate a TOTP secret, send the OTP to a specified phone number, and verify the OTP entered by the user. The script demonstrates how to configure Twilio and handle OTP generation and verification processes.

Uploaded by

slovoprobivator
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Code Python

This document provides a Python script that uses the Twilio API to send a One-Time Password (OTP) via SMS. It includes functions to generate a TOTP secret, send the OTP to a specified phone number, and verify the OTP entered by the user. The script demonstrates how to configure Twilio and handle OTP generation and verification processes.

Uploaded by

slovoprobivator
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import pyotp

from twilio.rest import Client

# Konfigurasi Twilio
account_sid = 'YOUR_TWILIO_ACCOUNT_SID'
auth_token = 'YOUR_TWILIO_AUTH_TOKEN'
twilio_phone_number = 'YOUR_TWILIO_PHONE_NUMBER'
client = Client(account_sid, auth_token)

# Fungsi untuk mengirim OTP


def send_otp(phone_number):
# Buat secret key untuk TOTP
secret = pyotp.random_base32()
totp = pyotp.TOTP(secret)
otp = totp.now() # Menghasilkan OTP saat ini

# Kirim OTP melalui SMS


message = client.messages.create(
body=f'Your OTP is: {otp}',
from_=twilio_phone_number,
to=phone_number
)

print(f'OTP sent to {phone_number}: {otp}')


return secret # Kembalikan secret untuk verifikasi di kemudian hari

# Contoh penggunaan
phone_number = '0811xxxxxxx' # Ganti dengan nomor telepon yang valid
secret = send_otp(phone_number)

# Untuk verifikasi OTP


def verify_otp(secret, user_input):
totp = pyotp.TOTP(secret)
return totp.verify(user_input)

# Contoh verifikasi
user_input = input('Enter the OTP you received: ')
if verify_otp(secret, user_input):
print('OTP is valid!')
else:
print('Invalid OTP!')

You might also like