From 47a19fad1bd23cd1d358d1538346bf9a977ed570 Mon Sep 17 00:00:00 2001 From: Bronislav Date: Sat, 16 May 2026 01:30:13 +0400 Subject: [PATCH] telegram: add ability to specify a custom URL for the API (#12040) * feat: Custom telegram api host * fix: default param telegram api host * fix: default api_host for DOCUMENTATION * fix: Documentation and example * changelog: add bugfix fragment for telegram api_host * fix: use [] for module.params access --- .../fragments/12040-telegram-api_host.yaml | 2 ++ plugins/modules/telegram.py | 27 ++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/12040-telegram-api_host.yaml diff --git a/changelogs/fragments/12040-telegram-api_host.yaml b/changelogs/fragments/12040-telegram-api_host.yaml new file mode 100644 index 0000000000..45165761f5 --- /dev/null +++ b/changelogs/fragments/12040-telegram-api_host.yaml @@ -0,0 +1,2 @@ +bugfixes: + - "telegram - added the ``api_host`` parameter to fix connectivity with self-hosted proxies and custom API endpoints (https://github.com/ansible-collections/community.general/pull/12040)." diff --git a/plugins/modules/telegram.py b/plugins/modules/telegram.py index acf3dc89e6..079cbfc8ee 100644 --- a/plugins/modules/telegram.py +++ b/plugins/modules/telegram.py @@ -32,6 +32,12 @@ options: description: - Token identifying your telegram bot. required: true + api_host: + type: str + description: + - Custom telegram API host. + default: api.telegram.org + version_added: 13.0.0 api_method: type: str description: @@ -67,6 +73,17 @@ EXAMPLES = r""" from_chat_id: 111111 disable_notification: true message_id: '{{ saved_msg_id }}' + +- name: Send notify to custom telegram API host + community.general.telegram: + token: '9999999:XXXXXXXXXXXXXXXXXXXXXXX' + api_host: "telegram.example.com" + 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 """ RETURN = r""" @@ -95,15 +112,17 @@ def main(): module = AnsibleModule( argument_spec=dict( token=dict(type="str", required=True, no_log=True), + api_host=dict(type="str", default="api.telegram.org"), api_args=dict(type="dict"), api_method=dict(type="str", default="SendMessage"), ), supports_check_mode=True, ) - token = quote(module.params.get("token")) - api_args = module.params.get("api_args") or {} - api_method = module.params.get("api_method") + token = quote(module.params["token"]) + api_host = module.params["api_host"] + api_args = module.params["api_args"] or {} + api_method = module.params["api_method"] # filling backward compatibility args api_args["chat_id"] = api_args.get("chat_id") api_args["parse_mode"] = api_args.get("parse_mode") @@ -112,7 +131,7 @@ def main(): if api_args["parse_mode"] == "plain": del api_args["parse_mode"] - url = f"https://api.telegram.org/bot{token}/{api_method}" + url = f"https://{api_host}/bot{token}/{api_method}" if module.check_mode: module.exit_json(changed=False)