-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
50 lines (42 loc) · 1.22 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import request
from application import app, bot, SECRET_TOKEN
from webhook import set_webhook
import json
import logging
import os
import sys
import telebot
import traceback
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logging.info('Starting...')
@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()
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)