This is an automated email from the ASF dual-hosted git repository.
eladkal pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new e5688b9ae9 Add telegram connection with documnetation (#37515)
e5688b9ae9 is described below
commit e5688b9ae9556f3e6da3b55437bea803fd0bb444
Author: rom sharon <[email protected]>
AuthorDate: Sun Feb 18 21:28:38 2024 +0200
Add telegram connection with documnetation (#37515)
* add telegram connection with documnetation
* add to index
* Update provider.yaml
Co-authored-by: Andrey Anshin <[email protected]>
---------
Co-authored-by: Andrey Anshin <[email protected]>
---
airflow/providers/telegram/hooks/telegram.py | 21 ++++++++++--
airflow/providers/telegram/provider.yaml | 4 +++
.../connections.rst | 39 ++++++++++++++++++++++
docs/apache-airflow-providers-telegram/index.rst | 1 +
.../operators.rst | 2 +-
tests/providers/telegram/hooks/test_telegram.py | 8 ++---
6 files changed, 67 insertions(+), 8 deletions(-)
diff --git a/airflow/providers/telegram/hooks/telegram.py
b/airflow/providers/telegram/hooks/telegram.py
index 3d67d3bea7..514e0f7875 100644
--- a/airflow/providers/telegram/hooks/telegram.py
+++ b/airflow/providers/telegram/hooks/telegram.py
@@ -19,6 +19,7 @@
from __future__ import annotations
import asyncio
+from typing import Any
import telegram
import tenacity
@@ -38,14 +39,15 @@ class TelegramHook(BaseHook):
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'
+ conn_type: 'telegram'
+ password: 'TELEGRAM_TOKEN' (optional)
host: 'chat_id' (optional)
Examples:
.. code-block:: python
# Create hook
telegram_hook = TelegramHook(telegram_conn_id="telegram_default")
+ telegram_hook = TelegramHook() # will use telegram_default
# or telegram_hook = TelegramHook(telegram_conn_id='telegram_default',
chat_id='-1xxx')
# or telegram_hook = TelegramHook(token='xxx:xxx', chat_id='-1xxx')
@@ -58,9 +60,14 @@ class TelegramHook(BaseHook):
:param chat_id: optional chat_id of the telegram chat/channel/group
"""
+ conn_name_attr = "telegram_conn_id"
+ default_conn_name = "telegram_default"
+ conn_type = "telegram"
+ hook_name = "Telegram"
+
def __init__(
self,
- telegram_conn_id: str | None = None,
+ telegram_conn_id: str | None = default_conn_name,
token: str | None = None,
chat_id: str | None = None,
) -> None:
@@ -69,6 +76,14 @@ class TelegramHook(BaseHook):
self.chat_id = self.__get_chat_id(chat_id, telegram_conn_id)
self.connection = self.get_conn()
+ @classmethod
+ def get_ui_field_behaviour(cls) -> dict[str, Any]:
+ """Return custom field behaviour."""
+ return {
+ "hidden_fields": ["schema", "extra", "login", "port", "extra"],
+ "relabeling": {},
+ }
+
def get_conn(self) -> telegram.Bot:
"""
Return the telegram bot client.
diff --git a/airflow/providers/telegram/provider.yaml
b/airflow/providers/telegram/provider.yaml
index 97637b913f..3d90502d0f 100644
--- a/airflow/providers/telegram/provider.yaml
+++ b/airflow/providers/telegram/provider.yaml
@@ -60,6 +60,10 @@ operators:
python-modules:
- airflow.providers.telegram.operators.telegram
+connection-types:
+ - hook-class-name: airflow.providers.telegram.hooks.telegram.TelegramHook
+ connection-type: telegram
+
hooks:
- integration-name: Telegram
python-modules:
diff --git a/docs/apache-airflow-providers-telegram/connections.rst
b/docs/apache-airflow-providers-telegram/connections.rst
new file mode 100644
index 0000000000..3cd628e587
--- /dev/null
+++ b/docs/apache-airflow-providers-telegram/connections.rst
@@ -0,0 +1,39 @@
+ .. Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ .. http://www.apache.org/licenses/LICENSE-2.0
+
+ .. Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+
+.. _howto/connection:telegram:
+
+Connecting to Telegram
+======================
+
+The Telegram connection type enable connection to Telegram that allows you to
post messages to Telegram using the telegram python-telegram-bot library.
+
+After installing the Telegram provider in your Airflow environment, the
corresponding connection type of ``telegram`` will be made available.
+
+Default Connection IDs
+----------------------
+
+Telegram Hook uses the parameter ``telegram_conn_id`` for Connection IDs and
the value of the parameter as ``telegram_default`` by default. You can create
multiple connections in case you want to switch between environments.
+
+Configuring the Connection
+--------------------------
+
+Password (optional)
+ The Telegram API token
+
+Host (optional)
+ The Chat ID of the telegram chat/channel/group
diff --git a/docs/apache-airflow-providers-telegram/index.rst
b/docs/apache-airflow-providers-telegram/index.rst
index 6e6a6f7d3a..efff17ee43 100644
--- a/docs/apache-airflow-providers-telegram/index.rst
+++ b/docs/apache-airflow-providers-telegram/index.rst
@@ -35,6 +35,7 @@
:caption: Guides
Operators <operators>
+ Connection types <connections>
.. toctree::
:hidden:
diff --git a/docs/apache-airflow-providers-telegram/operators.rst
b/docs/apache-airflow-providers-telegram/operators.rst
index e317264b3a..0f3ad86b19 100644
--- a/docs/apache-airflow-providers-telegram/operators.rst
+++ b/docs/apache-airflow-providers-telegram/operators.rst
@@ -44,7 +44,7 @@ the connection metadata is structured as follows:
* - Host: string
- Chat ID of the Telegram group/chat
* - Connection Type: string
- - http as connection type
+ - telegram as connection type
An example usage of the TelegramOperator is as follows:
diff --git a/tests/providers/telegram/hooks/test_telegram.py
b/tests/providers/telegram/hooks/test_telegram.py
index d2bb17e974..b5888eb019 100644
--- a/tests/providers/telegram/hooks/test_telegram.py
+++ b/tests/providers/telegram/hooks/test_telegram.py
@@ -62,11 +62,11 @@ class TestTelegramHook:
)
)
- def
test_should_raise_exception_if_both_connection_or_token_is_not_provided(self):
- with pytest.raises(airflow.exceptions.AirflowException) as ctx:
- TelegramHook()
+ def test_should_use_default_connection(self):
+ hook = TelegramHook()
- assert "Cannot get token: No valid Telegram connection supplied." ==
str(ctx.value)
+ assert hook.token == TELEGRAM_TOKEN
+ assert hook.chat_id is None
def test_should_raise_exception_if_conn_id_doesnt_exist(self):
with pytest.raises(airflow.exceptions.AirflowNotFoundException) as ctx: