N
N
n2932018-02-08 11:10:26
Python
n293, 2018-02-08 11:10:26

How to start and disable the program through telegram?

Hey!
There is a program for which I created a telegram bot. Through it, I want to use the command to start and stop the program.
Now it turns out that it is only starting up, but, in fact, it is impossible to stop it. Despite sending the /deactivate command, the program continues to run.
Maybe I messed something up with the delays? Or something else, I can't figure it out.
Here is the telegram bot code, it accepts commands from me and, in theory, should execute them (/activate works, but /deactivate does not):

from telegram.ext import Updater, CommandHandler
from telegram_features.telegram_private_keys import telegram_api_key,tel_id
from telegram_features.telegram_notification import send_notification
from main import init_program
from functools import wraps


def restricted(func):
    @wraps(func)
    def wrapped(bot, update, *args, **kwargs):
        user_id = update.effective_user.id
        if user_id != tel_id:
            send_notification('Неавторизованный доступ с ID: {}.'.format(user_id))
            return
        return func(bot, update, *args, **kwargs)
    return wrapped


@restricted
def activate_program_bot(bot, update, job_queue, user_data):
    program_job = job_queue.run_repeating(init_program, 300, first=1)
    user_data['program_job'] = program_job
    send_notification('программа начала работу')


@restricted
def deactivate_program_bot(bot, update, user_data):
    program_job = user_data.get('program_job')
    if program_job:
        program_job.schedule_removal()
        send_notification('программа завершила работу')
    send_notification('программа видимо не завершила работу')

def main():
    updater = Updater(telegram_api_key)
    dp = updater.dispatcher
    dp.add_handler(CommandHandler("activate", activate_program_bot, pass_job_queue=True, pass_user_data=True))
    dp.add_handler(CommandHandler("deactivate", deactivate_program_bot, pass_user_data=True))
    updater.start_polling(timeout=1000)

if __name__ == '__main__':
    main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
n293, 2018-03-13
@n293

Yes, I decided, the problem turned out to be in another section of the code. I used the decorator @run_acyncfrom the python-telegram-bot library without understanding it a bit.
It turned out that I started several threads with the /activate command (I entered it several times), and I entered the /deactivate command once. Instead of how many I entered the /activate command.
As a result, I came to the conclusion that I do not need asynchrony for this task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question