This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_webhook_listener.py
66 lines (52 loc) · 2.07 KB
/
sample_webhook_listener.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
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.httpclient
import tornado.escape
from tornado.options import define, options
import logging
import sys
define("port", default=8044, help="run on the given port", type=int)
tornado_logger = logging.getLogger('WebhookListener')
tornado_logger.propagate = False
tornado_logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(u"%(levelname)-8s %(name)-4s %(asctime)s,%(msecs)d %(module)s-%(funcName)s: %(message)s")
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(logging.DEBUG)
stdout_handler.setFormatter(formatter)
stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setLevel(logging.ERROR)
stderr_handler.setFormatter(formatter)
tornado_logger.addHandler(stdout_handler)
tornado_logger.addHandler(stderr_handler)
hn = logging.NullHandler()
hn.setLevel(logging.DEBUG)
logging.getLogger("tornado.access").addHandler(hn)
logging.getLogger("tornado.access").propagate = False
class MainHandler(tornado.web.RequestHandler):
async def post(self):
# tornado_logger.debug(self.request.headers['content-type'])
# tornado_logger.debug('---Transaction---')
request_json = tornado.escape.json_decode(self.request.body)
self.set_status(status_code=202)
self.write({'success': True})
if 'event_name' in request_json:
tornado_logger.debug('========New event received========')
tornado_logger.debug(request_json['event_name'])
tornado_logger.debug('---JSON Payload delivered-----')
tornado_logger.debug(request_json)
def main():
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/", MainHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
try:
tornado.ioloop.IOLoop.current().start()
except KeyboardInterrupt:
tornado_logger.debug('Shutting down...')
tornado.ioloop.IOLoop.current().stop()
if __name__ == '__main__':
main()