使用 python 从 Twilio 读取 OTP

问题描述 投票:0回答:2

我目前正在使用 selenium python-pytest 框架从事网络自动化工作。这里我有一个场景,在提供我的电话号码后,将生成 OTP,我需要获取 OTP 并将其放入 Web 应用程序的文本框中。我看过一些有关 twilio 如何工作的教程,并且了解了如何使用 python 的 Twilio 库发送 OTP。但在这里,OTP 应该在单击某个按钮后由 Web 应用程序本身发送,但我不确定在这种情况下如何检索 OTP。如果我将 Twilio 号码放入电话号码文本框中并在我的 twilio 个人资料中获取 OTP,而不是我的个人电话号码,如何使用 python 检索相同的 OTP?我需要使用哪个 API 才能从我的 twilio 个人资料的收件箱中检索 OTP?任何线索都会很有帮助。

python selenium twilio twilio-api
2个回答
0
投票

本文档包含您需要阅读的所有代码。下面是从上述位置获取的一个示例,用于读取消息资源。

import os
from twilio.rest import Client

account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)

message = client.messages('MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX').fetch()

print(message.body)

0
投票

我是这样做的:

脚本点击将otp发送到twilio号码的按钮后,您需要等待号码发送。您可以使用下面的代码从消息中获取顶部的数字。

from twilio.rest import Client

verification_code = ""
for sms in client.messages.list(limit=1): # limit = 1 means that the code is going to read the last message sent.

   verification_code = sms.body[-6:] # [-6:] means that the last six characters of the message will be store in the variable. in my case the code is last six characters. You can configure it as you want.

   print(verification_code)
   time.sleep(1)
driver.find_element(By.XPATH, "elemenxpath").send_keys(verification_code)
© www.soinside.com 2019 - 2024. All rights reserved.