-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
52 lines (40 loc) · 1.3 KB
/
server.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
# -*- coding: utf-8 -*-
import logging
import os
import time
import flask
import telebot
from flask_sslify import SSLify
from base.bot.bot import TELEGRAM_BOT_NAME, TELEGRAM_BOT_TOKEN
from popot_bot import bot
logger = logging.getLogger(__name__)
app = flask.Flask(__name__)
sslify = SSLify(app)
WEBHOOK_URL_PATH = "/{}".format(TELEGRAM_BOT_TOKEN)
@app.route('/', methods=["GET"])
def index():
bot.remove_webhook()
time.sleep(0.1)
bot.set_webhook(
url="https://{}.herokuapp.com/{}".format(TELEGRAM_BOT_NAME, TELEGRAM_BOT_TOKEN))
return "Set Webhook!", 200
@app.route('/deleteWebhook', methods=["GET"])
def deleteWebhook():
bot.remove_webhook()
return "Remove Webhook!", 200
# @app.route("/")
@app.route(WEBHOOK_URL_PATH, methods=['POST'])
def webhook():
logging.info(" >>> POST webhook <<<")
# return "<h1>ddd</h1>"
if flask.request.headers.get('content-type') == 'application/json':
json_string = flask.request.get_data().decode('utf-8')
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])
return "OK", 200
else:
logging.error(" !!! POST 403 !!!")
flask.abort(403)
if __name__ == "__main__":
app.debug = True
app.run(host="0.0.0.0", port=int(os.environ.get('PORT', 5000)))