Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite microsoft teams webhook #36397

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -17,6 +17,7 @@

"""
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):
Expand All @@ -28,7 +29,9 @@
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,
Expand All @@ -41,12 +44,13 @@
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:

Check failure on line 53 in Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook.py

View workflow job for this annotation

GitHub Actions / pre-commit / pre-commit

Ruff (E501)

Packs/MicrosoftTeams/Integrations/MicrosoftTeamsWebhook/MicrosoftTeamsWebhook.py:53:131: E501 Line too long (139 > 130 characters)
"""
Creates the Teams message using the messageCard format, and returns the card

Expand All @@ -59,60 +63,85 @@
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

Expand All @@ -131,7 +160,8 @@
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:
Expand Down Expand Up @@ -160,7 +190,7 @@
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')

Expand All @@ -178,20 +208,23 @@
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()

if args.get('alternative_url'):
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':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Loading