[英]How to send a text message using the Twilio API
我正在开发一个 Python 项目,以向特定的电话号码发送短信。 下面的代码应将 api 凭据加载到 Twilio REST 客户端,然后向RECIPIENT
发送消息。
# Download the helper library from https://www.twilio.com/docs/python/install
from email import message
import os
from twilio.rest import Client
from dotenv import load_dotenv
import logging
import os
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
# Set environment variables
ACCOUNT_SID = os.getenv("ACCOUNT_SID")
AUTH_TOKEN = os.getenv("AUTH_TOKEN")
PHONE = os.getenv("PHONE")
RECIPIENT = os.getenv("RECIPIENT")
# Load environment variables
load_dotenv()
accountSID = ACCOUNT_SID
authToken = AUTH_TOKEN
myNumber = RECIPIENT
twilioNumber = PHONE
twilioCli = Client(accountSID,authToken)
messages = twilioCli.messages.create(body="The boring task is finished",from_=twilioNumber, to=myNumber)
logging.debug(f"{message.sid}")
预期:应该发送消息并且应该打印出消息SID。
实际的:
File "C:\Users\EvanGertis\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\twilio\rest\__init__.py", line 58, in __init__
raise TwilioException("Credentials are required to create a TwilioClient")
twilio.base.exceptions.TwilioException: Credentials are required to create a TwilioClient
为什么这不起作用?
看起来像: # Load environment variables is below your assignments, put it at thr top before assignment.
看那个“#Set environment variables”部分,
试着把这些东西放进去
ACCOUNT_SID = os.getenv("# Put your account Sid here")
AUTH_TOKEN = os.getenv("# your auth token here")
PHONE = os.getenv("# your phone you registered on twilio")
RECIPIENT = os.getenv("# the number you got from twilio")
这将帮助您运行代码,如果这不起作用,那么有一个技巧:
查看“#加载环境变量”部分,
试着把这些东西放进去
accountSID = # Put your account Sid here
authToken = # your auth token here
myNumber = # your phone you registered on twilio
twilioNumber = # the number you got from twilio
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.