Skip to content

Commit

Permalink
optional map of alerts added, will be sent as separate message
Browse files Browse the repository at this point in the history
  • Loading branch information
yurnov committed Mar 27, 2024
1 parent 8af7f1a commit ad65d0a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.exmple
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CHAT_ID=<CHAT_ID>
# URL=http://alerts.net.ua/explosives_statuses_v2.json
# Timezone, optional, default Europe/Kiev
# TIMEZONE=Europe/Kiev
# To send map with alerts, optional, default False
# MAP=False
# List of regions to filter, don't use spaces, single line, optional
# REGION_LIST="Одеська область","Київська область","Житомирська область","м. Київ","Львівська область"
# Full list of regions:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

Додатково можна налаштувати часовий пояс (за змовчуванням використовується `Europe/Kyiv` і для нього нічого вказувати не потрібно), а також вимкнути нотифікації за допомогою параметра `SLIENT` (його можна ставити у `true` чи `false`)

Також, бот може відправляти схематичну карту повітряних тривог з сервера даних [JAAM - Just another alerts map](https://github.com/J-A-A-M/ukraine_alarm_map), для цього додай параметер `MAP` (його можна ставьт у `true` чи `false`, за змовчуванням `false`).

## Запуск
### Збудуй власний імедж

Expand Down
22 changes: 22 additions & 0 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 @@ -46,6 +47,7 @@
REGION_LIST = os.getenv("REGION_LIST").split(",") if os.getenv("REGION_LIST") else None
TIMEZONE = os.getenv("TIMEZONE")
SLIENT = os.getenv("SLIENT")
MAP = os.getenv("MAP")

"""
Full list of regions:
Expand Down Expand Up @@ -119,6 +121,12 @@
else:
SLIENT = SLIENT.lower()

if not MAP or MAP.lower() not in ["true", "false"]:
logger.warning("MAP is not defined in .env file, or not a boolean, using a default value false")
MAP = "false"
else:
MAP = MAP.lower()

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 @@ -144,6 +152,18 @@ def send_message(text):
return response.json()


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:
response = requests.get(url, timeout=20)
response.raise_for_status()
except requests.exceptions.RequestException as e:
logger.error(f"Error while sending alert map: {e}")
return None
return response.json()


def main():
last_data = None

Expand Down Expand Up @@ -173,6 +193,8 @@ def main():

if message != MESSAGE:
message += "\nЙобанарусня!"
if MAP == "true":
send_map("Йобанарусня!")
send_message(message)

last_data = data
Expand Down

0 comments on commit ad65d0a

Please sign in to comment.