Skip to content

Commit

Permalink
send plots to knew subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
juckerj committed Jan 31, 2023
1 parent 919d745 commit 6eabe7e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
23 changes: 22 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def __init__(self,token):
# start the bot
self.updater.start_polling()

self._new_users_waiting_for_plots = []

def _start(self,update: Update, context: CallbackContext):
reply_text = "Hi! I am OpenEns. I supply you with the latest ECWMF meteograms. \
As soon as the latest forecast is available I deliver them to you. \
Expand All @@ -43,7 +45,7 @@ def _start(self,update: Update, context: CallbackContext):
update.message.reply_text(reply_text, reply_markup=markup)

def _subscribe(self,update: Update, context: CallbackContext):
reply_text = "You sucessfully subscribed."
reply_text = "You sucessfully subscribed. You will receive your first plots in a minute or two..."
update.message.reply_text(reply_text)

# add user to subscription list
Expand All @@ -53,6 +55,8 @@ def _subscribe(self,update: Update, context: CallbackContext):

logging.info(context.bot_data.setdefault('user_id', set()))

self._new_users_waiting_for_plots.append(user_id)

def _unsubscribe(self,update: Update, context: CallbackContext):
reply_text = "You sucessfully unsubscribed."
update.message.reply_text(reply_text)
Expand All @@ -75,6 +79,23 @@ def _get_ip_address(self,update: Update, context: CallbackContext):
reply_text = f"IP-ADDRESS: {ip_address}"
update.message.reply_text(reply_text)

def has_new_subscribers_waiting(self):
if self._new_users_waiting_for_plots:
return True
else:
return False

def send_plots_to_new_subscribers(self,plots):
for user_id in self._new_users_waiting_for_plots:
logging.debug(user_id)
for station_name in plots:
message = station_name
self._dp.bot.send_message(chat_id=user_id, text=message)
for plot in plots[station_name]:
self._dp.bot.send_photo(chat_id=user_id, photo=open(plot, 'rb'))
logging.info('plots sent')
self._new_users_waiting_for_plots = []


def broadcast(self,plots):
if plots:
Expand Down
12 changes: 11 additions & 1 deletion ecmwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self,name,lat,lon):
TST = Station(name='Tschiertschen',lat='46.8167',lon='9.6')
DVS = Station(name='Davos',lat='46.8043',lon='9.83723')
ELM = Station(name='Elm',lat='46.9167',lon='9.16667')
ALL_STATIONS = [TST,DVS,ELM]
ALL_STATIONS = [TST,DVS]
ALL_EPSGRAM = [d10_plume,d10_eps,d15_eps]

class EcmwfApi():
Expand Down Expand Up @@ -98,6 +98,16 @@ def _save_image_of_station(self,image_api,station,eps_type):
logging.info("image saved in {}".format(file))
return file

def download_plots(self):
for Station in self._stations:
self._download_plots(Station)

# copy because we reset _plots_for_broadcast now
plots_for_broadcast = self._plots_for_broadcast.copy()
self._plots_for_broadcast = {}

return plots_for_broadcast

def download_latest_plots(self):
for Station in self._stations:
if self._new_forecast_available(Station):
Expand Down
4 changes: 3 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ def main():

while True:

if bot.has_new_subscribers_waiting():
bot.send_plots_to_new_subscribers(ecmwf.download_plots())
bot.broadcast(ecmwf.download_latest_plots())

snooze = 120
snooze = 1
logging.debug(f'snooze {snooze}s ...')
time.sleep(snooze)

Expand Down

0 comments on commit 6eabe7e

Please sign in to comment.