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 2022/08/18 16:32:36 UTC

[GitHub] [airflow] spatocode commented on a diff in pull request #25769: Add support for MS Teams webhook operator

spatocode commented on code in PR #25769:
URL: https://github.com/apache/airflow/pull/25769#discussion_r949365495


##########
airflow/providers/microsoft/teams/hooks/teams_webhook.py:
##########
@@ -0,0 +1,132 @@
+#
+# 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.
+#
+import json
+from typing import Optional
+
+from airflow.exceptions import AirflowException
+from airflow.providers.http.hooks.http import HttpHook
+
+
+class TeamsWebhookHook(HttpHook):
+    """
+    This hook allows you to post messages to MS Teams using the incoming webhooks.
+    Takes both MS Teams webhook token directly and connection that has MS Teams webhook token.
+    If both supplied, the webhook token will be appended to the host in the connection.
+
+    :param http_conn_id: connection that has MS Teams webhook URL
+    :param webhook_token: MS Teams webhook token
+    :param message: The message you want to send on MS Teams
+    :param facts: The facts to send on MS Teams. Should be a list of
+        dictionaries of two keys representing more details to the message.
+        E.g {"name": "Status", "value": "Not started"}
+    :param subtitle: The subtitle of the message to send
+    :param action_button_name: The name of the action button
+    :param action_button_url: The URL for the action button clicked
+    :param theme_color: Hex code of the card theme, without the #
+    :param icon_url: The icon activityImage URL string to be added to message card.
+    :param proxy: Proxy to use to make the MS Teams webhook call
+    """
+
+    def __init__(
+        self,
+        http_conn_id=None,
+        webhook_token=None,
+        message="",
+        subtitle="",
+        theme_color="",
+        facts=None,
+        action_button_name="",
+        action_button_url="",
+        icon_url=None,
+        proxy=None,
+        *args,
+        **kwargs,
+    ):
+        super().__init__(http_conn_id=http_conn_id, *args, **kwargs)
+        self.webhook_token = self._get_token(webhook_token, http_conn_id)
+        self.message = message
+        self.subtitle = subtitle
+        self.theme_color = theme_color
+        self.facts = facts
+        self.action_button_name = action_button_name
+        self.action_button_url = action_button_url
+        self.icon_url = icon_url
+        self.proxy = proxy
+
+    def _get_token(self, token: str, http_conn_id: Optional[str]) -> str:
+        """
+        Given either a manually set token or a conn_id, return the webhook_token to use.
+
+        :param token: The manually provided token
+        :param http_conn_id: The conn_id provided
+        :return: webhook_token to use
+        :rtype: str
+        """
+        if token:
+            return token
+        elif http_conn_id:
+            conn = self.get_connection(http_conn_id)
+            extra = conn.extra_dejson
+            web_token = extra.get('webhook_token', '')
+            return web_token
+        else:
+            raise AirflowException('Cannot get token: No valid Teams webhook token nor conn_id supplied')
+
+    def _build_teams_message(self) -> str:
+        """
+        Construct Teams connector message. All the relevant parameters are combined to a valid
+        Teams json message.
+
+        :return: JSON formated MS Teams connector message to send
+        :rtype: str
+        """
+        card = {
+            "themeColor": self.theme_color,
+            "summary": self.message,
+            "sections": [{
+                "activityTitle": self.message,
+                "activitySubtitle": self.subtitle,
+                "activityImage": self.icon_url,
+                "facts": self.facts,
+                "potentialAction": [{
+                    "@context": "http://schema.org",
+                    "@type": "OpenUri",
+                    "name": self.action_button_name,
+                    "targets": [{
+                        "os": "default",
+                        "uri": self.action_button_url
+                    }]
+                }]}
+            ]
+        }
+        return json.dumps(card)
+
+    def execute(self) -> None:

Review Comment:
   > I think there is no need for the teams_ prefix in the file names
   
   Just like slack, we might be adding support for integrating Teams API asides webhook in the future. To maintain naming conventions and avoid filenaming issues in the future, I guess it might be best for this module to be left as teams_webhook.py and the upcoming teams API can then be named teams.py, just like slack providers naming conventions.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

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