D
D
Dozen Frozenballs2016-11-16 05:55:48
Python
Dozen Frozenballs, 2016-11-16 05:55:48

How to start the simultaneous work of sending messages by timer and leave manual requests and location changes working?

There is a telegram bot, to which I try to bind the sending of weather on a timer. Separately, it works on a timer, as well as separately sends requests for the weather manually, like "what's the weather like right now." There was a problem - to connect both processes, that is, it smells like multithreading, as I understand it. I found an option - to use schedule and ran into the following problem: after setting the timer time, I remain in the function with sending messages on the timer, that is, manually all functions fall off.
Here is the code for your reference:

def set_time(message): #Функция с запуском таймера и отправкой сообщения погоды
    timer = message.text
    schedule.every().day.at(timer).do(timer_alert, message)
    nowTime = datetime.now()
    print ('%s' % nowTime.strftime("%d.%m.%Y %H:%M:%S"), "// Set timer at: ", timer)
    process_step(message)
    while True:
        schedule.run_pending()

def timer_alert(message): #Сообщение по таймеру
    nowTime = datetime.now()
    bot.send_message(message.chat.id, 'Погода пришла!')
    print ('%s' % nowTime.strftime("%d.%m.%Y %H:%M:%S"), '// Send weather timer message')

@bot.message_handler(content_types=['location']) #обработка локации
def handle_location(message):
    nowTime = datetime.now()
    lat = message.location.latitude
    lng = message.location.longitude
    bot.reply_to(message, '%s\n%s' % (lat, lng))
    send_alltime(message)
    global forecast
    forecast = forecastio.load_forecast(api_key, lat, lng, time=datetime.now(), units="si")
    print ('%s' % nowTime.strftime("%d.%m.%Y %H:%M:%S"), '// Set location message')

Actually the question is how to start the simultaneous work of sending messages on a timer and leave manual requests and changing the location working.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nllm, 2016-11-16
@nllm

The easiest way is to run the mailing list separately with cron

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question