[英]In Twilio trial account I unable to get incoming message for OTP scenario
[英]Send custom OTP with twilio message python
Twilio Messaging
,其中我在 python 的帮助下生成一个 OTP,我想将该 OTP 发送给发件人。${random_str}
$random_str
。import random
import math
import os
from twilio.rest import Client
digits = [i for i in range(0, 10)]
random_str = ""
## create a number of any length for now range = 6
for i in range(6):
index = math.floor(random.random() * 10)
random_str += str(digits[index])
## display the otp
print(random_str)
account_sid = '<sid>'
auth_token = '<auth_token>'
client = Client(account_sid, auth_token)
message = client.messages.create(
messaging_service_sid='<SID>',
body='Your OTP is $random_str',
to='<number>'
)
print(message.sid)
Sent from your Twilio trial account - Your OTP is ${random_str}
Sent from your Twilio trial account - Your OTP is {random_str}
Sent from your Twilio trial account - Your OTP is $random_str
这不是js... python中,需要用f-string
格式化一个字符串:
body=f'Your OTP is {random_str}'
编辑:
生成随机 OTP 的一种更简洁的方法是使用random.randint
function,它接受 2 个数字作为其范围,并将返回该范围内的随机选择:
random_otp = random.randint(10000, 99999)
现在 random_otp 将是 10000 到 99999 之间的数字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.