1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
This commit is contained in:
Dmitry Sobolev 2017-04-09 00:53:35 +05:00 committed by Toshio Kuratomi
parent a075c56dbf
commit 1cf607d8a8
2 changed files with 34 additions and 12 deletions

View file

@ -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

View file

@ -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