-
安装Twilio;
我在anaconda中使用"conda install twilio"安装失败,但是使用pip可以.并且能够在conda中查到Twilio包;pip install twilio -
注册Twilio:
- 在官网(https://www.twilio.com)点击中间的红色按钮:get free API key进行注册.我使用126.com的邮箱能够成功注册.
- 在邮箱中点击邮件激活进入手机号码验证,会给你发个验证码(这个号码将是你待会能够使用进行测试短信收发的已激活号码),
- 注册成功会给你返回一个号码. -
进入个人页面

上图三个框中的符号在代码中会使用到.单击箭头中的show会显示密钥. -
代码:
from twilio.rest import Client account_sid = "AC941ae479828492xxxxxxxxxxxxxxxxxxx" account_token = "57a220114cd7f95xxxxxxxxxxxxxxxxxxx" client = Client(account_sid,account_token) message = client.messages.create( body = "心情不好的时候,就抬头看天空吧.", to = "+8615812345678", from_ = "+12055123456") print(message.sid) -
遇到的问题:
– ObsoleteException: TwilioRestClient has been removed from this version of the library. Please refer to current documentation for guidance.
出现这个是因为这个代码版本太老了,包版本更新了.开始使用的代码是这样的(旧代码),把语句换了就好:from twilio.rest import TwilioRestClient account_sid = "AC941ae479xxxxxxxxxxxxxxxxxxxxxx" account_token = "57a22011xxxxxxxxxxxxxxxxxxxxxx" client = TwilioRestClient(account_sid,account_token) message = client.sms.messages.create( body = "心情不好的时候,就抬头看天空吧.", to = "+8615812345678", from_ = "+12055123456") print(message.sid)– TwilioRestException: HTTP 400 error: Unable to create record: The number is unverified. Trial accounts cannot send messages to unverified numbers; verify at twilio.com/user/account/phone-numbers/verified, or purchase a Twilio number to send messages to unverified numbers.
出现这个问题的原因是你是免费用户,在没有验证手机的情况下,不能给别的手机发短信,只能给刚刚上面提到的已经接受短信验证的手机发送,你可以在twilio.com/user/account/phone-numbers/verified
这个网站上添加验证手机号验证.
用Python实现Twilio模块的短信发送
最新推荐文章于 2023-10-20 21:18:19 发布
本文介绍了如何在Python环境中安装和使用Twilio模块发送短信。通过conda和pip尝试安装Twilio,最终成功并完成注册过程。在使用过程中遇到的错误,如`ObsoleteException`和`TwilioRestException`,都是由于版本过旧或免费试用限制导致。解决方案包括查阅官方文档和验证手机号码。
4047





