adding twilio module for sending text notifications in build process

This commit is contained in:
Matthew Makai 2014-01-23 15:55:23 -05:00 committed by Michael DeHaan
parent b41541c62a
commit 2215111ec5

View file

@ -20,22 +20,16 @@
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
version_added: "1.2" version_added: "1.5"
module: text module: text
short_description: Sends a text message to a mobile phone. short_description: Sends a text message to a mobile phone through Twilio.
description: description:
- Sends a text message to a phone number through an SMS service. Currently - Sends a text message to a phone number through an the Twilio SMS service.
only Twilio is supported.
notes: notes:
- Like the other notification modules, this one requires an external - Like the other notification modules, this one requires an external
dependency to work. In this case, you'll need a Twilio account with dependency to work. In this case, you'll need a Twilio account with
a purchased phone number to send the text message. a purchased or verified phone number to send the text message.
options: options:
sms_service:
description:
the SMS service to use; currently only Twilio is supported
required: false
default: twilio
account_sid: account_sid:
description: description:
user's account id for Twilio found on the account page user's account id for Twilio found on the account page
@ -88,12 +82,10 @@ except ImportError:
import base64 import base64
def post_text(module, sms_service, account_sid, auth_token, msg, def post_text(module, account_sid, auth_token, msg, from_number, to_number):
from_number, to_number):
if sms_service == 'twilio':
URI = "https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json" \ URI = "https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json" \
% (account_sid,) % (account_sid,)
AGENT = "Ansible/1.6" AGENT = "Ansible/1.5"
data = {'From':from_number, 'To':to_number, 'Body':msg} data = {'From':from_number, 'To':to_number, 'Body':msg}
encoded_data = urllib.urlencode(data) encoded_data = urllib.urlencode(data)
@ -105,8 +97,6 @@ def post_text(module, sms_service, account_sid, auth_token, msg,
request.add_header('Accept', 'application/ansible') request.add_header('Accept', 'application/ansible')
request.add_header('Authorization', 'Basic %s' % base64string) request.add_header('Authorization', 'Basic %s' % base64string)
return urllib2.urlopen(request, encoded_data) return urllib2.urlopen(request, encoded_data)
else:
raise Exception('unknown messaging service')
# ======================================= # =======================================
@ -117,7 +107,6 @@ def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
sms_service=dict(default='twilio', choices=['twilio', ]),
account_sid=dict(required=True), account_sid=dict(required=True),
auth_token=dict(required=True), auth_token=dict(required=True),
msg=dict(required=True), msg=dict(required=True),
@ -127,7 +116,6 @@ def main():
supports_check_mode=True supports_check_mode=True
) )
sms_service = module.params['sms_service']
account_sid = module.params['account_sid'] account_sid = module.params['account_sid']
auth_token = module.params['auth_token'] auth_token = module.params['auth_token']
msg = module.params['msg'] msg = module.params['msg']
@ -135,8 +123,8 @@ def main():
to_number = module.params['to_number'] to_number = module.params['to_number']
try: try:
response = post_text(module, sms_service, account_sid, auth_token, response = post_text(module, account_sid, auth_token, msg,
msg, from_number, to_number) from_number, to_number)
except Exception, e: except Exception, e:
module.fail_json(msg="unable to send text message to %s" % to_number) module.fail_json(msg="unable to send text message to %s" % to_number)