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!')