[英]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.