2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 22:45:15 +02:00
|
|
|
# Copyright (c) 2016, Artem Feofanov <artem.feofanov@gmail.com>
|
2022-08-05 13:17:19 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
|
|
|
|
module: telegram
|
2021-01-25 13:43:47 +01:00
|
|
|
author:
|
|
|
|
- "Artem Feofanov (@tyouxa)"
|
|
|
|
- "Nikolai Lomov (@lomserman)"
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
short_description: module for sending notifications via telegram
|
|
|
|
|
|
|
|
description:
|
2021-01-25 13:43:47 +01:00
|
|
|
- Send notifications via telegram bot, to a verified group or user.
|
|
|
|
- Also, the user may try to use any other telegram bot API method, if you specify I(api_method) argument.
|
2020-03-09 10:11:07 +01:00
|
|
|
notes:
|
|
|
|
- You will require a telegram account and create telegram bot to use this module.
|
|
|
|
options:
|
2021-01-25 13:43:47 +01:00
|
|
|
token:
|
2020-11-05 06:50:47 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2021-01-25 13:43:47 +01:00
|
|
|
- Token identifying your telegram bot.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
2021-01-25 13:43:47 +01:00
|
|
|
api_method:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- Bot API method.
|
|
|
|
- For reference, see U(https://core.telegram.org/bots/api).
|
|
|
|
default: SendMessage
|
|
|
|
version_added: 2.0.0
|
|
|
|
api_args:
|
|
|
|
type: dict
|
|
|
|
description:
|
|
|
|
- Any parameters for the method.
|
|
|
|
- For reference to default method, C(SendMessage), see U(https://core.telegram.org/bots/api#sendmessage).
|
|
|
|
version_added: 2.0.0
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
|
2021-01-25 13:43:47 +01:00
|
|
|
- name: Send notify to Telegram
|
|
|
|
community.general.telegram:
|
|
|
|
token: '9999999:XXXXXXXXXXXXXXXXXXXXXXX'
|
|
|
|
api_args:
|
|
|
|
chat_id: 000000
|
|
|
|
parse_mode: "markdown"
|
|
|
|
text: "Your precious application has been deployed: https://example.com"
|
|
|
|
disable_web_page_preview: True
|
|
|
|
disable_notification: True
|
|
|
|
|
|
|
|
- name: Forward message to someone
|
|
|
|
community.general.telegram:
|
|
|
|
token: '9999999:XXXXXXXXXXXXXXXXXXXXXXX'
|
|
|
|
api_method: forwardMessage
|
|
|
|
api_args:
|
|
|
|
chat_id: 000000
|
|
|
|
from_chat_id: 111111
|
|
|
|
disable_notification: True
|
|
|
|
message_id: '{{ saved_msg_id }}'
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
|
|
|
|
msg:
|
|
|
|
description: The message you attempted to send
|
|
|
|
returned: success
|
|
|
|
type: str
|
|
|
|
sample: "Ansible task finished"
|
|
|
|
telegram_error:
|
|
|
|
description: Error message gotten from Telegram API
|
|
|
|
returned: failure
|
|
|
|
type: str
|
|
|
|
sample: "Bad Request: message text is empty"
|
|
|
|
"""
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-01-25 13:43:47 +01:00
|
|
|
# noinspection PyUnresolvedReferences
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible.module_utils.six.moves.urllib.parse import quote
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
token=dict(type='str', required=True, no_log=True),
|
2021-01-25 13:43:47 +01:00
|
|
|
api_args=dict(type='dict'),
|
|
|
|
api_method=dict(type="str", default="SendMessage"),
|
|
|
|
),
|
2020-03-09 10:11:07 +01:00
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
token = quote(module.params.get('token'))
|
2021-01-25 13:43:47 +01:00
|
|
|
api_args = module.params.get('api_args') or {}
|
|
|
|
api_method = module.params.get('api_method')
|
|
|
|
# filling backward compatibility args
|
2021-10-12 13:56:15 +02:00
|
|
|
api_args['chat_id'] = api_args.get('chat_id')
|
|
|
|
api_args['parse_mode'] = api_args.get('parse_mode')
|
|
|
|
api_args['text'] = api_args.get('text')
|
2021-01-25 13:43:47 +01:00
|
|
|
|
|
|
|
if api_args['parse_mode'] == 'plain':
|
|
|
|
del api_args['parse_mode']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-01-25 13:43:47 +01:00
|
|
|
url = 'https://api.telegram.org/bot{token}/{api_method}'.format(token=token, api_method=api_method)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
|
2021-01-25 13:43:47 +01:00
|
|
|
response, info = fetch_url(module, url, method="POST", data=json.dumps(api_args),
|
|
|
|
headers={'Content-Type': 'application/json'})
|
2020-03-09 10:11:07 +01:00
|
|
|
if info['status'] == 200:
|
|
|
|
module.exit_json(changed=True)
|
2021-01-25 13:43:47 +01:00
|
|
|
elif info['status'] == -1:
|
|
|
|
# SSL errors, connection problems, etc.
|
|
|
|
module.fail_json(msg="Failed to send message", info=info, response=response)
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
body = json.loads(info['body'])
|
2021-01-25 13:43:47 +01:00
|
|
|
module.fail_json(
|
|
|
|
msg="Failed to send message, return status = {status}\n"
|
|
|
|
"url = {api_url}\n"
|
|
|
|
"api_args = {api_args}".format(
|
|
|
|
status=info['status'], api_url=url, api_args=api_args
|
|
|
|
),
|
|
|
|
telegram_error=body['description'],
|
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|