| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2015, Matt Makai <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: twilio |
| 11 |
short_description: Sends a text message to a mobile phone through Twilio |
| 12 |
description: |
| 13 |
- Sends a text message to a phone number through the Twilio messaging API. |
| 14 |
notes: |
| 15 |
- This module is non-idempotent because it sends an email through the external API. It is idempotent only in the case that |
| 16 |
the module fails. |
| 17 |
- Like the other notification modules, this one requires an external dependency to work. In this case, you need a Twilio |
| 18 |
account with a purchased or verified phone number to send the text message. |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._attributes |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: full |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
options: |
| 27 |
account_sid: |
| 28 |
type: str |
| 29 |
description: |
| 30 |
- User's Twilio account token found on the account page. |
| 31 |
required: true |
| 32 |
auth_token: |
| 33 |
type: str |
| 34 |
description: |
| 35 |
- User's Twilio authentication token. |
| 36 |
required: true |
| 37 |
msg: |
| 38 |
type: str |
| 39 |
description: |
| 40 |
- The body of the text message. |
| 41 |
required: true |
| 42 |
to_numbers: |
| 43 |
type: list |
| 44 |
elements: str |
| 45 |
description: |
| 46 |
- One or more phone numbers to send the text message to, format C(+15551112222). |
| 47 |
required: true |
| 48 |
aliases: [to_number] |
| 49 |
from_number: |
| 50 |
type: str |
| 51 |
description: |
| 52 |
- The Twilio number to send the text message from, format C(+15551112222). |
| 53 |
required: true |
| 54 |
media_url: |
| 55 |
type: str |
| 56 |
description: |
| 57 |
- A URL with a picture, video or sound clip to send with an MMS (multimedia message) instead of a plain SMS. |
| 58 |
|
| 59 |
author: "Matt Makai (@makaimc)" |
| 60 |
""" |
| 61 |
|
| 62 |
EXAMPLES = r""" |
| 63 |
# send an SMS about the build status to (555) 303 5681 |
| 64 |
# note: replace account_sid and auth_token values with your credentials |
| 65 |
# and you have to have the 'from_number' on your Twilio account |
| 66 |
- name: Send a text message to a mobile phone through Twilio |
| 67 |
community.general.twilio: |
| 68 |
msg: All servers with webserver role are now configured. |
| 69 |
account_sid: ACXXXXXXXXXXXXXXXXX |
| 70 |
auth_token: ACXXXXXXXXXXXXXXXXX |
| 71 |
from_number: "+15552014545" |
| 72 |
to_number: "+15553035681" |
| 73 |
delegate_to: localhost |
| 74 |
|
| 75 |
# send an SMS to multiple phone numbers about the deployment |
| 76 |
# note: replace account_sid and auth_token values with your credentials |
| 77 |
# and you have to have the 'from_number' on your Twilio account |
| 78 |
- name: Send a text message to a mobile phone through Twilio |
| 79 |
community.general.twilio: |
| 80 |
msg: This server configuration is now complete. |
| 81 |
account_sid: ACXXXXXXXXXXXXXXXXX |
| 82 |
auth_token: ACXXXXXXXXXXXXXXXXX |
| 83 |
from_number: "+15553258899" |
| 84 |
to_numbers: |
| 85 |
- "+15551113232" |
| 86 |
- "+12025551235" |
| 87 |
- "+19735559010" |
| 88 |
delegate_to: localhost |
| 89 |
|
| 90 |
# send an MMS to a single recipient with an update on the deployment |
| 91 |
# and an image of the results |
| 92 |
# note: replace account_sid and auth_token values with your credentials |
| 93 |
# and you have to have the 'from_number' on your Twilio account |
| 94 |
- name: Send a text message to a mobile phone through Twilio |
| 95 |
community.general.twilio: |
| 96 |
msg: Deployment complete! |
| 97 |
account_sid: ACXXXXXXXXXXXXXXXXX |
| 98 |
auth_token: ACXXXXXXXXXXXXXXXXX |
| 99 |
from_number: "+15552014545" |
| 100 |
to_number: "+15553035681" |
| 101 |
media_url: https://demo.twilio.com/logo.png |
| 102 |
delegate_to: localhost |
| 103 |
""" |
| 104 |
|
| 105 |
# ======================================= |
| 106 |
# twilio module support methods |
| 107 |
# |
| 108 |
from urllib.parse import urlencode |
| 109 |
|
| 110 |
from ansible.module_utils.basic import AnsibleModule |
| 111 |
from ansible.module_utils.urls import fetch_url |
| 112 |
|
| 113 |
|
| 114 |
def post_twilio_api(module, account_sid, auth_token, msg, from_number, to_number, media_url=None): |
| 115 |
URI = f"https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json" |
| 116 |
AGENT = "Ansible" |
| 117 |
|
| 118 |
data = {"From": from_number, "To": to_number, "Body": msg} |
| 119 |
if media_url: |
| 120 |
data["MediaUrl"] = media_url |
| 121 |
encoded_data = urlencode(data) |
| 122 |
|
| 123 |
headers = { |
| 124 |
"User-Agent": AGENT, |
| 125 |
"Content-type": "application/x-www-form-urlencoded", |
| 126 |
"Accept": "application/json", |
| 127 |
} |
| 128 |
|
| 129 |
# Hack module params to have the Basic auth params that fetch_url expects |
| 130 |
module.params["url_username"] = account_sid.replace("\n", "") |
| 131 |
module.params["url_password"] = auth_token.replace("\n", "") |
| 132 |
|
| 133 |
return fetch_url(module, URI, data=encoded_data, headers=headers) |
| 134 |
|
| 135 |
|
| 136 |
# ======================================= |
| 137 |
# Main |
| 138 |
# |
| 139 |
|
| 140 |
|
| 141 |
def main(): |
| 142 |
module = AnsibleModule( |
| 143 |
argument_spec=dict( |
| 144 |
account_sid=dict(required=True), |
| 145 |
auth_token=dict(required=True, no_log=True), |
| 146 |
msg=dict(required=True), |
| 147 |
from_number=dict(required=True), |
| 148 |
to_numbers=dict(required=True, aliases=["to_number"], type="list", elements="str"), |
| 149 |
media_url=dict(), |
| 150 |
), |
| 151 |
supports_check_mode=True, |
| 152 |
) |
| 153 |
|
| 154 |
account_sid = module.params["account_sid"] |
| 155 |
auth_token = module.params["auth_token"] |
| 156 |
msg = module.params["msg"] |
| 157 |
from_number = module.params["from_number"] |
| 158 |
to_numbers = module.params["to_numbers"] |
| 159 |
media_url = module.params["media_url"] |
| 160 |
|
| 161 |
for number in to_numbers: |
| 162 |
r, info = post_twilio_api(module, account_sid, auth_token, msg, from_number, number, media_url) |
| 163 |
if info["status"] not in [200, 201]: |
| 164 |
body_message = "unknown error" |
| 165 |
if "body" in info: |
| 166 |
body = module.from_json(info["body"]) |
| 167 |
body_message = body["message"] |
| 168 |
module.fail_json(msg=f"unable to send message to {number}: {body_message}") |
| 169 |
|
| 170 |
module.exit_json(msg=msg, changed=False) |
| 171 |
|
| 172 |
|
| 173 |
if __name__ == "__main__": |
| 174 |
main() |