twilio 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2014, Matt Makai <matthew.makai@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = '''
---
23
version_added: "1.6"
24
module: twilio
25
short_description: Sends a text message to a mobile phone through Twilio.
26
description:
27
   - Sends a text message to a phone number through an the Twilio SMS service. 
28 29 30
notes:
   - Like the other notification modules, this one requires an external 
     dependency to work. In this case, you'll need a Twilio account with
31
     a purchased or verified phone number to send the text message.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
options:
  account_sid:
    description:
      user's account id for Twilio found on the account page
    required: true
  auth_token:
    description: user's authentication token for Twilio found on the account page
    required: true
  msg:
    description:
      the body of the text message
    required: true
  to_number:
    description:
      what phone number to send the text message to, format +15551112222
    required: true
  from_number:
    description:
      what phone number to send the text message from, format +15551112222
    required: true
  
requirements: [ urllib, urllib2 ]
author: Matt Makai
'''

EXAMPLES = '''
# send a text message from the local server about the build status to (555) 303 5681
# note: you have to have purchased the 'from_number' on your Twilio account
- local_action: text msg="All servers with webserver role are now configured." 
  account_sid={{ twilio_account_sid }}
  auth_token={{ twilio_auth_token }}
  from_number=+15552014545 to_number=+15553035681

# send a text message from a server to (555) 111 3232
# note: you have to have purchased the 'from_number' on your Twilio account
- text: msg="This server's configuration is now complete."
  account_sid={{ twilio_account_sid }}
  auth_token={{ twilio_auth_token }}
  from_number=+15553258899 to_number=+15551113232
  
'''

# =======================================
# text module support methods
#
try:
    import urllib, urllib2
except ImportError:
    module.fail_json(msg="urllib and urllib2 are required")

import base64


85 86 87 88
def post_text(module, account_sid, auth_token, msg, from_number, to_number):
    URI = "https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json" \
        % (account_sid,)
    AGENT = "Ansible/1.5"
89

90 91 92 93 94 95 96 97 98 99
    data = {'From':from_number, 'To':to_number, 'Body':msg}
    encoded_data = urllib.urlencode(data)
    request = urllib2.Request(URI)
    base64string = base64.encodestring('%s:%s' % \
        (account_sid, auth_token)).replace('\n', '')
    request.add_header('User-Agent', AGENT)
    request.add_header('Content-type', 'application/x-www-form-urlencoded')
    request.add_header('Accept', 'application/ansible')
    request.add_header('Authorization', 'Basic %s' % base64string)
    return urllib2.urlopen(request, encoded_data)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125


# =======================================
# Main
#

def main():

    module = AnsibleModule(
        argument_spec=dict(
            account_sid=dict(required=True),
            auth_token=dict(required=True),
            msg=dict(required=True),
            from_number=dict(required=True),
            to_number=dict(required=True),
        ),
        supports_check_mode=True
    )
  
    account_sid = module.params['account_sid']
    auth_token = module.params['auth_token']
    msg = module.params['msg']
    from_number = module.params['from_number']
    to_number = module.params['to_number']

    try:
126 127
        response = post_text(module, account_sid, auth_token, msg, 
            from_number, to_number)
128 129 130 131 132 133 134 135
    except Exception, e:
        module.fail_json(msg="unable to send text message to %s" % to_number)

    module.exit_json(msg=msg, changed=False) 

# import module snippets
from ansible.module_utils.basic import *
main()