You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/10/26 12:31:12 UTC

[GitHub] [airflow] rootcss opened a new pull request #11850: WIP: Add Telegram hook and operator

rootcss opened a new pull request #11850:
URL: https://github.com/apache/airflow/pull/11850


   closes: https://github.com/apache/airflow/issues/11845
   
   Adds:
   - Telegram Hook
   - Telegram Operator


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-734896677


   ```
   $ time ./build_docs.py --package apache-airflow-providers-telegram --docs-only
   real	0m11.899s
   user	0m9.563s
   sys	0m1.464s
   ```
   ```
   $ time ./build_docs.py --package apache-airflow-providers-telegram
   real	0m17.059s
   user	0m14.816s
   sys	0m2.042s
   ```
   It really is fast.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733060387


   Test failure:
   
   ```
   ________________ TestProviderManager.test_providers_are_loaded _________________
   
   self = <test_providers_manager.TestProviderManager testMethod=test_providers_are_loaded>
   
       def test_providers_are_loaded(self):
           provider_manager = ProvidersManager()
           provider_list = list(provider_manager.providers.keys())
           provider_list.sort()
   >       self.assertEqual(ALL_PROVIDERS, provider_list)
   E       AssertionError: Lists differ: ['apa[2132 chars]ders-vertica', 'apache-airflow-providers-yande[34 chars]esk'] != ['apa[2132 chars]ders-telegram', 'apache-airflow-providers-vert[71 chars]esk']
   E       
   E       First differing element 57:
   E       'apache-airflow-providers-vertica'
   E       'apache-airflow-providers-telegram'
   E       
   E       Second list contains 1 additional elements.
   E       First extra element 60:
   E       'apache-airflow-providers-zendesk'
   E       
   E       Diff is 2442 characters long. Set self.maxDiff to None to see it.
   
   tests/core/test_providers_manager.py:91: AssertionError
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r525099101



##########
File path: airflow/providers/telegram/hooks/telegram.py
##########
@@ -0,0 +1,153 @@
+#
+# 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.
+"""Hook for Telegram"""
+from typing import Optional
+
+import telegram
+import tenacity
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base_hook import BaseHook
+
+
+class TelegramHook(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.call(None', {"text": "message", "chat_id": "-1xxx"})
+        # or telegram_hook.call(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
+    """
+
+    def __init__(
+        self,
+        telegram_conn_id: Optional[str] = None,
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+    ) -> None:
+        super().__init__()
+        self.token = self.__get_token(token, telegram_conn_id)
+        self.chat_id = self.__get_chat_id(chat_id, telegram_conn_id)
+        self.connection = self.get_conn()
+
+    def get_conn(self) -> telegram.bot.Bot:
+        """
+        Returns the telegram bot client
+
+        :return: telegram bot client
+        :rtype: telegram.bot.Bot
+        """
+        return telegram.bot.Bot(token=self.token)
+
+    def __get_token(self, token: Optional[str], telegram_conn_id: str) -> 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
+        """
+        if token is not None:
+            return token
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+
+            if not conn.password:
+                raise AirflowException("Missing token(password) in Telegram connection")
+
+            return conn.password
+
+        raise AirflowException("Cannot get token: No valid Telegram connection supplied.")
+
+    def __get_chat_id(self, chat_id: Optional[str], telegram_conn_id: str) -> Optional[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
+        """
+        if chat_id is not None:
+            return chat_id
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+            return conn.host
+
+        return None
+
+    @tenacity.retry(
+        retry=tenacity.retry_if_exception_type(telegram.error.TelegramError),
+        stop=tenacity.stop_after_attempt(5),
+        wait=tenacity.wait_fixed(1),
+    )
+    def call(self, api_params: dict) -> None:
+        """
+        Sends the message to a telegram channel or chat.
+
+        :param method: not used
+        :type method: str
+        :param api_params: params for telegram_instance.send_message. It can also be used to override chat_id
+        :type api_params: dict
+        """
+        kwargs = {
+            "chat_id": self.chat_id,
+            "parse_mode": telegram.parsemode.ParseMode.HTML,
+            "disable_web_page_preview": True,
+        }
+        kwargs.update(api_params)
+
+        if 'text' not in kwargs or kwargs['text'] is None:
+            raise AirflowException("'text' must be provided for telegram message")
+
+        if kwargs['chat_id'] is None:
+            raise AirflowException("'chat_id' must be provided for telegram message")
+
+        self.log.debug(self.connection.send_message(**kwargs))

Review comment:
       yea please separate it to 2 commands, one that get's response and second that logs it




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] michalslowikowski00 commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
michalslowikowski00 commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r530169606



##########
File path: airflow/providers/telegram/operators/telegram.py
##########
@@ -0,0 +1,80 @@
+#
+# 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.
+"""Operator for Telegram"""
+from typing import Optional
+
+from airflow.exceptions import AirflowException
+from airflow.models import BaseOperator
+from airflow.providers.telegram.hooks.telegram import TelegramHook
+from airflow.utils.decorators import apply_defaults
+
+
+class TelegramOperator(BaseOperator):
+    """
+    This operator allows you to post messages to Telegram using Telegram Bot API.
+    Takes both Telegram Bot API token directly or connection that has Telegram token in password field.
+    If both supplied, token parameter will be given precedence.
+
+    :param telegram_conn_id: Telegram connection ID which its password is Telegram API token
+    :type telegram_conn_id: str
+    :param token: Telegram API Token
+    :type token: str
+    :param chat_id: Telegram chat ID for a chat/channel/group
+    :type chat_id: str
+    :param text: Message to be sent on telegram
+    :type text: str
+    :param telegram_kwargs: Extra args to be passed to telegram client
+    :type telegram_kwargs: dict
+    """
+
+    template_fields = ('text', 'chat_id')
+    ui_color = '#FFBA40'
+
+    @apply_defaults
+    def __init__(
+        self,

Review comment:
       ```suggestion
           self, *,
   ```
   All args have to be a keywords.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-731593734


   Building documentation for new providers does not work. I'm already working on it. See: https://github.com/apache/airflow/pull/12527


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-734906064


   Breeze is just a little slower.
   ```
   $ time ./breeze build-docs -- --package apache-airflow-providers-telegram
   real	0m49.030s
   user	0m1.515s
   sys	0m0.926s
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733943149


   [The Workflow run](https://github.com/apache/airflow/actions/runs/383938202) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-720921259


   @kaxil What's the right place to add package dependencies? I see it's not requirements.txt, Is setup.py the right place? As I can see it has all deps. (just wanted to confirm)


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r530523680



##########
File path: airflow/providers/telegram/hooks/telegram.py
##########
@@ -0,0 +1,154 @@
+#
+# 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.
+"""Hook for Telegram"""
+from typing import Optional
+
+import telegram
+import tenacity
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base_hook import BaseHook
+
+
+class TelegramHook(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.call(None', {"text": "message", "chat_id": "-1xxx"})
+        # or telegram_hook.call(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
+    """
+
+    def __init__(
+        self,
+        telegram_conn_id: Optional[str] = None,
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+    ) -> None:
+        super().__init__()
+        self.token = self.__get_token(token, telegram_conn_id)
+        self.chat_id = self.__get_chat_id(chat_id, telegram_conn_id)
+        self.connection = self.get_conn()
+
+    def get_conn(self) -> telegram.bot.Bot:
+        """
+        Returns the telegram bot client
+
+        :return: telegram bot client
+        :rtype: telegram.bot.Bot
+        """
+        return telegram.bot.Bot(token=self.token)
+
+    def __get_token(self, token: Optional[str], telegram_conn_id: str) -> 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
+        """
+        if token is not None:
+            return token
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+
+            if not conn.password:
+                raise AirflowException("Missing token(password) in Telegram connection")
+
+            return conn.password
+
+        raise AirflowException("Cannot get token: No valid Telegram connection supplied.")
+
+    def __get_chat_id(self, chat_id: Optional[str], telegram_conn_id: str) -> Optional[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
+        """
+        if chat_id is not None:
+            return chat_id
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+            return conn.host
+
+        return None
+
+    @tenacity.retry(
+        retry=tenacity.retry_if_exception_type(telegram.error.TelegramError),
+        stop=tenacity.stop_after_attempt(5),
+        wait=tenacity.wait_fixed(1),
+    )
+    def call(self, api_params: dict) -> None:

Review comment:
       I followed the name from slack hook but makes sense. changed it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-738984810


   Thanks for asking @mik-laj no issues, I couldn't get time to rebase and push. Just did, let's see how it goes. It works locally.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss edited a comment on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss edited a comment on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-735051502


   Failed with same error 🤔 
   <img width="1377" alt="Screenshot 2020-11-28 at 1 02 02 PM" src="https://user-images.githubusercontent.com/4328561/100496416-ebb14800-3179-11eb-80b5-6a76d913da1d.png">
   
   I checked the implementation of `check_doc_files()` in pre_commit_check_provider_yaml_files.py. `/docs/apache-airflow-providers-telegram/index.rst` file is not present in the `current_doc_urls`.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739149177


   pre-commit only runs checks that apply to the currently changed files, but on CI we always check all files. When I am on my computer it will try to fix the rules to better handle your case.
   
   Can you add this guide to provider.yaml? See example: https://github.com/apache/airflow/blob/master/airflow/providers/databricks/provider.yaml


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-726265464


   @RosterIn I've added telegram there.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] RosterIn commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
RosterIn commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-723565978


   @rootcss  it's a good idea to allow user to select telegram conn_type when defining a connection in the UI. Also, adding example dag is good addition.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739237710


   The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest master at your convenience, or amend the last commit of the PR, and push it with --force-with-lease.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] potiuk edited a comment on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
potiuk edited a comment on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-721003162


   I can assign you if you would be happy with it :). Just comment on the #10386  issue if you would like to help 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739134797


    /docs/apache-airlfow-providers/operators.rst
   
   Index is generated automatically based on provider.yaml
   http://apache-airflow-docs.s3-website.eu-central-1.amazonaws.com/docs/apache-airflow-providers/operators-and-hooks-ref/index.html


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-738948015


   @rootcss  Do you need any help?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-734276453


   [The Workflow run](https://github.com/apache/airflow/actions/runs/385229355) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-735051502


   Failed with same error 🤔 
   <img width="1377" alt="Screenshot 2020-11-28 at 1 02 02 PM" src="https://user-images.githubusercontent.com/4328561/100496416-ebb14800-3179-11eb-80b5-6a76d913da1d.png">
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733862559


   🤞 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil edited a comment on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil edited a comment on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733061006


   Mostly, needs adding of Telegram Provider here: https://github.com/apache/airflow/blob/master/tests/core/test_providers_manager.py#L22-L83
   
   and static check failure:
   
   ```
   Validate providers.yaml files.....................................................................................................Failed
   - hook id: provider-yamls
   - exit code: 1
   
   Checking integration duplicates
   Checking completeness of list of {sensors, hooks, operators}
   Checking for duplicates in list of {sensors, hooks, operators}
   Checking completeness of list of transfers
   Checking for duplicates in list of transfers
   Checking doc files
   Items in the first set but not the second:
   '/docs/howto/operator/telegram.rst'
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739007848


   > Thanks for asking @mik-laj no issues, I couldn't get time to rebase and push. Just did, let's see how it goes. It works locally.
   
   Thanks for your patience while dealing with the CI issues


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739198195


   @mik-laj everything passes, except `CI Build / Postgres9.6,Py3.6: Always Core Other API CLI Providers WWW Integration Heisentests (pull_request) Failing after 45m` I don't see a clear reason or message for the failure.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-732162105


   Looks like https://github.com/apache/airflow/pull/12527 was merged, can you rebase on Master please @rootcss 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-724041185


   [The Workflow run](https://github.com/apache/airflow/actions/runs/354040616) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-734288752


   [The Workflow run](https://github.com/apache/airflow/actions/runs/385270658) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-731581208


   @kaxil one test is failing with this error:
   > E       AssertionError: Lists differ: ['apa[2132 chars]ders-vertica', 'apache-airflow-providers-yande[34 chars]esk'] != ['apa[2132 chars]ders-telegram', 'apache-airflow-providers-vert[71 chars]esk']
   > E       
   > E       First differing element 57:
   > E       'apache-airflow-providers-vertica'
   > E       'apache-airflow-providers-telegram'
   > E       
   > E       Second list contains 1 additional elements.
   > E       First extra element 60:
   > E       'apache-airflow-providers-zendesk'
   > 
   Am I missing a change in one of the files? Since I see the assertion message is missing `..providers-telegram`


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-734815880


   Pushed https://github.com/apache/airflow/pull/11850/commits/4c8dc1e329f766780e2eb374274b844fd09f6896 -- that should hopefully fix the failing test


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] michalslowikowski00 commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
michalslowikowski00 commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r530163758



##########
File path: airflow/providers/telegram/hooks/telegram.py
##########
@@ -0,0 +1,154 @@
+#
+# 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.
+"""Hook for Telegram"""
+from typing import Optional
+
+import telegram
+import tenacity
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base_hook import BaseHook
+
+
+class TelegramHook(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.call(None', {"text": "message", "chat_id": "-1xxx"})
+        # or telegram_hook.call(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
+    """
+
+    def __init__(
+        self,
+        telegram_conn_id: Optional[str] = None,
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+    ) -> None:
+        super().__init__()
+        self.token = self.__get_token(token, telegram_conn_id)
+        self.chat_id = self.__get_chat_id(chat_id, telegram_conn_id)
+        self.connection = self.get_conn()
+
+    def get_conn(self) -> telegram.bot.Bot:
+        """
+        Returns the telegram bot client
+
+        :return: telegram bot client
+        :rtype: telegram.bot.Bot
+        """
+        return telegram.bot.Bot(token=self.token)
+
+    def __get_token(self, token: Optional[str], telegram_conn_id: str) -> 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
+        """
+        if token is not None:
+            return token
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+
+            if not conn.password:
+                raise AirflowException("Missing token(password) in Telegram connection")
+
+            return conn.password
+
+        raise AirflowException("Cannot get token: No valid Telegram connection supplied.")
+
+    def __get_chat_id(self, chat_id: Optional[str], telegram_conn_id: str) -> Optional[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
+        """
+        if chat_id is not None:
+            return chat_id
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+            return conn.host
+
+        return None
+
+    @tenacity.retry(
+        retry=tenacity.retry_if_exception_type(telegram.error.TelegramError),
+        stop=tenacity.stop_after_attempt(5),
+        wait=tenacity.wait_fixed(1),
+    )
+    def call(self, api_params: dict) -> None:

Review comment:
       IMHO this function name is misleading. From this name I would expect, hm... A call. :)
   What do you think about `send_message` like the Telegram API method?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739241985


   Thanks @kaxil @mik-laj and others. This PR was a marathon :) 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739148205


   @mik-laj  Static checks :sob: 
   ```
   Items in the first set but not the second:
   '/docs/apache-airflow-providers-telegram/operators.rst'
   ```
   also, not sure how it passes locally.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r531964329



##########
File path: docs/howto/operator/telegram.rst
##########
@@ -0,0 +1,58 @@
+ .. 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/operator:TelegramOperator:

Review comment:
       Done.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739135668


   @mik-laj Where do we place this file? It is `howto` for the telegram operator.
   https://github.com/apache/airflow/blob/1e7568de54a56d43ec3d47d248932fbd11ffd10b/docs/howto/operator/telegram.rst
   
   Currently there is no howto/ directory under /docs/apache-airflow-providers/


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r531007867



##########
File path: airflow/providers/telegram/hooks/telegram.py
##########
@@ -0,0 +1,154 @@
+#
+# 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.
+"""Hook for Telegram"""
+from typing import Optional
+
+import telegram
+import tenacity
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base_hook import BaseHook
+
+
+class TelegramHook(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.call(None', {"text": "message", "chat_id": "-1xxx"})
+        # or telegram_hook.call(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
+    """
+
+    def __init__(
+        self,
+        telegram_conn_id: Optional[str] = None,
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+    ) -> None:
+        super().__init__()
+        self.token = self.__get_token(token, telegram_conn_id)
+        self.chat_id = self.__get_chat_id(chat_id, telegram_conn_id)
+        self.connection = self.get_conn()
+
+    def get_conn(self) -> telegram.bot.Bot:
+        """
+        Returns the telegram bot client
+
+        :return: telegram bot client
+        :rtype: telegram.bot.Bot
+        """
+        return telegram.bot.Bot(token=self.token)
+
+    def __get_token(self, token: Optional[str], telegram_conn_id: str) -> 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
+        """
+        if token is not None:
+            return token
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+
+            if not conn.password:
+                raise AirflowException("Missing token(password) in Telegram connection")
+
+            return conn.password
+
+        raise AirflowException("Cannot get token: No valid Telegram connection supplied.")
+
+    def __get_chat_id(self, chat_id: Optional[str], telegram_conn_id: str) -> Optional[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
+        """
+        if chat_id is not None:
+            return chat_id
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+            return conn.host
+
+        return None
+
+    @tenacity.retry(
+        retry=tenacity.retry_if_exception_type(telegram.error.TelegramError),
+        stop=tenacity.stop_after_attempt(5),
+        wait=tenacity.wait_fixed(1),
+    )
+    def call(self, api_params: dict) -> None:
+        """
+        Sends the message to a telegram channel or chat.
+
+        :param method: not used
+        :type method: str
+        :param api_params: params for telegram_instance.send_message. It can also be used to override chat_id
+        :type api_params: dict
+        """
+        kwargs = {
+            "chat_id": self.chat_id,
+            "parse_mode": telegram.parsemode.ParseMode.HTML,
+            "disable_web_page_preview": True,
+        }
+        kwargs.update(api_params)
+
+        if 'text' not in kwargs or kwargs['text'] is None:
+            raise AirflowException("'text' must be provided for telegram message")
+
+        if kwargs['chat_id'] is None:
+            raise AirflowException("'chat_id' must be provided for telegram message")
+
+        response = self.connection.send_message(**kwargs)
+        self.log.debug(response)

Review comment:
       👍 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733943414


   [The Workflow run](https://github.com/apache/airflow/actions/runs/383944491) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-726327535


   [The Workflow run](https://github.com/apache/airflow/actions/runs/360360317) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-720536501






----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733138203


   Thank you @kaxil 
   I've added the entry in https://github.com/apache/airflow/blob/master/tests/core/test_providers_manager.py#L22-L83
   
   static checks are passing locally with this: `./breeze static-check all`


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-734898709


   Thanks @mik-laj Will do that. I was using `./breeze build-docs` locally to test it, which was working.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-724175490


   [The Workflow run](https://github.com/apache/airflow/actions/runs/354467420) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-731606107


   Thanks @mik-laj 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733061006


   Mostly, needs adding of Telegram Operator here: https://github.com/apache/airflow/blob/master/tests/core/test_providers_manager.py#L22-L83


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739137357


   Thanks @mik-laj That helped.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733011674


   [The Workflow run](https://github.com/apache/airflow/actions/runs/381232317) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r531687943



##########
File path: docs/apache-airflow-providers-telegram/index.rst
##########
@@ -0,0 +1,35 @@
+
+ .. 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.
+
+``apache-airflow-providers-telegram``
+==================================
+
+Content
+-------
+
+.. toctree::
+    :maxdepth: 1
+    :caption: References
+
+    Python API <_api/airflow/providers/telegram/index>
+
+.. toctree::
+    :maxdepth: 1
+    :caption: Resources
+
+    Example DAGs <example-dags>

Review comment:
       ```suggestion
       Example DAGs <https://github.com/apache/airflow/tree/master/airflow/providers/telegram/example_dags>
   ```
   We don't need a separate page if we only have one `example_dags` directory in this provider.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil edited a comment on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil edited a comment on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733061006


   Mostly, needs adding of Telegram Provider here: https://github.com/apache/airflow/blob/master/tests/core/test_providers_manager.py#L22-L83


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r531687078



##########
File path: docs/howto/operator/telegram.rst
##########
@@ -0,0 +1,58 @@
+ .. 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/operator:TelegramOperator:

Review comment:
       Can you move this file to `/docs/apache-airlfow-providers/operators.rst`?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r530804701



##########
File path: airflow/providers/telegram/provider.yaml
##########
@@ -0,0 +1,40 @@
+# 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.
+
+---
+package-name: apache-airflow-providers-telegram
+name: Telegram
+description: |
+    `Telegram <https://telegram.org/>`__
+
+versions:
+  - 0.0.1
+
+integrations:
+  - integration-name: Telegram
+    external-doc-url: https://telegram.org/

Review comment:
       See: https://github.com/apache/airflow/blob/eacf40d85eb014e5a0350d46a3fe68625d8d5f15/airflow/provider.yaml.schema.json#L35-L41




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-735141890


   @rootcss Oh yes. We don't allow operators guides in `apache-airflow-provider-*` directories yet. Here is PR: https://github.com/apache/airflow/pull/12681/files
   Can you wait while it gets merged? Alternatively, you can edit the `pre_commit_check_provider_yaml_files.py` file so that this check finds your guide.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r530521245



##########
File path: airflow/providers/telegram/hooks/telegram.py
##########
@@ -0,0 +1,154 @@
+#
+# 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.
+"""Hook for Telegram"""
+from typing import Optional
+
+import telegram
+import tenacity
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base_hook import BaseHook
+
+
+class TelegramHook(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.call(None', {"text": "message", "chat_id": "-1xxx"})
+        # or telegram_hook.call(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
+    """
+
+    def __init__(
+        self,
+        telegram_conn_id: Optional[str] = None,
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+    ) -> None:
+        super().__init__()
+        self.token = self.__get_token(token, telegram_conn_id)
+        self.chat_id = self.__get_chat_id(chat_id, telegram_conn_id)
+        self.connection = self.get_conn()
+
+    def get_conn(self) -> telegram.bot.Bot:
+        """
+        Returns the telegram bot client
+
+        :return: telegram bot client
+        :rtype: telegram.bot.Bot
+        """
+        return telegram.bot.Bot(token=self.token)
+
+    def __get_token(self, token: Optional[str], telegram_conn_id: str) -> 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
+        """
+        if token is not None:
+            return token
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+
+            if not conn.password:
+                raise AirflowException("Missing token(password) in Telegram connection")
+
+            return conn.password
+
+        raise AirflowException("Cannot get token: No valid Telegram connection supplied.")
+
+    def __get_chat_id(self, chat_id: Optional[str], telegram_conn_id: str) -> Optional[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
+        """
+        if chat_id is not None:
+            return chat_id
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+            return conn.host
+
+        return None
+
+    @tenacity.retry(
+        retry=tenacity.retry_if_exception_type(telegram.error.TelegramError),
+        stop=tenacity.stop_after_attempt(5),
+        wait=tenacity.wait_fixed(1),
+    )
+    def call(self, api_params: dict) -> None:
+        """
+        Sends the message to a telegram channel or chat.
+
+        :param method: not used
+        :type method: str
+        :param api_params: params for telegram_instance.send_message. It can also be used to override chat_id
+        :type api_params: dict
+        """
+        kwargs = {
+            "chat_id": self.chat_id,
+            "parse_mode": telegram.parsemode.ParseMode.HTML,
+            "disable_web_page_preview": True,
+        }
+        kwargs.update(api_params)
+
+        if 'text' not in kwargs or kwargs['text'] is None:
+            raise AirflowException("'text' must be provided for telegram message")
+
+        if kwargs['chat_id'] is None:
+            raise AirflowException("'chat_id' must be provided for telegram message")
+
+        response = self.connection.send_message(**kwargs)
+        self.log.debug(response)

Review comment:
       I followed the pattern from other operators where we don't put anything in INFO mode unless very crucial. What do you suggest?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss edited a comment on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss edited a comment on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733138203


   Thank you @kaxil 
   I've added the entry in https://github.com/apache/airflow/blob/master/tests/core/test_providers_manager.py#L22-L83
   
   static checks are passing locally with this: `./breeze static-check all`
   
   mypy and flake8 defined checks are passing too:
   `./breeze static-check flake8 -- --all-files`
   `./breeze static-check mypy -- --all-files`


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739134322


   @mik-laj what's the new location for howto docs for providers?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] potiuk commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
potiuk commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-721001072






----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-725839722


   @kaxil Please review. I'll fix the two conflicting files, looks like it gets changed in master frequently enough.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-731401063


   [The Workflow run](https://github.com/apache/airflow/actions/runs/375131593) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-735051137


   [The Workflow run](https://github.com/apache/airflow/actions/runs/388347477) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-737019905


   @mik-laj https://github.com/apache/airflow/pull/12681/files is merged now. I'll try this and see if it works.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r531964587



##########
File path: docs/apache-airflow-providers-telegram/index.rst
##########
@@ -0,0 +1,35 @@
+
+ .. 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.
+
+``apache-airflow-providers-telegram``
+==================================
+
+Content
+-------
+
+.. toctree::
+    :maxdepth: 1
+    :caption: References
+
+    Python API <_api/airflow/providers/telegram/index>
+
+.. toctree::
+    :maxdepth: 1
+    :caption: Resources
+
+    Example DAGs <example-dags>

Review comment:
       Got it. Done.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-734888026


   > Thanks @kaxil. Building docs still failed with:
   > 
   > ```
   > Warning, treated as error:
   > failed to reach any of the inventories with the following issues:
   > intersphinx inventory '/opt/airflow/docs/_build/docs/apache-airflow-providers-telegram/latest/objects.inv' not fetchable due to <class 'FileNotFoundError'>: [Errno 2] No such file or directory: '/opt/airflow/docs/_build/docs/apache-airflow-providers-telegram/latest/objects.inv'
   > intersphinx inventory 'http://apache-airflow-docs.s3-website.eu-central-1.amazonaws.com/docs/apache-airflow-providers-telegram/latest/objects.inv' not fetchable due to <class 'requests.exceptions.HTTPError'>: 404 Client Error: Not Found for url: http://apache-airflow-docs.s3-website.eu-central-1.amazonaws.com/docs/apache-airflow-providers-telegram/latest/objects.inv
   > ```
   > 
   > Have I missed any config for this?
   
   cc @mik-laj 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-734861771


   Thanks @kaxil. Building docs still failed with:
   ```
   Warning, treated as error:
   failed to reach any of the inventories with the following issues:
   intersphinx inventory '/opt/airflow/docs/_build/docs/apache-airflow-providers-telegram/latest/objects.inv' not fetchable due to <class 'FileNotFoundError'>: [Errno 2] No such file or directory: '/opt/airflow/docs/_build/docs/apache-airflow-providers-telegram/latest/objects.inv'
   intersphinx inventory 'http://apache-airflow-docs.s3-website.eu-central-1.amazonaws.com/docs/apache-airflow-providers-telegram/latest/objects.inv' not fetchable due to <class 'requests.exceptions.HTTPError'>: 404 Client Error: Not Found for url: http://apache-airflow-docs.s3-website.eu-central-1.amazonaws.com/docs/apache-airflow-providers-telegram/latest/objects.inv
   ```
   Have I missed any config for this?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] michalslowikowski00 commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
michalslowikowski00 commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r530169606



##########
File path: airflow/providers/telegram/operators/telegram.py
##########
@@ -0,0 +1,80 @@
+#
+# 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.
+"""Operator for Telegram"""
+from typing import Optional
+
+from airflow.exceptions import AirflowException
+from airflow.models import BaseOperator
+from airflow.providers.telegram.hooks.telegram import TelegramHook
+from airflow.utils.decorators import apply_defaults
+
+
+class TelegramOperator(BaseOperator):
+    """
+    This operator allows you to post messages to Telegram using Telegram Bot API.
+    Takes both Telegram Bot API token directly or connection that has Telegram token in password field.
+    If both supplied, token parameter will be given precedence.
+
+    :param telegram_conn_id: Telegram connection ID which its password is Telegram API token
+    :type telegram_conn_id: str
+    :param token: Telegram API Token
+    :type token: str
+    :param chat_id: Telegram chat ID for a chat/channel/group
+    :type chat_id: str
+    :param text: Message to be sent on telegram
+    :type text: str
+    :param telegram_kwargs: Extra args to be passed to telegram client
+    :type telegram_kwargs: dict
+    """
+
+    template_fields = ('text', 'chat_id')
+    ui_color = '#FFBA40'
+
+    @apply_defaults
+    def __init__(
+        self,

Review comment:
       ```suggestion
           self, 
           *,
   ```
   All args have to be a keywords.

##########
File path: airflow/providers/telegram/operators/telegram.py
##########
@@ -0,0 +1,80 @@
+#
+# 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.
+"""Operator for Telegram"""
+from typing import Optional
+
+from airflow.exceptions import AirflowException
+from airflow.models import BaseOperator
+from airflow.providers.telegram.hooks.telegram import TelegramHook
+from airflow.utils.decorators import apply_defaults
+
+
+class TelegramOperator(BaseOperator):
+    """
+    This operator allows you to post messages to Telegram using Telegram Bot API.
+    Takes both Telegram Bot API token directly or connection that has Telegram token in password field.
+    If both supplied, token parameter will be given precedence.
+
+    :param telegram_conn_id: Telegram connection ID which its password is Telegram API token
+    :type telegram_conn_id: str
+    :param token: Telegram API Token
+    :type token: str
+    :param chat_id: Telegram chat ID for a chat/channel/group
+    :type chat_id: str
+    :param text: Message to be sent on telegram
+    :type text: str
+    :param telegram_kwargs: Extra args to be passed to telegram client
+    :type telegram_kwargs: dict
+    """
+
+    template_fields = ('text', 'chat_id')
+    ui_color = '#FFBA40'
+
+    @apply_defaults
+    def __init__(
+        self,
+        telegram_conn_id: str = "telegram_default",
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+        text: str = "No message has been set.",
+        telegram_kwargs: Optional[dict] = None,
+        *args,
+        **kwargs,

Review comment:
       ```suggestion
           **kwargs,
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r527919795



##########
File path: airflow/providers/telegram/hooks/telegram.py
##########
@@ -0,0 +1,153 @@
+#
+# 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.
+"""Hook for Telegram"""
+from typing import Optional
+
+import telegram
+import tenacity
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base_hook import BaseHook
+
+
+class TelegramHook(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.call(None', {"text": "message", "chat_id": "-1xxx"})
+        # or telegram_hook.call(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
+    """
+
+    def __init__(
+        self,
+        telegram_conn_id: Optional[str] = None,
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+    ) -> None:
+        super().__init__()
+        self.token = self.__get_token(token, telegram_conn_id)
+        self.chat_id = self.__get_chat_id(chat_id, telegram_conn_id)
+        self.connection = self.get_conn()
+
+    def get_conn(self) -> telegram.bot.Bot:
+        """
+        Returns the telegram bot client
+
+        :return: telegram bot client
+        :rtype: telegram.bot.Bot
+        """
+        return telegram.bot.Bot(token=self.token)
+
+    def __get_token(self, token: Optional[str], telegram_conn_id: str) -> 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
+        """
+        if token is not None:
+            return token
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+
+            if not conn.password:
+                raise AirflowException("Missing token(password) in Telegram connection")
+
+            return conn.password
+
+        raise AirflowException("Cannot get token: No valid Telegram connection supplied.")
+
+    def __get_chat_id(self, chat_id: Optional[str], telegram_conn_id: str) -> Optional[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
+        """
+        if chat_id is not None:
+            return chat_id
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+            return conn.host
+
+        return None
+
+    @tenacity.retry(
+        retry=tenacity.retry_if_exception_type(telegram.error.TelegramError),
+        stop=tenacity.stop_after_attempt(5),
+        wait=tenacity.wait_fixed(1),
+    )
+    def call(self, api_params: dict) -> None:
+        """
+        Sends the message to a telegram channel or chat.
+
+        :param method: not used
+        :type method: str
+        :param api_params: params for telegram_instance.send_message. It can also be used to override chat_id
+        :type api_params: dict
+        """
+        kwargs = {
+            "chat_id": self.chat_id,
+            "parse_mode": telegram.parsemode.ParseMode.HTML,
+            "disable_web_page_preview": True,
+        }
+        kwargs.update(api_params)
+
+        if 'text' not in kwargs or kwargs['text'] is None:
+            raise AirflowException("'text' must be provided for telegram message")
+
+        if kwargs['chat_id'] is None:
+            raise AirflowException("'chat_id' must be provided for telegram message")
+
+        self.log.debug(self.connection.send_message(**kwargs))

Review comment:
       I've made this change @kaxil 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-726327226


   [The Workflow run](https://github.com/apache/airflow/actions/runs/360356200) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-734893415


   There is a bug summarizing the bugs, but there are a few bugs in the documentation that you should fix. I recommend building documentation locally. This is now very fast as you can only build one package.
   ./build_docs.py --package apache-airflow-proviiders-telegram
   ```
   2020-11-27T13:38:39.2917956Z sphinx.errors.SphinxWarning: /opt/airflow/docs/apache-airflow-providers-telegram/example-dags.rst:2:Block quote ends without a blank line; unexpected unindent.
   ```
   ```
   2020-11-27T13:38:45.5528798Z /opt/airflow/docs/apache-airflow-providers-telegram/example-dags.rst:2: WARNING: Block quote ends without a blank line; unexpected unindent.
   2020-11-27T13:38:45.5533986Z /opt/airflow/docs/apache-airflow-providers-telegram/example-dags.rst:12: WARNING: Block quote ends without a blank line; unexpected unindent.
   2020-11-27T13:38:45.5538704Z /opt/airflow/docs/apache-airflow-providers-telegram/index.rst:20: WARNING: Title underline too short.
   ```
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739136360


   /docs/apache-airlfow-providers-telegram/operators.rst
   Similar to Databricks https://github.com/apache/airflow/blob/master/docs/apache-airflow-providers-databricks/operators.rst


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] michalslowikowski00 commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
michalslowikowski00 commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r530170088



##########
File path: airflow/providers/telegram/operators/telegram.py
##########
@@ -0,0 +1,80 @@
+#
+# 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.
+"""Operator for Telegram"""
+from typing import Optional
+
+from airflow.exceptions import AirflowException
+from airflow.models import BaseOperator
+from airflow.providers.telegram.hooks.telegram import TelegramHook
+from airflow.utils.decorators import apply_defaults
+
+
+class TelegramOperator(BaseOperator):
+    """
+    This operator allows you to post messages to Telegram using Telegram Bot API.
+    Takes both Telegram Bot API token directly or connection that has Telegram token in password field.
+    If both supplied, token parameter will be given precedence.
+
+    :param telegram_conn_id: Telegram connection ID which its password is Telegram API token
+    :type telegram_conn_id: str
+    :param token: Telegram API Token
+    :type token: str
+    :param chat_id: Telegram chat ID for a chat/channel/group
+    :type chat_id: str
+    :param text: Message to be sent on telegram
+    :type text: str
+    :param telegram_kwargs: Extra args to be passed to telegram client
+    :type telegram_kwargs: dict
+    """
+
+    template_fields = ('text', 'chat_id')
+    ui_color = '#FFBA40'
+
+    @apply_defaults
+    def __init__(
+        self,
+        telegram_conn_id: str = "telegram_default",
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+        text: str = "No message has been set.",
+        telegram_kwargs: Optional[dict] = None,
+        *args,
+        **kwargs,
+    ):
+        self.chat_id = chat_id
+        self.token = token
+        self.telegram_kwargs = telegram_kwargs or {}
+
+        if text is not None:
+            self.telegram_kwargs['text'] = text
+
+        if telegram_conn_id is None:
+            raise AirflowException("No valid Telegram connection id supplied.")
+
+        self.telegram_conn_id = telegram_conn_id
+
+        super().__init__(*args, **kwargs)

Review comment:
       ```suggestion
           super().__init__(**kwargs)
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-735050101


   Things are passing locally. Let's hope this is it 🤞 
   @mik-laj yes, build_docs.py is much much faster :)


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733011270


   [The Workflow run](https://github.com/apache/airflow/actions/runs/381231644) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739237756


   > @mik-laj everything passes, except `CI Build / Postgres9.6,Py3.6: Always Core Other API CLI Providers WWW Integration Heisentests (pull_request) Failing after 45m` I don't see a clear reason or message for the failure.
   
   That's fine -- it is OOM error -- we can ignore it. Good work @rootcss 👏 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r522415435



##########
File path: airflow/providers/telegram/hooks/telegram.py
##########
@@ -0,0 +1,153 @@
+#
+# 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.
+"""Hook for Telegram"""
+from typing import Optional
+
+import telegram
+import tenacity
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base_hook import BaseHook
+
+
+class TelegramHook(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.call(None', {"text": "message", "chat_id": "-1xxx"})
+        # or telegram_hook.call(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
+    """
+
+    def __init__(
+        self,
+        telegram_conn_id: Optional[str] = None,
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+    ) -> None:
+        super().__init__()
+        self.token = self.__get_token(token, telegram_conn_id)
+        self.chat_id = self.__get_chat_id(chat_id, telegram_conn_id)
+        self.connection = self.get_conn()
+
+    def get_conn(self) -> telegram.bot.Bot:
+        """
+        Returns the telegram bot client
+
+        :return: telegram bot client
+        :rtype: telegram.bot.Bot
+        """
+        return telegram.bot.Bot(token=self.token)
+
+    def __get_token(self, token: Optional[str], telegram_conn_id: str) -> 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
+        """
+        if token is not None:
+            return token
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+
+            if not conn.password:
+                raise AirflowException("Missing token(password) in Telegram connection")
+
+            return conn.password
+
+        raise AirflowException("Cannot get token: No valid Telegram connection supplied.")
+
+    def __get_chat_id(self, chat_id: Optional[str], telegram_conn_id: str) -> Optional[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
+        """
+        if chat_id is not None:
+            return chat_id
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+            return conn.host
+
+        return None
+
+    @tenacity.retry(
+        retry=tenacity.retry_if_exception_type(telegram.error.TelegramError),
+        stop=tenacity.stop_after_attempt(5),
+        wait=tenacity.wait_fixed(1),
+    )
+    def call(self, api_params: dict) -> None:
+        """
+        Sends the message to a telegram channel or chat.
+
+        :param method: not used
+        :type method: str
+        :param api_params: params for telegram_instance.send_message. It can also be used to override chat_id
+        :type api_params: dict
+        """
+        kwargs = {
+            "chat_id": self.chat_id,
+            "parse_mode": telegram.parsemode.ParseMode.HTML,
+            "disable_web_page_preview": True,
+        }
+        kwargs.update(api_params)
+
+        if 'text' not in kwargs or kwargs['text'] is None:
+            raise AirflowException("'text' must be provided for telegram message")
+
+        if kwargs['chat_id'] is None:
+            raise AirflowException("'chat_id' must be provided for telegram message")
+
+        self.log.debug(self.connection.send_message(**kwargs))

Review comment:
       ??? Is the wrapping in the DEBUG call intentional?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] mik-laj commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
mik-laj commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r530804898



##########
File path: docs/apache-airflow-providers-telegram/index.rst
##########
@@ -0,0 +1,29 @@
+
+ .. 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.
+
+``apache-airflow-providers-telegram``
+==================================
+
+Content
+-------
+
+.. toctree::
+    :maxdepth: 1
+    :caption: References
+
+    Python API <_api/airflow/providers/telegram/index>

Review comment:
       Can you add link to example DAGs directory?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-731370059


   [The Workflow run](https://github.com/apache/airflow/actions/runs/375035679) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-727814486


   [The Workflow run](https://github.com/apache/airflow/actions/runs/365596953) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-716690116


   [The Workflow run](https://github.com/apache/airflow/actions/runs/329422612) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks$,^Build docs$,^Spell check docs$,^Backport packages$,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r523720622



##########
File path: airflow/providers/telegram/hooks/telegram.py
##########
@@ -0,0 +1,153 @@
+#
+# 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.
+"""Hook for Telegram"""
+from typing import Optional
+
+import telegram
+import tenacity
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base_hook import BaseHook
+
+
+class TelegramHook(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.call(None', {"text": "message", "chat_id": "-1xxx"})
+        # or telegram_hook.call(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
+    """
+
+    def __init__(
+        self,
+        telegram_conn_id: Optional[str] = None,
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+    ) -> None:
+        super().__init__()
+        self.token = self.__get_token(token, telegram_conn_id)
+        self.chat_id = self.__get_chat_id(chat_id, telegram_conn_id)
+        self.connection = self.get_conn()
+
+    def get_conn(self) -> telegram.bot.Bot:
+        """
+        Returns the telegram bot client
+
+        :return: telegram bot client
+        :rtype: telegram.bot.Bot
+        """
+        return telegram.bot.Bot(token=self.token)
+
+    def __get_token(self, token: Optional[str], telegram_conn_id: str) -> 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
+        """
+        if token is not None:
+            return token
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+
+            if not conn.password:
+                raise AirflowException("Missing token(password) in Telegram connection")
+
+            return conn.password
+
+        raise AirflowException("Cannot get token: No valid Telegram connection supplied.")
+
+    def __get_chat_id(self, chat_id: Optional[str], telegram_conn_id: str) -> Optional[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
+        """
+        if chat_id is not None:
+            return chat_id
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+            return conn.host
+
+        return None
+
+    @tenacity.retry(
+        retry=tenacity.retry_if_exception_type(telegram.error.TelegramError),
+        stop=tenacity.stop_after_attempt(5),
+        wait=tenacity.wait_fixed(1),
+    )
+    def call(self, api_params: dict) -> None:
+        """
+        Sends the message to a telegram channel or chat.
+
+        :param method: not used
+        :type method: str
+        :param api_params: params for telegram_instance.send_message. It can also be used to override chat_id
+        :type api_params: dict
+        """
+        kwargs = {
+            "chat_id": self.chat_id,
+            "parse_mode": telegram.parsemode.ParseMode.HTML,
+            "disable_web_page_preview": True,
+        }
+        kwargs.update(api_params)
+
+        if 'text' not in kwargs or kwargs['text'] is None:
+            raise AirflowException("'text' must be provided for telegram message")
+
+        if kwargs['chat_id'] is None:
+            raise AirflowException("'chat_id' must be provided for telegram message")
+
+        self.log.debug(self.connection.send_message(**kwargs))

Review comment:
       Yes, send_message() sends the response, I thought in debug mode it could be useful. Let me know your thoughts. It'd be better to keep it consistent with other hooks.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r531008168



##########
File path: docs/apache-airflow-providers-telegram/index.rst
##########
@@ -0,0 +1,29 @@
+
+ .. 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.
+
+``apache-airflow-providers-telegram``
+==================================
+
+Content
+-------
+
+.. toctree::
+    :maxdepth: 1
+    :caption: References
+
+    Python API <_api/airflow/providers/telegram/index>

Review comment:
       I've added it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-739142470


   [The Workflow run](https://github.com/apache/airflow/actions/runs/402267294) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733221781


   [The Workflow run](https://github.com/apache/airflow/actions/runs/381856097) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-728879626


   Static checks are failing


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-731480205


   tests are failing


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-727797419


   @kaxil @RosterIn I've added the docs and example dag. Please have a look.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] michalslowikowski00 commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
michalslowikowski00 commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-733540924


   @rootcss kudos for the tests. 💪 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] michalslowikowski00 commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
michalslowikowski00 commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r530166095



##########
File path: airflow/providers/telegram/hooks/telegram.py
##########
@@ -0,0 +1,154 @@
+#
+# 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.
+"""Hook for Telegram"""
+from typing import Optional
+
+import telegram
+import tenacity
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base_hook import BaseHook
+
+
+class TelegramHook(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.call(None', {"text": "message", "chat_id": "-1xxx"})
+        # or telegram_hook.call(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
+    """
+
+    def __init__(
+        self,
+        telegram_conn_id: Optional[str] = None,
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+    ) -> None:
+        super().__init__()
+        self.token = self.__get_token(token, telegram_conn_id)
+        self.chat_id = self.__get_chat_id(chat_id, telegram_conn_id)
+        self.connection = self.get_conn()
+
+    def get_conn(self) -> telegram.bot.Bot:
+        """
+        Returns the telegram bot client
+
+        :return: telegram bot client
+        :rtype: telegram.bot.Bot
+        """
+        return telegram.bot.Bot(token=self.token)
+
+    def __get_token(self, token: Optional[str], telegram_conn_id: str) -> 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
+        """
+        if token is not None:
+            return token
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+
+            if not conn.password:
+                raise AirflowException("Missing token(password) in Telegram connection")
+
+            return conn.password
+
+        raise AirflowException("Cannot get token: No valid Telegram connection supplied.")
+
+    def __get_chat_id(self, chat_id: Optional[str], telegram_conn_id: str) -> Optional[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
+        """
+        if chat_id is not None:
+            return chat_id
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+            return conn.host
+
+        return None
+
+    @tenacity.retry(
+        retry=tenacity.retry_if_exception_type(telegram.error.TelegramError),
+        stop=tenacity.stop_after_attempt(5),
+        wait=tenacity.wait_fixed(1),
+    )
+    def call(self, api_params: dict) -> None:
+        """
+        Sends the message to a telegram channel or chat.
+
+        :param method: not used
+        :type method: str
+        :param api_params: params for telegram_instance.send_message. It can also be used to override chat_id
+        :type api_params: dict
+        """
+        kwargs = {
+            "chat_id": self.chat_id,
+            "parse_mode": telegram.parsemode.ParseMode.HTML,
+            "disable_web_page_preview": True,
+        }
+        kwargs.update(api_params)
+
+        if 'text' not in kwargs or kwargs['text'] is None:
+            raise AirflowException("'text' must be provided for telegram message")
+
+        if kwargs['chat_id'] is None:
+            raise AirflowException("'chat_id' must be provided for telegram message")
+
+        response = self.connection.send_message(**kwargs)
+        self.log.debug(response)

Review comment:
       ```suggestion
           self.log.info(response)
   ```
   Do we need to log full response here?
   Maybe log the message that call to API ends with success or other user friendly message. IMHO we should log here smth like `self.log.info("Message has been sent.")`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] kaxil merged pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
kaxil merged pull request #11850:
URL: https://github.com/apache/airflow/pull/11850


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] github-actions[bot] commented on pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#issuecomment-734856911


   [The Workflow run](https://github.com/apache/airflow/actions/runs/387241222) is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] michalslowikowski00 commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
michalslowikowski00 commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r530840458



##########
File path: airflow/providers/telegram/hooks/telegram.py
##########
@@ -0,0 +1,154 @@
+#
+# 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.
+"""Hook for Telegram"""
+from typing import Optional
+
+import telegram
+import tenacity
+
+from airflow.exceptions import AirflowException
+from airflow.hooks.base_hook import BaseHook
+
+
+class TelegramHook(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.call(None', {"text": "message", "chat_id": "-1xxx"})
+        # or telegram_hook.call(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
+    """
+
+    def __init__(
+        self,
+        telegram_conn_id: Optional[str] = None,
+        token: Optional[str] = None,
+        chat_id: Optional[str] = None,
+    ) -> None:
+        super().__init__()
+        self.token = self.__get_token(token, telegram_conn_id)
+        self.chat_id = self.__get_chat_id(chat_id, telegram_conn_id)
+        self.connection = self.get_conn()
+
+    def get_conn(self) -> telegram.bot.Bot:
+        """
+        Returns the telegram bot client
+
+        :return: telegram bot client
+        :rtype: telegram.bot.Bot
+        """
+        return telegram.bot.Bot(token=self.token)
+
+    def __get_token(self, token: Optional[str], telegram_conn_id: str) -> 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
+        """
+        if token is not None:
+            return token
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+
+            if not conn.password:
+                raise AirflowException("Missing token(password) in Telegram connection")
+
+            return conn.password
+
+        raise AirflowException("Cannot get token: No valid Telegram connection supplied.")
+
+    def __get_chat_id(self, chat_id: Optional[str], telegram_conn_id: str) -> Optional[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
+        """
+        if chat_id is not None:
+            return chat_id
+
+        if telegram_conn_id is not None:
+            conn = self.get_connection(telegram_conn_id)
+            return conn.host
+
+        return None
+
+    @tenacity.retry(
+        retry=tenacity.retry_if_exception_type(telegram.error.TelegramError),
+        stop=tenacity.stop_after_attempt(5),
+        wait=tenacity.wait_fixed(1),
+    )
+    def call(self, api_params: dict) -> None:
+        """
+        Sends the message to a telegram channel or chat.
+
+        :param method: not used
+        :type method: str
+        :param api_params: params for telegram_instance.send_message. It can also be used to override chat_id
+        :type api_params: dict
+        """
+        kwargs = {
+            "chat_id": self.chat_id,
+            "parse_mode": telegram.parsemode.ParseMode.HTML,
+            "disable_web_page_preview": True,
+        }
+        kwargs.update(api_params)
+
+        if 'text' not in kwargs or kwargs['text'] is None:
+            raise AirflowException("'text' must be provided for telegram message")
+
+        if kwargs['chat_id'] is None:
+            raise AirflowException("'chat_id' must be provided for telegram message")
+
+        response = self.connection.send_message(**kwargs)
+        self.log.debug(response)

Review comment:
       I think I have never uses debug in my operators. I have no strong opinion here. I thought about confirmation and explicit info for the user that the operation ended with success. 
   Like I mentioned in previous comment. 
   But when I am looking at debug info today it make sense to log response. The question is if the DebugExecutor logs this  in the console, I supposed it is logged. Anyway, debug is ok for me.  




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [airflow] rootcss commented on a change in pull request #11850: Add Telegram hook and operator

Posted by GitBox <gi...@apache.org>.
rootcss commented on a change in pull request #11850:
URL: https://github.com/apache/airflow/pull/11850#discussion_r531008074



##########
File path: airflow/providers/telegram/provider.yaml
##########
@@ -0,0 +1,40 @@
+# 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.
+
+---
+package-name: apache-airflow-providers-telegram
+name: Telegram
+description: |
+    `Telegram <https://telegram.org/>`__
+
+versions:
+  - 0.0.1
+
+integrations:
+  - integration-name: Telegram
+    external-doc-url: https://telegram.org/

Review comment:
       Got it. Thank you.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org