twilio的注册环节略过,
twilio在使用环节中注意短信发送频率不要太高,目前限制每天每个号码发送5条信息,过后会出现连续收不到短信的情况。
封装成函数,以便调用:
from twilio.rest import Client
#327账户配置
account_sid = 'ACeefb5xxxbd33a85bbxxxxxxxx55c0cb3'
auth_token = '90c4d2721xxxxxba81eb6exxxx9cb2'
def sendSMS(bodys):
client=Client(account_sid,auth_token)
client.messages.create(to='+8617xxxxxxx08',
from_='+12562429158',
body=bodys)
#sendSMS('小王怎么这么好看...')
# =============================================================================
# #引入包 调用函数(如果找不到模块就自行添加临时环境变量)
# from twilioSendSMS import twilioSendSMS
#
# twilioSendSMS('这是调用函数方法发送...01') #调用函数方法实现向手机发送短信内容
# =============================================================================
另外还可以
试着将twilio封装成类,以便在其他文件中实例调用。
# =============================================================================
# #类封装 Twilio配置信息及 sendMsg方法:
# #从 本文件名 引入 类名: from twilioSendMsg import TwilioSendMsg
# #创建实例 tsm=TwilioSendMsg() ,后调用 sendMsg()方法发送短信内容到手机。
# =============================================================================
from twilio.rest import Client
class TwilioSendMsg():
#327账户配置
def __init__(self):
self.account_sid = 'ACeefb5xxxxxxd33a85bxxxxxxxx5c0cb3'
self.auth_token = '90xxxxxxx9e9c12adba81eb6e5ab9cb2'
self.fromNumber='+12562429158'
self.toNumber='+86175xxxxx708'
def sendMsg(self,bodys):
self.client=Client(self.account_sid,self.auth_token)
self.client.messages.create(to=self.toNumber,
from_=self.fromNumber,
body=bodys)
#tsm1=TwilioSendMsg()
#tsm1.sendMsg('小王怎么这么好看...')
# =============================================================================
# #引入包 及 类名(如果找不到模块就自行添加临时环境变量)
# from twilioSendMsg import TwilioSendMsg
#
# tsm=TwilioSendMsg() #创建实例
# tsm.sendMsg('这是调用类实例方法发送...01') #调用类方法实现向手机发送短信内容
# =============================================================================