-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwsgi.py
78 lines (62 loc) · 1.88 KB
/
wsgi.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
import logging
import os
from logging.config import fileConfig
import telebot
from flask import request
from ifaxbotcovid.bot import factory
from ifaxbotcovid.config.utils import settings
# logging settings
fileConfig('ifaxbotcovid/config/logging.ini')
botlogger = logging.getLogger('botlogger')
# getting environ vars
TOKEN = os.environ.get('TOKEN')
URL = os.environ.get('URL')
if TOKEN is None:
botlogger.warning('TOKEN should be defined as system var')
if URL is None:
botlogger.warning('URL should be defined as system var')
# creating Flask, Telebot and Chef instances
app = factory.create_app()
bot, tblogger = factory.create_bot(
TOKEN, get_logger=True, loglevel=logging.INFO
)
chef = factory.create_chef(
short_procedure_key=settings.short_procedure_key,
check_phrases=settings.key_words,
stop_phrase=settings.stop_phrase,
maxlen=3,
time_gap=2,
logger=botlogger
)
# registering into flask
app.config['TELEBOT'] = bot
app.config['TELEBOT_LOGGER'] = tblogger
app.config['COVIDCHEF'] = chef
# registering bot handlers
with app.app_context():
from ifaxbotcovid.bot.handlers import BotHandlers
BotHandlers.register()
#
# FLASK ROUTES
#
@app.route('/' + TOKEN, methods=['POST'])
def get_message():
json_string = request.get_data().decode('utf-8')
update = telebot.types.Update.de_json(json_string)
app.logger.debug('Get some message')
bot.process_new_updates([update])
return "!", 200
@app.route('/setwebhook', methods=['GET', 'POST'])
def set_webhook():
bot.remove_webhook()
s = bot.set_webhook('{URL}{HOOK}'.format(URL=URL, HOOK=TOKEN))
if s:
app.logger.info('Webhook setup successful')
return "webhook setup ok"
else:
app.logger.critical('Webhook setup failed!')
return "webhook setup failed"
@app.route('/')
def index():
app.logger.debug('Operational test. Serving normally')
return '.'