|
1 | 1 | # -*- encoding: utf-8 -*-
|
2 | 2 |
|
3 |
| -import traceback |
| 3 | +import sys |
| 4 | +import logging |
4 | 5 |
|
5 | 6 | from flask import Flask, request, abort
|
6 | 7 | from vbio.bot import VkBot
|
7 | 8 | from vbio.types import VkBotServer
|
8 |
| -from datetime import datetime |
9 | 9 |
|
10 | 10 | __all__ = ('FlaskServer',)
|
11 | 11 |
|
12 | 12 |
|
13 | 13 | class FlaskServer(VkBotServer):
|
14 | 14 |
|
15 |
| - def __init__(self, bot: VkBot, host: str = '0.0.0.0', port: int = 5000): |
| 15 | + def __init__(self, bot: VkBot, secret: str, confirmation: str, app: Flask = None): |
| 16 | + |
16 | 17 | self.bot = bot
|
17 |
| - self.host = host |
18 |
| - self.port = port |
19 |
| - self.app = None |
| 18 | + self.secret = secret |
| 19 | + self.confirmation = confirmation |
20 | 20 |
|
21 |
| - def run(self): |
22 |
| - self.app = Flask(__name__) |
| 21 | + self.app = app or Flask(__name__) |
23 | 22 |
|
24 |
| - @self.app.route('/', methods=['POST']) |
25 |
| - def message_handler(): |
26 |
| - data = request.json |
| 23 | + def message_handler(self): |
| 24 | + data = request.json |
27 | 25 |
|
28 |
| - if data is None: |
29 |
| - return abort(400) |
| 26 | + if data is None: |
| 27 | + return abort(400) |
30 | 28 |
|
31 |
| - if data.get('secret', '') != self.bot.secret: |
32 |
| - return abort(403) |
| 29 | + if data.get('secret', '') != self.secret: |
| 30 | + self.bot.logger.warning('Invalid secret passed!') |
| 31 | + return abort(403) |
33 | 32 |
|
34 |
| - if data.get('type') == 'confirmation': |
35 |
| - return self.bot.confirmation |
| 33 | + if data.get('type') == 'confirmation': |
| 34 | + self.bot.logger.info('Confirmation sent') |
| 35 | + return self.confirmation |
36 | 36 |
|
37 |
| - elif data.get('type') == 'message_new': |
38 |
| - try: |
39 |
| - self.bot.process_message(data['object']) |
| 37 | + try: |
| 38 | + if data.get('type') == 'message_new': |
| 39 | + self.bot.process_message(data['object']) |
| 40 | + self.bot.logger.info('Processed message from {}: {}'.format(data['object'].get('from_id'), |
| 41 | + data['object'].get('text')[:40])) |
40 | 42 |
|
41 |
| - except Exception as e: |
42 |
| - if self.bot.logger is not None: |
43 |
| - self.bot.logger.error( |
44 |
| - '[X] {} Error \n{}\ncaused during handling request: ' |
45 |
| - '\n{}\n-------------'.format( |
46 |
| - datetime.now().strftime("%d/%m/%y %H:%M:%S"), |
47 |
| - traceback.format_exc(), |
48 |
| - data |
49 |
| - ) |
50 |
| - ) |
| 43 | + else: |
| 44 | + self.bot.process_request(data) |
| 45 | + self.bot.logger.info('Processed request: {}'.format(data.get('type'))) |
51 | 46 |
|
52 |
| - if not self.bot.ignore_errors: |
53 |
| - raise e |
| 47 | + except Exception as ex: |
| 48 | + self.bot.logger.error('From {}'.format(data.get('type')), exc_info=sys.exc_info()) |
| 49 | + if not self.bot.ignore_errors: |
| 50 | + raise ex |
54 | 51 |
|
55 |
| - else: |
56 |
| - try: |
57 |
| - self.bot.process_request(data) |
58 |
| - |
59 |
| - except Exception as e: |
60 |
| - if self.bot.logger is not None: |
61 |
| - self.bot.logger.error( |
62 |
| - '[X] {} Error \n{}\ncaused during handling request: ' |
63 |
| - '\n{}\n-------------'.format( |
64 |
| - datetime.now().strftime("%d/%m/%y %H:%M:%S"), |
65 |
| - traceback.format_exc(), |
66 |
| - data |
67 |
| - ) |
68 |
| - ) |
69 |
| - |
70 |
| - if not self.bot.ignore_errors: |
71 |
| - raise e |
72 |
| - |
73 |
| - return 'ok' |
74 |
| - |
75 |
| - self.app.run(self.host, self.port, debug=False) |
| 52 | + return 'ok' |
| 53 | + |
| 54 | + def run(self, path: str = '/', *args, **kwargs): |
| 55 | + self.app.route(path, methods=['POST'])( |
| 56 | + self.message_handler |
| 57 | + ) |
| 58 | + |
| 59 | + logging.getLogger('werkzeug').setLevel(logging.FATAL) |
| 60 | + self.bot.logger.info('starting webhook') |
| 61 | + self.app.run(*args, **kwargs) |
0 commit comments