From 1cf607d8a82324ec69299338c859723511b53928 Mon Sep 17 00:00:00 2001 From: Dmitry Sobolev Date: Sun, 9 Apr 2017 00:53:35 +0500 Subject: [PATCH] Telegram notification module updated (#22858) * msg_format parameter added error message received from telegram API is added to fail json compatibility with python3 added * pep8 formatted * version_added property added for msg_format * bot token must be set without 'bot' prefix in module parameters * formatting options described in documentation * six module for compatibility used telegram.py removed from legacy-files.txt --- lib/ansible/modules/notification/telegram.py | 45 +++++++++++++++----- test/sanity/pep8/legacy-files.txt | 1 - 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/ansible/modules/notification/telegram.py b/lib/ansible/modules/notification/telegram.py index caa405fbdf2..f058acf4f06 100644 --- a/lib/ansible/modules/notification/telegram.py +++ b/lib/ansible/modules/notification/telegram.py @@ -42,6 +42,14 @@ options: description: - What message you wish to send. required: true + msg_format: + description: + - Message format. Formatting options `markdown` and `html` described in + Telegram API docs (https://core.telegram.org/bots/api#formatting-options). + If option `plain` set, message will not be formatted. + default: plain + choices: [ "plain", "markdown", "html" ] + version_added: "2.4" token: description: - Token identifying your telegram bot. @@ -57,7 +65,7 @@ EXAMPLES = """ - name: send a message to chat in playbook telegram: - token: 'bot9999999:XXXXXXXXXXXXXXXXXXXXXXX' + token: '9999999:XXXXXXXXXXXXXXXXXXXXXXX' chat_id: 000000 msg: Ansible task finished """ @@ -69,25 +77,38 @@ msg: returned: success type: string sample: "Ansible task finished" +telegram_error: + description: Error message gotten from Telegram API + returned: failure + type: string + sample: "Bad Request: message text is empty" """ -import urllib +import json +from ansible.module_utils.six.moves.urllib.parse import quote + def main(): module = AnsibleModule( - argument_spec = dict( - token = dict(type='str',required=True,no_log=True), - chat_id = dict(type='str',required=True,no_log=True), - msg = dict(type='str',required=True)), + argument_spec=dict( + token=dict(type='str', required=True, no_log=True), + chat_id=dict(type='str', required=True, no_log=True), + msg_format=dict(type='str', required=False, default='plain', + choices=['plain', 'markdown', 'html']), + msg=dict(type='str', required=True)), supports_check_mode=True ) - token = urllib.quote(module.params.get('token')) - chat_id = urllib.quote(module.params.get('chat_id')) - msg = urllib.quote(module.params.get('msg')) + token = quote(module.params.get('token')) + chat_id = quote(module.params.get('chat_id')) + msg_format = quote(module.params.get('msg_format')) + msg = quote(module.params.get('msg')) - url = 'https://api.telegram.org/' + token + '/sendMessage?text=' + msg + '&chat_id=' + chat_id + url = 'https://api.telegram.org/bot' + token + \ + '/sendMessage?text=' + msg + '&chat_id=' + chat_id + if msg_format in ('markdown', 'html'): + url += '&parse_mode=' + msg_format if module.check_mode: module.exit_json(changed=False) @@ -96,7 +117,9 @@ def main(): if info['status'] == 200: module.exit_json(changed=True) else: - module.fail_json(msg="failed to send message, return status=%s" % str(info['status'])) + body = json.loads(info['body']) + module.fail_json(msg="failed to send message, return status=%s" % str(info['status']), + telegram_error=body['description']) # import module snippets diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 7d8b4716536..f74f8c2124c 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -659,7 +659,6 @@ lib/ansible/modules/notification/rocketchat.py lib/ansible/modules/notification/sendgrid.py lib/ansible/modules/notification/slack.py lib/ansible/modules/notification/sns.py -lib/ansible/modules/notification/telegram.py lib/ansible/modules/notification/twilio.py lib/ansible/modules/packaging/language/bundler.py lib/ansible/modules/packaging/language/cpanm.py