2019-10-11 22:40:28 +02:00
|
|
|
import logging
|
2020-09-24 11:41:17 +02:00
|
|
|
import threading
|
|
|
|
import time
|
2020-05-12 22:29:44 +02:00
|
|
|
|
2020-09-24 11:41:17 +02:00
|
|
|
import schedule
|
2020-09-24 19:34:29 +02:00
|
|
|
from telegram import InlineKeyboardMarkup, ParseMode
|
|
|
|
from telegram.error import BadRequest
|
2021-02-12 20:38:08 +01:00
|
|
|
from telegram.ext import Updater, CommandHandler
|
2019-10-11 22:40:28 +02:00
|
|
|
|
2019-10-17 19:03:09 +02:00
|
|
|
from exceptions import *
|
2021-02-12 20:38:08 +01:00
|
|
|
from marudor_departures import handle_marudor_departures
|
2020-09-24 19:34:29 +02:00
|
|
|
from push_information import __init__ as push_init
|
2020-09-18 22:36:37 +02:00
|
|
|
from weather_meteomedia import handle_meteomedia
|
2020-09-24 11:41:17 +02:00
|
|
|
from weather_openweathermap import __init__ as openweathermap_init
|
2020-09-24 19:34:29 +02:00
|
|
|
from weather_openweathermap import handle_weather
|
2019-10-17 19:03:09 +02:00
|
|
|
|
2019-10-11 22:40:28 +02:00
|
|
|
token_file = open("token", "r")
|
|
|
|
updater = Updater(token=token_file.read(), use_context=True)
|
|
|
|
token_file.close()
|
|
|
|
|
|
|
|
dispatcher = updater.dispatcher
|
|
|
|
|
|
|
|
logging.basicConfig(format='%(acstime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
|
|
|
|
|
2019-10-17 19:03:09 +02:00
|
|
|
|
2020-09-24 11:41:17 +02:00
|
|
|
def send_message(chat_id: int, message: str):
|
2020-09-24 19:34:29 +02:00
|
|
|
dispatcher.bot.send_message(chat_id=chat_id, text=message, disable_notification=True, parse_mode=ParseMode.MARKDOWN)
|
|
|
|
|
|
|
|
|
|
|
|
def edit_message_and_delete_answer(chat_id: int, message_id: int, message: str,
|
|
|
|
reply_markup: InlineKeyboardMarkup = None):
|
|
|
|
dispatcher.bot.delete_message(chat_id=chat_id, message_id=message_id)
|
|
|
|
try:
|
|
|
|
dispatcher.bot.edit_message_text(chat_id=chat_id, message_id=message_id - 1, text=message,
|
|
|
|
reply_markup=reply_markup)
|
|
|
|
except BadRequest:
|
|
|
|
try:
|
|
|
|
dispatcher.bot.edit_message_text(chat_id=chat_id, message_id=message_id - 2, text=message,
|
|
|
|
reply_markup=reply_markup)
|
|
|
|
except BadRequest:
|
|
|
|
pass
|
2020-09-24 11:41:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
cease_continuous_run = threading.Event()
|
|
|
|
|
|
|
|
|
|
|
|
class ScheduleThread(threading.Thread):
|
|
|
|
@classmethod
|
|
|
|
def run(cls):
|
|
|
|
while not cease_continuous_run.is_set():
|
|
|
|
schedule.run_pending()
|
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
|
2020-05-12 22:27:47 +02:00
|
|
|
def __main__():
|
2020-09-18 22:36:37 +02:00
|
|
|
dispatcher.add_handler(CommandHandler('meteomedia', handle_meteomedia))
|
2021-02-12 20:38:08 +01:00
|
|
|
dispatcher.add_handler(CommandHandler('weather', handle_weather))
|
|
|
|
dispatcher.add_handler(CommandHandler('departure', handle_marudor_departures))
|
2020-09-18 22:36:37 +02:00
|
|
|
|
2020-09-24 11:41:17 +02:00
|
|
|
openweathermap_init()
|
|
|
|
dispatcher.add_handler(push_init())
|
2019-10-11 22:40:28 +02:00
|
|
|
|
2020-05-12 22:27:47 +02:00
|
|
|
dispatcher.add_error_handler(error_callback)
|
2019-10-11 22:40:28 +02:00
|
|
|
|
2020-09-24 11:41:17 +02:00
|
|
|
continuous_thread = ScheduleThread()
|
|
|
|
continuous_thread.start()
|
|
|
|
|
2020-05-12 22:27:47 +02:00
|
|
|
updater.start_polling()
|
|
|
|
updater.idle()
|
|
|
|
|
2020-09-24 11:41:17 +02:00
|
|
|
cease_continuous_run.set()
|
|
|
|
|
2020-05-12 22:27:47 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
__main__()
|