1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/modules/notification/nexmo.py
patchback[bot] a3a33cb019
Tidy up validate-modules ignores for notification modules (#1229) (#1232)
* fixed validation-modules for plugins/modules/notification/bearychat.py

* fixed validation-modules for plugins/modules/notification/campfire.py

* fixed validation-modules for plugins/modules/notification/catapult.py

* fixed validation-modules for plugins/modules/notification/flowdock.py

* fixed validation-modules for plugins/modules/notification/grove.py

* fixed validation-modules for plugins/modules/notification/hipchat.py

* fixed validation-modules for plugins/modules/notification/irc.py

* fixed validation-modules for plugins/modules/notification/jabber.py

* fixed validation-modules for plugins/modules/notification/logentries_msg.py

* fixed validation-modules for plugins/modules/notification/mail.py

* fixed validation-modules for plugins/modules/notification/matrix.py

* fixed validation-modules for plugins/modules/notification/mattermost.py

* fixed validation-modules for plugins/modules/notification/mqtt.py

* fixed validation-modules for plugins/modules/notification/nexmo.py

* fixed validation-modules for plugins/modules/notification/office_365_connector_card.py

* fixed some docs issues in plugins/modules/notification/pushbullet.py, but cannot remove the ignore lines yet

* fixed validation-modules for plugins/modules/notification/pushover.py

* fixed some docs issues in plugins/modules/notification/rocketchat.py, but cannot remove all ignore lines yet

* fixed validation-modules for plugins/modules/notification/say.py

* fixed validation-modules for plugins/modules/notification/sendgrid.py

* fixed validation-modules for plugins/modules/notification/slack.py

* fixed validation-modules for plugins/modules/notification/syslogger.py

* fixed validation-modules for plugins/modules/notification/telegram.py

* fixed validation-modules for plugins/modules/notification/twilio.py

* fixed validation-modules for plugins/modules/notification/typetalk.py

* Enabling validation-modules for modules in: notification

* removed naughty trailing space

(cherry picked from commit 24f780ec9a)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
2020-11-05 07:40:30 +01:00

135 lines
3.4 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2014, Matt Martz <matt@sivel.net>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = '''
module: nexmo
short_description: Send a SMS via nexmo
description:
- Send a SMS message via nexmo
author: "Matt Martz (@sivel)"
options:
api_key:
type: str
description:
- Nexmo API Key
required: true
api_secret:
type: str
description:
- Nexmo API Secret
required: true
src:
type: int
description:
- Nexmo Number to send from
required: true
dest:
type: list
description:
- Phone number(s) to send SMS message to
required: true
msg:
type: str
description:
- Message to text to send. Messages longer than 160 characters will be
split into multiple messages
required: true
validate_certs:
description:
- If C(no), SSL certificates will not be validated. This should only be used
on personally controlled sites using self-signed certificates.
type: bool
default: 'yes'
extends_documentation_fragment:
- url
'''
EXAMPLES = """
- name: Send notification message via Nexmo
community.general.nexmo:
api_key: 640c8a53
api_secret: 0ce239a6
src: 12345678901
dest:
- 10987654321
- 16789012345
msg: '{{ inventory_hostname }} completed'
delegate_to: localhost
"""
import json
from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url, url_argument_spec
NEXMO_API = 'https://rest.nexmo.com/sms/json'
def send_msg(module):
failed = list()
responses = dict()
msg = {
'api_key': module.params.get('api_key'),
'api_secret': module.params.get('api_secret'),
'from': module.params.get('src'),
'text': module.params.get('msg')
}
for number in module.params.get('dest'):
msg['to'] = number
url = "%s?%s" % (NEXMO_API, urlencode(msg))
headers = dict(Accept='application/json')
response, info = fetch_url(module, url, headers=headers)
if info['status'] != 200:
failed.append(number)
responses[number] = dict(failed=True)
try:
responses[number] = json.load(response)
except Exception:
failed.append(number)
responses[number] = dict(failed=True)
else:
for message in responses[number]['messages']:
if int(message['status']) != 0:
failed.append(number)
responses[number] = dict(failed=True, **responses[number])
if failed:
msg = 'One or messages failed to send'
else:
msg = ''
module.exit_json(failed=bool(failed), msg=msg, changed=False,
responses=responses)
def main():
argument_spec = url_argument_spec()
argument_spec.update(
dict(
api_key=dict(required=True, no_log=True),
api_secret=dict(required=True, no_log=True),
src=dict(required=True, type='int'),
dest=dict(required=True, type='list'),
msg=dict(required=True),
),
)
module = AnsibleModule(
argument_spec=argument_spec
)
send_msg(module)
if __name__ == '__main__':
main()