This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
99 lines (77 loc) · 3.27 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# pylint: disable=unused-argument, import-error, logging-fstring-interpolation, global-statement, fixme
from functools import partial
import signal
import sys
import os
from json import dumps
from http.server import BaseHTTPRequestHandler, HTTPServer
import requests
class WebhookServer:
"""Webhook server"""
def start_server(self):
class GoogleChat:
"""Google Chat"""
def __init__(self, webhook_url: str = None) -> None:
self.webhook_url = webhook_url
def send(self, message: str) -> requests.Response:
"""Send a message to a webhook"""
msg = {"text": message}
hdrs = {"Content-Type": "application/json; charset=UTF-8"}
response = requests.post(
self.webhook_url, data=dumps(msg), headers=hdrs, timeout=5
)
return response
class WebhookRequestHandler(BaseHTTPRequestHandler):
"""Webhook request handler"""
protocol_version = "HTTP/1.0"
def __init__(self, google_chat, *args, **kwargs):
self.google_chat = google_chat
super().__init__(*args, **kwargs)
def _send_response(self, http_code, message=None, headers=None):
"""Sends HTTP response code, message and headers."""
self.send_response(http_code, message)
if headers:
for header in headers:
self.send_header(*header)
self.end_headers()
def do_GET(self): # pylint: disable=invalid-name
"""Handles HTTP GET requests."""
self._send_response(
200, headers=(("Content-type", "%s; charset=utf-8" % "text/html"),)
)
def do_HEAD(self): # pylint: disable=invalid-name
"""Handles HTTP HEAD requests."""
self._send_response(
200, headers=(("Content-type", "%s; charset=utf-8" % "text/html"),)
)
def do_POST(self): # pylint: disable=invalid-name
"""Handles HTTP POST requests."""
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length)
data = post_data.decode("utf-8")
self.google_chat.send(data)
self._send_response(
200, headers=(("Content-type", "%s; charset=utf-8" % "text/html"),)
)
port = int(os.getenv("PORT", "8001"))
webhook_url = os.getenv("WEBHOOK_URL", "undefined")
server_address = ("", port)
google_chat = GoogleChat(webhook_url)
handler = partial(WebhookRequestHandler, google_chat)
self.httpd = HTTPServer(server_address, handler)
print(f"Webhook server is running on {port} ...")
self.httpd.serve_forever()
def exit_now(signum, frame):
sys.exit(0)
if __name__ == "__main__":
try:
forwarder = WebhookServer()
signal.signal(signal.SIGTERM, exit_now)
forwarder.start_server()
except KeyboardInterrupt:
print("Interrupted")
try:
sys.exit(0)
except SystemExit:
os._exit(0)