-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
57 lines (46 loc) · 1.54 KB
/
bot.py
File metadata and controls
57 lines (46 loc) · 1.54 KB
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
from http.server import BaseHTTPRequestHandler,HTTPServer
import json
import events
import threading
import config
from irc import IrcConnection
irc = None
# handle POST events from github server
# We should also make sure to ignore requests from the IRC, which can clutter
# the output with errors
CONTENT_TYPE = 'content-type'
CONTENT_LEN = 'content-length'
EVENT_TYPE = 'x-github-event'
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
pass
def do_CONNECT(self):
pass
def do_POST(self):
print(self.headers)
if not all(x in self.headers for x in [CONTENT_TYPE, CONTENT_LEN, EVENT_TYPE]):
return
content_type = self.headers['content-type']
content_len = int(self.headers['content-length'])
event_type = self.headers['x-github-event']
data = self.rfile.read(content_len)
self.send_response(200)
self.send_header('content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes('OK', 'utf-8'))
events.handle_event(irc, event_type, json.loads(data.decode()))
return
def worker():
irc.loop()
irc = IrcConnection('config.ini')
t = threading.Thread(target=worker)
t.start()
if irc.widelands['webhook']['start']:
# Run Github webhook handling server
try:
server = HTTPServer((irc.widelands['webhook']['host'], irc.widelands['webhook']['port']), MyHandler)
server.serve_forever()
except KeyboardInterrupt:
print("Exiting")
server.socket.close()
irc.stop_loop()