From fd3adffc39d02d1e6a2ffc03138398c378250533 Mon Sep 17 00:00:00 2001 From: Tal Date: Thu, 19 Sep 2024 18:34:49 +0300 Subject: [PATCH] first commit --- .../MicrosoftTeamsWebhook.py | 137 +++++++++++------- .../MicrosoftTeamsWebhook.yml | 5 + .../MicrosoftTeamsWebhook_description.md | 6 +- 3 files changed, 94 insertions(+), 54 deletions(-) diff --git a/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook.py b/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook.py index 83550f879938..1543a58c6985 100644 --- a/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook.py +++ b/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook.py @@ -8,7 +8,7 @@ class Client(BaseClient): - def __init__(self, base_url: str, proxy: bool, verify: bool): + def __init__(self, base_url: str, proxy: bool, verify: bool, is_workflow: bool = True): """ Client to use in the. Overrides BaseClient. @@ -17,6 +17,7 @@ def __init__(self, base_url: str, proxy: bool, verify: bool): """ self.base_url = base_url + self.is_workflow= is_workflow super().__init__(base_url=base_url, proxy=proxy, verify=verify) def send_teams_message(self, messagecard: dict, adaptive_cards_format: bool = False): @@ -28,7 +29,9 @@ def send_teams_message(self, messagecard: dict, adaptive_cards_format: bool = Fa adaptive_cards_format (bool): Should the adaptive card url format be used? """ - if adaptive_cards_format: + demisto.debug(f"{self.base_url=}") + demisto.debug(f"{messagecard=}") + if adaptive_cards_format or self.is_workflow: res = self._http_request( method='POST', json_data=messagecard, @@ -41,12 +44,13 @@ def send_teams_message(self, messagecard: dict, adaptive_cards_format: bool = Fa method='POST', json_data=messagecard, raise_on_status=True, - resp_type='text' + resp_type='text', ) + demisto.debug(f"{res=}") demisto.info(f'completed post of message. response text: {res}') -def create_teams_message(message: str, title: str, serverurls: str, adaptive_cards_format: bool = False) -> dict: +def create_teams_message(message: str, title: str, serverurls: str, adaptive_cards_format: bool = False, is_workflow: bool = True) -> dict: """ Creates the Teams message using the messageCard format, and returns the card @@ -59,60 +63,85 @@ def create_teams_message(message: str, title: str, serverurls: str, adaptive_car Returns: messagecard (dict): dict the adaptive card to send to Teams. """ - messagecard: dict = {} - if adaptive_cards_format: - messagecard = { - "type": "message", - "attachments": [ - { + if is_workflow: + if adaptive_cards_format: + messagecard = { + "type": "message", + "attachments": [ + { "contentType": "application/vnd.microsoft.card.adaptive", - "contentUrl": None, "content": { "type": "AdaptiveCard", "body": [ - { - "type": "TextBlock", - "size": "Medium", - "weight": "Bolder", - "text": "Cortex XSOAR Notification" - }, - { - "type": "TextBlock", - "text": message, - "wrap": True - } - ], - "actions": [ - { - "type": "Action.OpenUrl", - "title": title, - "url": serverurls - } + { + "type": "TextBlock", + "text": "Message Text from Postman with AdaptiveCard (113)" + } ], "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", - "version": "1.6" + "version": "1.0" } - } - ] - } + } + ] + } + else: + messagecard = { + "text": message + } else: - messagecard = { - "@type": "MessageCard", - "@context": "http://schema.org/extensions", - "themeColor": "0076D7", - "summary": "Cortex XSOAR Notification", - "sections": [{ - "activityTitle": "Cortex XSOAR Notification", - "activitySubtitle": message, - "markdown": True - }], - "potentialAction": [{ - "@type": "OpenUri", - "name": title, - "targets": [{"os": "default", "uri": serverurls}] - }] - } + if adaptive_cards_format: + messagecard = { + "type": "message", + "attachments": [ + { + "contentType": "application/vnd.microsoft.card.adaptive", + "contentUrl": None, + "content": { + "type": "AdaptiveCard", + "body": [ + { + "type": "TextBlock", + "size": "Medium", + "weight": "Bolder", + "text": "Cortex XSOAR Notification" + }, + { + "type": "TextBlock", + "text": message, + "wrap": True + } + ], + "actions": [ + { + "type": "Action.OpenUrl", + "title": title, + "url": serverurls + } + ], + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "version": "1.6" + } + } + ] + } + else: + messagecard = { + "@type": "MessageCard", + "@context": "http://schema.org/extensions", + "themeColor": "0076D7", + "summary": "Cortex XSOAR Notification", + "sections": [{ + "activityTitle": "Cortex XSOAR Notification", + "activitySubtitle": message, + "markdown": True + }], + "potentialAction": [{ + "@type": "OpenUri", + "name": title, + "targets": [{"os": "default", "uri": serverurls}] + }] + } return messagecard @@ -131,7 +160,8 @@ def test_module(client: Client, serverurls: str) -> str: try: message = "Successful test message from Cortex XSOAR" title = "Cortex XSOAR Notification" - test_message = create_teams_message(message, title, serverurls) + test_message = create_teams_message(message, title, serverurls, is_workflow=client.is_workflow) + demisto.debug(f"{test_message=}") client.send_teams_message(test_message) return 'ok' except DemistoException as e: @@ -160,7 +190,7 @@ def send_teams_message_command( which contains the readable_output indicating the message was sent. """ - messagecard = create_teams_message(message, title, serverurls, adaptive_cards_format) + messagecard = create_teams_message(message, title, serverurls, adaptive_cards_format, is_workflow=client.is_workflow) client.send_teams_message(messagecard, adaptive_cards_format) return CommandResults(readable_output='message sent successfully') @@ -178,6 +208,7 @@ def main() -> None: # pragma: no cover verify_certificate = not params.get('insecure', False) proxy = params.get('proxy', False) adaptive_cards_format: bool = argToBoolean(args.get("adaptive_cards_format", False)) + serverurls = demisto.demistoUrls() @@ -185,13 +216,15 @@ def main() -> None: # pragma: no cover serverurls = args.get('alternative_url') else: serverurls = serverurls.get("investigation", serverurls["server"]) + demisto.debug(f"{serverurls=}") command = demisto.command() try: client = Client( base_url=webhook, verify=verify_certificate, - proxy=proxy + proxy=proxy, + is_workflow='workflow' in webhook ) if command == 'test-module': diff --git a/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook.yml b/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook.yml index ac2c710e9cea..7f95d99daa86 100644 --- a/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook.yml +++ b/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook.yml @@ -43,6 +43,11 @@ script: - description: Should the adaptive cards format be used? name: adaptive_cards_format required: false + auto: PREDEFINED + predefined: + - true + - false + defaultValue: false description: Send a message to Microsoft Teams via Incoming Webhook. name: ms-teams-message dockerimage: demisto/python3:3.11.9.105369 diff --git a/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook_description.md b/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook_description.md index 34ac32321e88..4d31924d0fb0 100644 --- a/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook_description.md +++ b/Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook_description.md @@ -1,11 +1,13 @@ Use the Microsoft Teams Webhook integration to send messages and notifications to Teams configured with an incoming webhook. The message will always include a link back to the investigation from which it was sent. ​ ## Create an Incoming Webhook on the Team in Microsoft Teams. -For information, see the [Microsoft Create an Incoming Webhook Documentation](https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook) +For information, see the [Install the Workflows app in Microsoft Teams](https://learn.microsoft.com/en-us/power-automate/teams/install-teams-app), +and the [Browse and add workflows in Microsoft Teams Create a workflow to support Teams Webhook](https://support.microsoft.com/en-us/office/browse-and-add-workflows-in-microsoft-teams-4998095c-8b72-4b0e-984c-f2ad39e6ba9a). +Create a workflow with template `Post to a channel when a webhook request is received`. ​ To create an instance of the Microsoft Teams Webhook in Cortex XSOAR, complete the following: ​ -1. Add an instance of the integration and add the Webhook URL for the Teams channel. +1. Add an instance of the integration and add the Workflow URL for the Teams channel. 2. Test the integration. If successful, you'll see a test message in the channel. ​ ​