-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
69 lines (55 loc) · 1.45 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# server.py
import fastapi
import uvicorn
import telebot
from configs import env
from telebot import TeleBot
BOT_NAME = env.BOT_NAME
SERVER_HOST = env.SERVER_HOST
SERVER_PORT = env.SERVER_PORT
SERVER_LISTEN = env.SERVER_LISTEN
WEBHOOK_HOST = env.WEBHOOK_HOST
WEBHOOK_PORT = env.WEBHOOK_PORT
SSL_ENABLED = env.SSL_ENABLED
SSL_CERT = env.SSL_CERT
SSL_PRIV = env.SSL_PRIV
URL_BASE = "https://{}:{}".format(WEBHOOK_HOST, WEBHOOK_PORT)
URL_PATH = "/{}/".format(BOT_NAME)
UDS_PATH = "/tmp/randomology/uvicorn.sock"
# when in production
def run(bot:TeleBot):
# create the app
app = fastapi.FastAPI(docs=None, redoc_url=None)
@app.post(f'/{BOT_NAME}/')
def process_webhook(update:dict):
if update:
update = telebot.types.Update.de_json(update)
bot.process_new_updates([update])
else:
return
# remove previous webhook(?)
bot.remove_webhook()
# set webhook
bot.set_webhook(
url=URL_BASE+URL_PATH
)
if SSL_ENABLED :
# run the server
uvicorn.run(
app,
host=SERVER_LISTEN,
port=SERVER_PORT,
ssl_certfile=SSL_CERT,
ssl_keyfile=SSL_PRIV,
uds=UDS_PATH
)
else:
uvicorn.run(
app,
host=SERVER_LISTEN,
port=SERVER_PORT,
uds=UDS_PATH
)
# when in dev environment
def run_dev(bot:TeleBot):
bot.infinity_polling()