-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (55 loc) · 1.58 KB
/
main.py
File metadata and controls
65 lines (55 loc) · 1.58 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
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import request
from application import app, bot, scheduler, SECRET_TOKEN
from src.race.application.get_races import get_races
from src.race.domain.race import Race
from utils.schedule import schedule_next_race
from src.race.application.finish_race import finish_race
from webhook import set_webhook
import json
import logging
import model
import os
import sys
import telebot
import traceback
logging.basicConfig(stream=sys.stdout)
logging.getLogger().setLevel(logging.INFO)
logging.info('Starting...')
scheduler.start()
race = Race.get_current_race()
if race != None:
finish_race()
else:
schedule_next_race()
@ app.route('/me', methods=['GET'])
def send_me():
"""
Devuelve información del bot
"""
me = bot.get_me()
return json.dumps(me, default=lambda o: o.__dict__, sort_keys=True, indent=4)
@ app.route('/webhook' + SECRET_TOKEN, methods=['POST'])
def get_messages():
"""
Se encarga de procesar los mensajes recibidos por el bot
"""
try:
logging.info("Updating message")
bot.process_new_updates(
[telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
except Exception as e:
logging.error("Exception raised")
logging.error(repr(e))
logging.error(traceback.format_exc())
return "!", 200
if bot.threaded:
logging.info('Polling...')
bot.remove_webhook()
bot.polling()
exit(0)
if __name__ == '__main__':
set_webhook(True)
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)