【319】Python 通过 Twilio 发短信

参考:python利用twilio模块给自己发短信

参考:使用python实现往手机发短信(基于twilio)


步骤如下:

  1. 登录 Twilio 网站注册,貌似需要科学上网,包括用户名、密码、手机号、项目目的&名称等;
  2. 通过 Console Dashboard 获取 Account Sid 和 Auth Token;
  3. 切换到 All Products & Service》Phone Numbers 可以创建用来发信息的电话号;
  4. 切换到 Programmable SMS Dashboard 进行设置,切换到 Learn & Build 》Build 界面中进行测试;
  5. 如果不成功,需要点击“Your trial accounts can only send messages to verified numbers in these countries”中的 these countries,并将 China 前面勾选。

  6. 配置成功,并且发送信息成功后,需要安装 Twilio 模块,首先需要是 Python 3.0 以上版本;
  7. cmd 中定位 Python 内部的 scripts 文件夹(sys.path 可获取相关路径),然后执行 pip install twilio 代码即可安装;

  调用代码如下所示:

from twilio.rest import Client  
  
account = 'XXXXXXXXXXXXXXXXXXXX'  
token = 'YYYYYYYYYYYYYY'  
myNumber='+86XXXXXXXXXXXXX'  
twilioNumber='+XXXXXXXXXX'  
message="Hello, it's me!"
  
client = Client(account, token)  
message = client.messages.create(to=myNumber, from_=twilioNumber, body=message)

  函数形式代码如下所示:

def textMessage(message):
	from twilio.rest import Client  
			
	account = 'AC******bd' 
	token = '2a******d9' 
	myNumber='+86******10' 
	twilioNumber='+14******55' 

	client = Client(account, token) 
	message = client.messages.create(to=myNumber, from_=twilioNumber, body=message)

  测试发现,发送英文可以正常发送,然而中文不可以发送成功~

原文地址:https://www.cnblogs.com/alex-bn-lee/p/9204076.html