Exemplo n.º 1
0
def delete_message(job_queue: JobQueue, chat_id, message_id, when=0):
    key = deletion_task_key(chat_id, message_id)
    delete_tasks = job_queue.get_jobs_by_name(key)
    for task in delete_tasks:
        # Deletion will be postponed
        task.schedule_removal()
    job_queue.run_once(lambda _: delete_message_task(chat_id, message_id),
                       when,
                       name=key)
Exemplo n.º 2
0
    def deschedule(self, job_queue: JobQueue) -> bool:
        """Deschedule this event job"""
        current_jobs = job_queue.get_jobs_by_name(self.job_name)
        if not current_jobs:
            return False

        for job in current_jobs:
            job.schedule_removal()
        return True
Exemplo n.º 3
0
def restart_user_jobs(chat_id: int,
                      chat_data: Dict[str, Dict[str, Union[int, str, dict]]],
                      job_queue: JobQueue):
    current_jobs = job_queue.get_jobs_by_name(str(chat_id))
    for job in current_jobs:
        job.schedule_removal()

    settings = chat_data.get('settings')
    assert isinstance(settings, dict)

    num_of_words = settings.get('num_of_words')
    str_start_time = settings.get('start_time')
    str_end_time = settings.get('end_time')
    assert isinstance(num_of_words, int)
    assert isinstance(str_start_time, str)
    assert isinstance(str_end_time, str)

    start_time = time.fromisoformat(str_start_time)
    end_time = time.fromisoformat(str_end_time)

    time_interval = timedelta()
    days = 0 if start_time <= end_time else 1
    if num_of_words > 0:
        time_interval = timedelta(
            days=days,
            hours=end_time.hour - start_time.hour,
            minutes=end_time.minute - start_time.minute) / num_of_words

    job_times = []
    for i in range(num_of_words):
        job_datetime = datetime.combine(datetime.utcnow(),
                                        start_time) + i * time_interval
        job_time = job_datetime.timetz()

        job_queue.run_daily(send_word,
                            job_time,
                            context={'chat_id': chat_id},
                            name=str(chat_id),
                            job_kwargs={'misfire_grace_time': 20 * 60})

        job_times.append({'time': job_time.isoformat()})

    set_to_database(chat_id, 'jobs', job_times)

    return num_of_words
Exemplo n.º 4
0
def verify_user(bot: Bot, update: Update, job_queue: JobQueue):
    query_user_id = update.callback_query.from_user.id
    jobs = job_queue.get_jobs_by_name(str(update.effective_user.id))
    if not jobs:
        bot.answer_callback_query(update.callback_query.id,
                                  text=BOT_NOT_VERIFIED)
        return
    job = jobs[0]
    data = job.context
    if query_user_id == data['user_id']:
        job.schedule_removal()
        bot.delete_message(data['chat_id'], data['msg_id'])
        bot.restrict_chat_member(data['chat_id'],
                                 data['user_id'],
                                 can_send_messages=True,
                                 can_add_web_page_previews=True,
                                 can_send_media_messages=True,
                                 can_send_other_messages=True)
        bot.answer_callback_query(update.callback_query.id, text=BOT_VERIFIED)
Exemplo n.º 5
0
 def check_if_job_exists(self, job_queue: JobQueue) -> bool:
     """Return true or false whether job already exists in job_queue"""
     current_jobs = job_queue.get_jobs_by_name(self.job_name)
     if not current_jobs:
         return False
     return True
Exemplo n.º 6
0
def stop_aoc_handlers(queue: JobQueue, bot: Bot):
    (job, ) = queue.get_jobs_by_name(JOB_AOC_UPDATE)
    if job is not None:
        job.schedule_removal()
Exemplo n.º 7
0
def rm_mailing_jobs(job_queue: JobQueue, user_id, chat_id):
    """remove all mailing jobs if where were such"""
    for job in job_queue.get_jobs_by_name(
            name=consts.MAILING_JOB):  # type: Job
        if job.context[0] == chat_id and job.context[1] == user_id:
            job.schedule_removal()
Exemplo n.º 8
0
def stop_aoc_handlers(queue: JobQueue, bot: Bot):
    jobs = queue.get_jobs_by_name(JOB_AOC_UPDATE)
    for job in jobs:
        job.schedule_removal()