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:
parent
a075c56dbf
commit
1cf607d8a8
2 changed files with 34 additions and 12 deletions
|
@ -42,6 +42,14 @@ options:
|
||||||
description:
|
description:
|
||||||
- What message you wish to send.
|
- What message you wish to send.
|
||||||
required: true
|
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:
|
token:
|
||||||
description:
|
description:
|
||||||
- Token identifying your telegram bot.
|
- Token identifying your telegram bot.
|
||||||
|
@ -57,7 +65,7 @@ EXAMPLES = """
|
||||||
|
|
||||||
- name: send a message to chat in playbook
|
- name: send a message to chat in playbook
|
||||||
telegram:
|
telegram:
|
||||||
token: 'bot9999999:XXXXXXXXXXXXXXXXXXXXXXX'
|
token: '9999999:XXXXXXXXXXXXXXXXXXXXXXX'
|
||||||
chat_id: 000000
|
chat_id: 000000
|
||||||
msg: Ansible task finished
|
msg: Ansible task finished
|
||||||
"""
|
"""
|
||||||
|
@ -69,25 +77,38 @@ msg:
|
||||||
returned: success
|
returned: success
|
||||||
type: string
|
type: string
|
||||||
sample: "Ansible task finished"
|
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():
|
def main():
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec = dict(
|
argument_spec=dict(
|
||||||
token = dict(type='str',required=True,no_log=True),
|
token=dict(type='str', required=True, no_log=True),
|
||||||
chat_id = dict(type='str',required=True,no_log=True),
|
chat_id=dict(type='str', required=True, no_log=True),
|
||||||
msg = dict(type='str',required=True)),
|
msg_format=dict(type='str', required=False, default='plain',
|
||||||
|
choices=['plain', 'markdown', 'html']),
|
||||||
|
msg=dict(type='str', required=True)),
|
||||||
supports_check_mode=True
|
supports_check_mode=True
|
||||||
)
|
)
|
||||||
|
|
||||||
token = urllib.quote(module.params.get('token'))
|
token = quote(module.params.get('token'))
|
||||||
chat_id = urllib.quote(module.params.get('chat_id'))
|
chat_id = quote(module.params.get('chat_id'))
|
||||||
msg = urllib.quote(module.params.get('msg'))
|
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:
|
if module.check_mode:
|
||||||
module.exit_json(changed=False)
|
module.exit_json(changed=False)
|
||||||
|
@ -96,7 +117,9 @@ def main():
|
||||||
if info['status'] == 200:
|
if info['status'] == 200:
|
||||||
module.exit_json(changed=True)
|
module.exit_json(changed=True)
|
||||||
else:
|
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
|
# import module snippets
|
||||||
|
|
|
@ -659,7 +659,6 @@ lib/ansible/modules/notification/rocketchat.py
|
||||||
lib/ansible/modules/notification/sendgrid.py
|
lib/ansible/modules/notification/sendgrid.py
|
||||||
lib/ansible/modules/notification/slack.py
|
lib/ansible/modules/notification/slack.py
|
||||||
lib/ansible/modules/notification/sns.py
|
lib/ansible/modules/notification/sns.py
|
||||||
lib/ansible/modules/notification/telegram.py
|
|
||||||
lib/ansible/modules/notification/twilio.py
|
lib/ansible/modules/notification/twilio.py
|
||||||
lib/ansible/modules/packaging/language/bundler.py
|
lib/ansible/modules/packaging/language/bundler.py
|
||||||
lib/ansible/modules/packaging/language/cpanm.py
|
lib/ansible/modules/packaging/language/cpanm.py
|
||||||
|
|
Loading…
Reference in a new issue