Skip to content

Commit 5f32ef9

Browse files
committed
Redo Heroku stuff
1 parent 94e5fce commit 5f32ef9

7 files changed

Lines changed: 70 additions & 11 deletions

File tree

Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM ubuntu:latest
2+
3+
WORKDIR /opt
4+
COPY . .
5+
6+
RUN apt update
7+
RUN apt upgrade -y
8+
RUN apt install -y python3 python3-pip wget
9+
10+
RUN pip install -r requirements.txt
11+
12+
RUN wget https://github.com/caddyserver/caddy/releases/download/v2.5.1/caddy_2.5.1_linux_amd64.tar.gz
13+
RUN tar -xzvf caddy_2.5.1_linux_amd64.tar.gz
14+
RUN rm caddy_2.5.1_linux_amd64.tar.gz
15+
16+
ENTRYPOINT ["sh", "-c", "/opt/heroku_startup.sh"]

Procfile

Lines changed: 0 additions & 2 deletions
This file was deleted.

app.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@
33
"description": "Visual Weather api. Returns beautiful pictures with the current weather.",
44
"repository": "https://github.com/mishailovic/VWapi",
55
"website": "https://github.com/mishailovic/VWapi",
6+
"stack": "container",
67
"env": {
8+
"APP_NAME": {
9+
"description": "Heroku app name",
10+
"value": "vwapi"
11+
},
712
"BOT_TOKEN": {
813
"description": "Your Telegram bot token",
914
"value": ""
1015
},
1116
"LRU_SIZE": {
1217
"description": "Size on an LRU cache, by default takes around 51 MB of RAM when full",
13-
"value": "1024"
18+
"value": "1024",
19+
"required": false
1420
},
1521
"LRU_EXPIRE": {
1622
"description": "Time in seconds after which LRU cache entry expires (default: 30 min)",
17-
"value": "1800"
23+
"value": "1800",
24+
"required": false
1825
},
1926
"MONGO_DB": {
2027
"description": "Telegram bot MongoDB database for analytics (disabled if not set)",
@@ -23,7 +30,8 @@
2330
},
2431
"API_BASE": {
2532
"description": "Change it to your local instance for better telegram bot performance.",
26-
"value": "https://vwapi.herokuapp.com"
33+
"value": "https://vwapi.herokuapp.com",
34+
"required": false
2735
}
2836
}
2937
}

bot.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import urllib
22
import uuid
3+
import os
34

45
from pymongo import MongoClient
56
from aiogram import Bot, Dispatcher, executor, types
@@ -118,4 +119,20 @@ async def inline_echo(inline_query: InlineQuery):
118119

119120

120121
if __name__ == "__main__":
121-
executor.start_polling(dp, skip_updates=True)
122+
# Heroku defines DYNO environment vaiable
123+
if os.environ.get("DYNO"):
124+
app_name = os.environ.get("APP_NAME")
125+
126+
async def on_startup(dp):
127+
await bot.set_webhook(f"https://{app_name}.herokuapp.com/bot/{TOKEN}")
128+
129+
executor.start_webhook(
130+
dispatcher=dp,
131+
webhook_path=f"/bot/{TOKEN}",
132+
on_startup=on_startup,
133+
skip_updates=True,
134+
host=f"https://{app_name}.herokuapp.com",
135+
port=8081, # Caddy will take care of that
136+
)
137+
else:
138+
executor.start_polling(dp, skip_updates=True)

config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
OWM_TOKEN = "9de243494c0b295cca9337e1e96b00e2" # pro token already here, no need to change it
44

5-
LRU_SIZE = 1024 # Size on an LRU cache, by default takes around 51 MB of RAM when full
6-
LRU_EXPIRE = 1800 # Time in seconds after which LRU cache entry expires (default: 30 min)
5+
LRU_SIZE = int(os.environ.get("LRU_SIZE", "1024")) # Size on an LRU cache, by default takes around 51 MB of RAM when full
6+
LRU_EXPIRE = int(os.environ.get("LRU_EXPIRE", "1800")) # Time in seconds after which LRU cache entry expires (default: 30 min)
77

8-
TOKEN = os.environ.get('BOT_TOKEN') # Telegram bot token, (TOKEN = "123456:qwertyuiop") if you running locally
8+
TOKEN = os.environ.get("BOT_TOKEN") # Telegram bot token, (TOKEN = "123456:qwertyuiop") if you running locally
99

10-
MONGO_DB = "" # Telegram bot MongoDB database for analytics (disabled if not set)
10+
MONGO_DB = os.environ.get("MONGO_DB", "") # Telegram bot MongoDB database for analytics (disabled if not set)
1111

12-
API_BASE = "https://vwapi.herokuapp.com" # Change it to your local instance for better telegram bot performance.
12+
API_BASE = os.environ.get("API_BASE", "https://vwapi.herokuapp.com") # Change it to your local instance for better telegram bot performance.

heroku.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build:
2+
docker:
3+
web: Dockerfile

heroku_startup.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Start the services in the background
2+
python3 -m uvicorn weatherapi:app --port=8080 &
3+
python3 bot.py &
4+
5+
cat << EOF > /opt/Caddyfile
6+
http://:${PORT} {
7+
route / {
8+
reverse_proxy localhost:8080
9+
}
10+
11+
route /bot/* {
12+
reverse_proxy localhost:8081
13+
}
14+
}
15+
EOF
16+
17+
/opt/caddy run -config /opt/Caddyfile

0 commit comments

Comments
 (0)