本文整理汇总了Python中twilio.rest.TwilioRestClient方法的典型用法代码示例。如果您正苦于以下问题:Python rest.TwilioRestClient方法的具体用法?Python rest.TwilioRestClient怎么用?Python rest.TwilioRestClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twilio.rest
的用法示例。
在下文中一共展示了rest.TwilioRestClient方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: plugin
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def plugin(srv, item):
""" expects (accountSID, authToken, from, to) in addrs"""
srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target)
try:
account_sid, auth_token, from_nr, to_nr = item.addrs
except:
srv.logging.warn("Twilio target is incorrectly configured")
return False
text = item.message
try:
client = TwilioRestClient(account_sid, auth_token)
message = client.messages.create(
body=text,
to=to_nr,
from_=from_nr)
srv.logging.debug("Twilio returns %s" % (message.sid))
except Exception as e:
srv.logging.warn("Twilio failed: %s" % e)
return False
return True
示例2: sendsms
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def sendsms(message):
"""Send SMS via Twilio SMS API
:param str message: SMS body to send
"""
# read configuration from the config file
config = check_config_values()
account_sid = config.get('DEFAULT', 'twilio_account_sid')
auth_token = config.get('DEFAULT', 'twilio_auth_token')
to_phone = config.get('DEFAULT', 'twilio_to_phone')
from_phone = config.get('DEFAULT', 'twilio_from_phone')
client = TwilioRestClient(account_sid, auth_token)
print("Sending this notification as SMS")
message = client.messages.create(
body=message, to=to_phone, from_=from_phone)
print("SMS sent")
# print(message.sid)
示例3: _send
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def _send(recipients, text_content=None, html_content=None, sent_from=None, subject=None, extra_data=None,
attachments=None):
try:
# twilio version 6
from twilio.rest import Client
except ImportError:
try:
# twillio version < 6
from twilio.rest import TwilioRestClient as Client
except ImportError:
raise Exception(
"Twilio is required for sending a TwilioTextNotification."
)
try:
account_sid = settings.TWILIO_ACCOUNT_SID
auth_token = settings.TWILIO_AUTH_TOKEN
except AttributeError:
raise Exception(
"TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN settings are required for sending a TwilioTextNotification"
)
client = Client(account_sid, auth_token)
for recipient in recipients:
client.messages.create(body=text_content, to=recipient, from_=sent_from)
示例4: init_twilio
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def init_twilio(self):
self.client = TwilioRestClient(self.sid, self.token)
示例5: notify
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def notify(self, notification):
client = TwilioRestClient(self.__config['SID'], self.__config['token'])
try:
client.calls.create(
url=settings.BASE_URL + "/twilio/%s/%s" % (notification.id, notification.user_to_notify.id),
method="GET",
to=notification.user_to_notify.profile.phone_number,
from_=self.__config['phone_number'])
print 'successfully sent the call'
except :
print 'failed to send the call'
示例6: notify
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def notify(self, notification):
max_length = 160
message = (notification.message[:max_length-2] + '..') if len(notification.message) > max_length else notification.message
client = TwilioRestClient(self.__config['SID'], self.__config['token'])
try:
client.sms.messages.create(body=message,
to=notification.user_to_notify.profile.phone_number,
from_=self.__config['sms_number'])
print 'successfully sent the sms'
except twilio.TwilioRestException as e:
print 'failed to send sms, Error: %s' % e
except :
e = sys.exc_info()[0]
print 'failed to send sms, Error: %s' % e
示例7: text_myself
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def text_myself(message):
"""Use Twilo to text the message argument to your phone."""
twilo_cli = TwilioRestClient(account_sid, auth_token)
twilo_cli.messages.create(body=message, from_=twilo_number, to=my_number)
示例8: __init__
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def __init__(self):
try:
number, account_sid, auth_token = utils.load_twilio_config()
self.number = number
self.twilio_client = TwilioRestClient(account_sid, auth_token)
except TwilioException:
print('\U0001F6AB Current twilio credentials invalid. '
'Please reset.'.encode("utf-8"))
utils.initialize_twiolio()
number, account_sid, auth_token = utils.load_twilio_config()
self.number = number
self.twilio_client = TwilioRestClient(account_sid, auth_token)
示例9: send_message
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def send_message(alert):
logging.debug('Opening settings.json.')
settings_file = settings.open_settings()
logging.debug('Getting Twilio settings.')
twilio_enabled = settings_file['settings']['general']['twilio']
logging.debug('Getting Slack settings.')
slack_enabled = settings_file['settings']['general']['slack']
logging.debug('Getting Pushover settings.')
pushover_enabled = settings_file['settings']['general']['pushover']
logging.debug('Checking if Twilio is enabled.')
if twilio_enabled:
logging.debug('Twilio enabled.')
logging.debug('Loading Twilio settings.')
twilio_settings = load_twilio_settings()
logging.debug('Configuring Twilio client.')
client = TwilioRestClient(twilio_settings["account_sid"], twilio_settings["auth_token"])
try:
logging.debug('Trying to send Twilio message.')
client.messages.create(body=alert,
to=twilio_settings['mobile_number'],
from_=twilio_settings["twilio_number"])
except TwilioRestException:
print('Unable to run application, correct Twilio credentials provided?')
logging.critical('Aborting... Unable to run application, Twilio credentials incorrect.')
sys.exit(406)
logging.debug('Checking if Slack is enabled.')
if slack_enabled:
logging.debug('Opening settings.json.')
slack_settings = load_slack_settings()
logging.debug('Staring up Slack Bot.')
slack_bot.run_bot(alert, slack_settings['channel_name'])
logging.debug('Checking if Pushover is enabled.')
if pushover_enabled:
logging.debug('Opening settings.json.')
pushover_settings = load_pushover_settings()
client = Client(pushover_settings['user_key'], api_token=pushover_settings['api_token'])
client.send_message(alert, priority=pushover_settings['priority'])
示例10: __init__
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def __init__(self, config):
super(TwilioSendSMSAction, self).__init__(config=config)
self.client = TwilioRestClient(self.config['account_sid'],
self.config['auth_token'])
示例11: _get_twilio_client
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def _get_twilio_client(self):
account = settings.TWILIO_ACCOUNT_SID
token = settings.TWILIO_AUTH_TOKEN
client = TwilioRestClient(account, token)
return client
示例12: _Action
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def _Action(self):
account_sid = self.redata['data']['account_sid']
auth_token = self.redata['data']['auth_token']
from_address = self.redata['data']['from_address']
to_address = self.redata['data']['to_address']
text = self.redata['data']['text']
client = TwilioRestClient(account_sid, auth_token)
message = client.messages.create(
to=to_address,
from_=from_address,
body=text)
assert message.status not in ('failed', 'undelivered'), \
message.ErrorMessage
return True
示例13: sms_twilio
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def sms_twilio(data, context):
'''
Sends an SMS message via Twilio to the given number.
The message is "[title] message"
config must include the strings twilio_accountsid, twilio_authtoken, and twilio_number
data['number']: phone number to send SMS message to.
data['twilio_accountsid'], data['twilio_authtoken'], data['twilio_number']: optional Twilio configuration
context['title']: used in creating SMS message
context['message']: used in creating SMS message
'''
from config import config
from twilio.rest import TwilioRestClient
if 'number' not in data:
util.die('alert_sms_twilio: missing number')
config_target = config
if 'twilio_accountsid' in data and 'twilio_authtoken' in data and 'twilio_number' in data:
config_target = data
sms_message = "[%s] %s" % (context['title'], context['message'])
client = TwilioRestClient(config_target['twilio_accountsid'], config_target['twilio_authtoken'])
message = client.messages.create(body = sms_message, to = data['number'], from_ = config_target['twilio_number'])
示例14: __init__
# 需要导入模块: from twilio import rest [as 别名]
# 或者: from twilio.rest import TwilioRestClient [as 别名]
def __init__(self, message):
self.entity = message['entity']
self.status = int(message['status'])
self.output = message['output']
if int(message['status']) == 0:
self.color = '#36a64f'
self.twilio_msg_prefix = 'recovery notification.'
self.twilio_msg_postfix = 'is okay!'
elif int(message['status']) == 1:
self.color = '#FFA500'
self.twilio_msg_prefix = 'this is a warning!'
self.twilio_msg_postfix = 'is in status warning!'
elif int(message['status']) == 2:
self.color = '#C74350'
self.twilio_msg_prefix = 'bad news, this is a critical notification!'
self.twilio_msg_postfix = 'is in status critical!'
else:
self.color = ''
self.twilio_msg_prefix = 'unknown notification.'
self.twilio_msg_postfix = 'is in status unknown!'
slack_alert_template = get_template('isubscribe/slack_alert.txt')
self.slack_msg_content_fallback = slack_alert_template.render({ 'entity': self.entity, 'status': self.status, 'output':self.output })
self.slack_attachments = [{
"fallback": self.slack_msg_content_fallback,
"title": self.entity,
"title_link": "%s%s" % (settings.REGISTRATION_URL_PREFIX, reverse_lazy('events')),
"text": self.output,
"color": self.color,
"author_name": settings.SLACK_BOT_NAME,
"author_link": "%s%s" % (settings.REGISTRATION_URL_PREFIX, reverse_lazy('events')),
"author_icon": settings.SLACK_BOT_ICON,
}]
twilio_msg_formated = self.twilio_msg_prefix + ' ' + self.entity + ' ' + self.twilio_msg_postfix
self.twilio_params = { 'msg' : twilio_msg_formated,
'api_token' : settings.TWILIO_CALLBACK_API_TOKEN,
'entity': self.entity,
'status': self.status
}
self.twilio_client = TwilioRestClient(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
self.slack_delivery_to = []
self.twilio_delivery_to = []