| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2016, Artem Feofanov <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: telegram |
| 11 |
author: |
| 12 |
- "Artem Feofanov (@tyouxa)" |
| 13 |
- "Nikolai Lomov (@lomserman)" |
| 14 |
|
| 15 |
short_description: Send notifications using telegram |
| 16 |
|
| 17 |
description: |
| 18 |
- Send notifications using telegram bot, to a verified group or user. |
| 19 |
- Also, the user may try to use any other telegram bot API method, if you specify O(api_method) argument. |
| 20 |
notes: |
| 21 |
- You need a telegram account and create telegram bot to use this module. |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.general._attributes |
| 24 |
attributes: |
| 25 |
check_mode: |
| 26 |
support: full |
| 27 |
diff_mode: |
| 28 |
support: none |
| 29 |
options: |
| 30 |
token: |
| 31 |
type: str |
| 32 |
description: |
| 33 |
- Token identifying your telegram bot. |
| 34 |
required: true |
| 35 |
api_host: |
| 36 |
type: str |
| 37 |
description: |
| 38 |
- Custom telegram API host. |
| 39 |
default: api.telegram.org |
| 40 |
version_added: 13.0.0 |
| 41 |
api_method: |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- Bot API method. |
| 45 |
- For reference, see U(https://core.telegram.org/bots/api). |
| 46 |
default: SendMessage |
| 47 |
version_added: 2.0.0 |
| 48 |
api_args: |
| 49 |
type: dict |
| 50 |
description: |
| 51 |
- Any parameters for the method. |
| 52 |
- For reference to default method, V(SendMessage), see U(https://core.telegram.org/bots/api#sendmessage). |
| 53 |
version_added: 2.0.0 |
| 54 |
""" |
| 55 |
|
| 56 |
EXAMPLES = r""" |
| 57 |
- name: Send notify to Telegram |
| 58 |
community.general.telegram: |
| 59 |
token: '9999999:XXXXXXXXXXXXXXXXXXXXXXX' |
| 60 |
api_args: |
| 61 |
chat_id: "000000" |
| 62 |
parse_mode: "markdown" |
| 63 |
text: "Your precious application has been deployed: https://example.com" |
| 64 |
disable_web_page_preview: true |
| 65 |
disable_notification: true |
| 66 |
|
| 67 |
- name: Forward message to someone |
| 68 |
community.general.telegram: |
| 69 |
token: '9999999:XXXXXXXXXXXXXXXXXXXXXXX' |
| 70 |
api_method: forwardMessage |
| 71 |
api_args: |
| 72 |
chat_id: "000000" |
| 73 |
from_chat_id: 111111 |
| 74 |
disable_notification: true |
| 75 |
message_id: '{{ saved_msg_id }}' |
| 76 |
|
| 77 |
- name: Send notify to custom telegram API host |
| 78 |
community.general.telegram: |
| 79 |
token: '9999999:XXXXXXXXXXXXXXXXXXXXXXX' |
| 80 |
api_host: "telegram.example.com" |
| 81 |
api_args: |
| 82 |
chat_id: "000000" |
| 83 |
parse_mode: "markdown" |
| 84 |
text: "Your precious application has been deployed: https://example.com" |
| 85 |
disable_web_page_preview: true |
| 86 |
disable_notification: true |
| 87 |
""" |
| 88 |
|
| 89 |
RETURN = r""" |
| 90 |
msg: |
| 91 |
description: The message you attempted to send. |
| 92 |
returned: success |
| 93 |
type: str |
| 94 |
sample: "Ansible task finished" |
| 95 |
telegram_error: |
| 96 |
description: Error message gotten from Telegram API. |
| 97 |
returned: failure |
| 98 |
type: str |
| 99 |
sample: "Bad Request: message text is empty" |
| 100 |
""" |
| 101 |
|
| 102 |
import json |
| 103 |
from urllib.parse import quote |
| 104 |
|
| 105 |
from ansible.module_utils.basic import AnsibleModule |
| 106 |
|
| 107 |
# noinspection PyUnresolvedReferences |
| 108 |
from ansible.module_utils.urls import fetch_url |
| 109 |
|
| 110 |
|
| 111 |
def main(): |
| 112 |
module = AnsibleModule( |
| 113 |
argument_spec=dict( |
| 114 |
token=dict(type="str", required=True, no_log=True), |
| 115 |
api_host=dict(type="str", default="api.telegram.org"), |
| 116 |
api_args=dict(type="dict"), |
| 117 |
api_method=dict(type="str", default="SendMessage"), |
| 118 |
), |
| 119 |
supports_check_mode=True, |
| 120 |
) |
| 121 |
|
| 122 |
token = quote(module.params["token"]) |
| 123 |
api_host = module.params["api_host"] |
| 124 |
api_args = module.params["api_args"] or {} |
| 125 |
api_method = module.params["api_method"] |
| 126 |
# filling backward compatibility args |
| 127 |
api_args["chat_id"] = api_args.get("chat_id") |
| 128 |
api_args["parse_mode"] = api_args.get("parse_mode") |
| 129 |
api_args["text"] = api_args.get("text") |
| 130 |
|
| 131 |
if api_args["parse_mode"] == "plain": |
| 132 |
del api_args["parse_mode"] |
| 133 |
|
| 134 |
url = f"https://{api_host}/bot{token}/{api_method}" |
| 135 |
|
| 136 |
if module.check_mode: |
| 137 |
module.exit_json(changed=False) |
| 138 |
|
| 139 |
response, info = fetch_url( |
| 140 |
module, url, method="POST", data=json.dumps(api_args), headers={"Content-Type": "application/json"} |
| 141 |
) |
| 142 |
if info["status"] == 200: |
| 143 |
module.exit_json(changed=True) |
| 144 |
elif info["status"] == -1: |
| 145 |
# SSL errors, connection problems, etc. |
| 146 |
module.fail_json(msg="Failed to send message", info=info, response=response) |
| 147 |
else: |
| 148 |
body = json.loads(info["body"]) |
| 149 |
module.fail_json( |
| 150 |
msg=f"Failed to send message, return status = {info['status']}\nurl = {url}\napi_args = {api_args}", |
| 151 |
telegram_error=body["description"], |
| 152 |
) |
| 153 |
|
| 154 |
|
| 155 |
if __name__ == "__main__": |
| 156 |
main() |