Skip to content

Commit

Permalink
replace passing url of image to letegram with download/upload
Browse files Browse the repository at this point in the history
rationale: telegram downloads image only once and then use a cached copy

side change: make MAP_URL configurable
  • Loading branch information
yurnov committed Mar 27, 2024
1 parent ad65d0a commit c5a453d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env.exmple
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHAT_ID=<CHAT_ID>
# TIMEZONE=Europe/Kiev
# To send map with alerts, optional, default False
# MAP=False
# MAP_URL=https://ubilling.net.ua/aerialalerts/?map=true
# List of regions to filter, don't use spaces, single line, optional
# REGION_LIST="Одеська область","Київська область","Житомирська область","м. Київ","Львівська область"
# Full list of regions:
Expand Down
25 changes: 19 additions & 6 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import time
from datetime import datetime
import pytz
import urllib.parse

"""
This is a simple telegram bot that every 30 secound get API http://alerts.net.ua/explosives_statuses_v2.json and send
Expand Down Expand Up @@ -48,6 +47,7 @@
TIMEZONE = os.getenv("TIMEZONE")
SLIENT = os.getenv("SLIENT")
MAP = os.getenv("MAP")
MAP_URL = os.getenv("MAP_URL")

"""
Full list of regions:
Expand Down Expand Up @@ -127,6 +127,10 @@
else:
MAP = MAP.lower()

if MAP == "true" and not MAP_URL:
logger.warning("MAP_URL is not defined in .env file, using a default URL http://alerts.net.ua/alerts_map.png")
MAP_URL = "http://alerts.net.ua/alerts_map.png"

logger.info(f"Bot started with CHAT_ID: {CHAT_ID} and SLIENT: {SLIENT}")
logger.info(f"Following regions will be monitored: {REGION_LIST}")

Expand All @@ -142,9 +146,10 @@ def get_data():


def send_message(text):
url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={CHAT_ID}&disable_notification={SLIENT}&text={text}"
url = f"https://api.telegram.org/bot{TOKEN}/sendMessage"
params = {"chat_id": CHAT_ID, "text": text, "disable_notification": SLIENT}
try:
response = requests.get(url, timeout=20)
response = requests.get(url, params=params, timeout=20)
response.raise_for_status()
except requests.exceptions.RequestException as e:
logger.error(f"Error while sending message: {e}")
Expand All @@ -153,10 +158,18 @@ def send_message(text):


def send_map(text):
MAP_URL = "http://alerts.net.ua/alerts_map.png"
url = f"https://api.telegram.org/bot{TOKEN}/sendPhoto?chat_id={CHAT_ID}&photo={urllib.parse.quote(MAP_URL)}&caption={text}&disable_notification={SLIENT}"

try:
alarm_map = requests.get(MAP_URL, timeout=15)
alarm_map.raise_for_status()
except requests.exceptions.RequestException as e:
logger.error(f"Error while getting map: {e}")
return None

url = f"https://api.telegram.org/bot{TOKEN}/sendPhoto"
params = {"chat_id": CHAT_ID, "caption": text, "disable_notification": SLIENT}
try:
response = requests.get(url, timeout=20)
response = requests.post(url, params=params, timeout=20, files={"photo": alarm_map.content})
response.raise_for_status()
except requests.exceptions.RequestException as e:
logger.error(f"Error while sending alert map: {e}")
Expand Down

0 comments on commit c5a453d

Please sign in to comment.