generated from otter18/telegram-bot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.py
75 lines (58 loc) · 2.24 KB
/
webhook.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
import datetime
import time
import traceback
import pytz
from flask import request
from werkzeug.exceptions import HTTPException
from setup import *
# ------------- server boot time -------------
boot_time = time.time()
boot_date = datetime.datetime.now(tz=pytz.timezone("Europe/Moscow"))
# -------------- Exception handler --------------
@app.errorhandler(Exception)
def handle_exception(e):
if isinstance(e, HTTPException):
return e
logger.error(traceback.format_exc())
return "Oops", 500
# -------------- status webpage --------------
@app.route('/')
def status():
password = request.args.get("password")
if password != ADMIN_PASSWORD:
logger.info('Status page loaded without password')
return "<h1>Access denied!<h1>", 403
return f'<h1>This is telegram bot server, ' \
f'<a href="https://github.com/otter18/telegram-bot-template">templated</a> by ' \
f'<a href="https://github.com/otter18">@otter18</a></h1>' \
f'<p>Server uptime: {datetime.timedelta(seconds=time.time() - boot_time)}</p>' \
f'<p>Server last boot at {boot_date}'
# ------------- webhook ----------------
@app.route('/' + WEBHOOK_TOKEN, methods=['POST'])
def getMessage():
# temp = request.stream.read().decode("utf-8")
temp = request.get_data().decode("utf-8")
temp = telebot.types.Update.de_json(temp)
logger.debug('New message received. raw: %s', temp)
bot.process_new_updates([temp])
return "!", 200
@app.route("/set_webhook")
def webhook_on():
password = request.args.get("password")
if password != ADMIN_PASSWORD:
logger.info('Set_webhook page loaded without password')
return "<h1>Access denied!<h1>", 403
bot.remove_webhook()
url = 'https://' + os.environ.get('HOST') + '/' + WEBHOOK_TOKEN
bot.set_webhook(url=url)
logger.info(f'Webhook is ON! Url: %s', url)
return "<h1>WebHook is ON!</h1>", 200
@app.route("/remove_webhook")
def webhook_off():
password = request.args.get("password")
if password != ADMIN_PASSWORD:
logger.info('Remove_webhook page loaded without password')
return "<h1>Access denied!<h1>", 403
bot.remove_webhook()
logger.info('WebHook is OFF!')
return "<h1>WebHook is OFF!</h1>", 200