-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.py
63 lines (45 loc) · 1.76 KB
/
webhook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from enum import Enum
import requests
HEADER = {"Content-Type": "application/json"}
class WebhookMessage:
JSON_TEMPLATE = ""
def __init__(self, url: str, title: str, message: str, additional_data: dict):
self.url = url
self.title = title
self.message = message
self.additional_data = additional_data
def get_message(self):
pass
class WebhookTypes(Enum):
MS_TEAMS = "MSTeams"
# TODO: need to add support
SLACK = "Slack"
class MSTeamsMessage(WebhookMessage):
JSON_TEMPLATE = '''{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "5bc0de",
"summary": "%s",
"sections": [{
"activityTitle": "%s",
"activitySubtitle": "%s",
"activityImage": "https://teamsnodesample.azurewebsites.net/static/img/image5.png",
"facts": %s,
"markdown": true
}]
}
'''
def __init__(self, url: str, title: str, message: str, additional_data: dict = {}):
super().__init__(url, title, message, additional_data)
def get_message(self):
formatted_facts = [{'name': k, 'value': v} for k, v in self.additional_data.items()]
return self.JSON_TEMPLATE % (self.title, self.title, self.message, str(formatted_facts))
webhook_type_map = {WebhookTypes.MS_TEAMS: MSTeamsMessage}
def send_webhook(message: WebhookMessage):
requests.post(message.url, data=message.get_message(), headers=HEADER)
def get_webhook_type(webhook_url: str):
if "office.com" in webhook_url:
return WebhookTypes.MS_TEAMS
if "slack.com" in webhook_url:
return WebhookTypes.SLACK
raise Exception("Cannot guess the type of webhook service from the webhook URL %s", webhook_url)