blob: 6471df5df3be3090b35a66a4f126bf998de3fd23 [file] [log] [blame]
:mod:`airflow.providers.telegram.hooks.telegram`
================================================
.. py:module:: airflow.providers.telegram.hooks.telegram
.. autoapi-nested-parse::
Hook for Telegram
Module Contents
---------------
.. py:class:: TelegramHook(telegram_conn_id: Optional[str] = None, token: Optional[str] = None, chat_id: Optional[str] = None)
Bases: :class:`airflow.hooks.base.BaseHook`
This hook allows you to post messages to Telegram using the telegram python-telegram-bot library.
The library can be found here: https://github.com/python-telegram-bot/python-telegram-bot
It accepts both telegram bot API token directly or connection that has telegram bot API token.
If both supplied, token parameter will be given precedence, otherwise 'password' field in the connection
from telegram_conn_id will be used.
chat_id can also be provided in the connection using 'host' field in connection.
Following is the details of a telegram_connection:
name: 'telegram-connection-name'
conn_type: 'http'
password: 'TELEGRAM_TOKEN'
host: 'chat_id' (optional)
Examples:
.. code-block:: python
# Create hook
telegram_hook = TelegramHook(telegram_conn_id='telegram_default')
# or telegram_hook = TelegramHook(telegram_conn_id='telegram_default', chat_id='-1xxx')
# or telegram_hook = TelegramHook(token='xxx:xxx', chat_id='-1xxx')
# Call method from telegram bot client
telegram_hook.send_message(None', {"text": "message", "chat_id": "-1xxx"})
# or telegram_hook.send_message(None', {"text": "message"})
:param telegram_conn_id: connection that optionally has Telegram API token in the password field
:type telegram_conn_id: str
:param token: optional telegram API token
:type token: str
:param chat_id: optional chat_id of the telegram chat/channel/group
:type chat_id: str
.. method:: get_conn(self)
Returns the telegram bot client
:return: telegram bot client
:rtype: telegram.bot.Bot
.. method:: __get_token(self, token: Optional[str], telegram_conn_id: str)
Returns the telegram API token
:param token: telegram API token
:type token: str
:param telegram_conn_id: telegram connection name
:type telegram_conn_id: str
:return: telegram API token
:rtype: str
.. method:: __get_chat_id(self, chat_id: Optional[str], telegram_conn_id: str)
Returns the telegram chat ID for a chat/channel/group
:param chat_id: optional chat ID
:type chat_id: str
:param telegram_conn_id: telegram connection name
:type telegram_conn_id: str
:return: telegram chat ID
:rtype: str
.. method:: send_message(self, api_params: dict)
Sends the message to a telegram channel or chat.
:param api_params: params for telegram_instance.send_message. It can also be used to override chat_id
:type api_params: dict