-
Notifications
You must be signed in to change notification settings - Fork 2
/
dev_webhook.py
50 lines (40 loc) · 1.16 KB
/
dev_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
from telegram import Update
from telegram.ext import Updater
from flask import Flask, request
import os
from bot.telegram_bot import setup_handlers
from pyngrok import ngrok
import logging
TOKEN = os.environ.get("TEST_TOKEN")
app = Flask(__name__)
updater = Updater(token=TOKEN)
dispatcher = updater.dispatcher
log_file = "telegram_bot.log"
logging.basicConfig(
filename=log_file,
filemode="a",
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
@app.route("/webhook", methods=["POST"])
def webhook():
update = Update.de_json(request.get_json(), updater.bot)
dispatcher.process_update(update)
return "OK"
@app.route("/")
def index():
return "Bot is running!"
setup_handlers(dispatcher)
if __name__ == "__main__":
public_url = ngrok.connect(5000, "http").public_url
updater.bot.set_webhook(url=f"{public_url}/webhook")
print("#" * 50)
print(f"# Bot is running at @{updater.bot.get_me().username} #")
print("#" * 50)
app.run(
host="0.0.0.0",
port=int(os.environ.get("PORT", 5000)),
debug=True,
use_reloader=False,
)