Ejemplo n.º 1
0
def set_interval_dialog(bot, update, chat_data, job_queue):
    """
    Begin the dialog with for setting an interval value
    
    Args:
        :param bot: <object> bot instance 
        :param update: <dict> update object that contains updates from user chat
        :param chat_data: <dict> stores the data for the chat. In this case a job object stores in it
        :param job_queue: <object> queue of jobs. The notify job automatically adds to queue after start 
        
    Returns:
        :return: settings menu identifier
    """

    msg = update.message
    interval = int(msg.text)
    if interval <= 0:
        msg.reply_text(messages.SET_INTERVAL_BAD_VALUE_TEXT,
                       parse_mode=messages.MARKDOWN)
    else:
        mq.update_setting(msg.chat_id,
                          setting=base_config.INTERVAL,
                          value=interval)
        if mq.get_user_settings(msg.chat_id)[base_config.NOTIFICATIONS]:
            chat_data['job'].schedule_removal()
            job = job_queue.run_repeating(notify,
                                          interval,
                                          context={"chat_id": msg.chat_id})
            chat_data['job'] = job
            msg.reply_text(messages.SET_INTERVAL_SUCC_TEXT.format(interval),
                           reply_markup=kb_settings,
                           parse_mode=messages.MARKDOWN)
        return SETTINGS_MENU
    return SET_INTERVAL
Ejemplo n.º 2
0
def notify(bot, job):
    """
    Send notification to user
    
    Args:
        :param bot: <telegram.Bot> bot instance
        :param job: <telegram.ext.jobqueue.Job> user's job instance
         
    """
    res = crawl(job.context['chat_id'])
    if len(res) > 0:
        try:
            bot.send_message(chat_id=job.context['chat_id'],
                             text=_generate_string(res),
                             parse_mode=messages.MARKDOWN)
        except Unauthorized:
            job.schedule_removal()
            mq.update_setting(job.context['chat_id'],
                              setting=base_config.NOTIFICATIONS,
                              value=False)
        except TimedOut:
            logger.warning('chat_id: {}; error: {}'.format(
                job.context['chat_id'], 'Time out while sending notification'))
        except Exception as e:
            logger.warning('chat_id: {}; error: {}\n'
                           '{}'.format(job.context['chat_id'], str(e),
                                       format_exc()))
Ejemplo n.º 3
0
def set_threshold(bot, update, args):
    """Set the threshold between cryptocurrencies by which the bot will notify the user"""

    msg = update.message
    try:
        threshold = float(args[0])
        if threshold <= 0:
            msg.reply_text(messages.SET_THRESHOLD_BAD_VALUE_TEXT,
                           reply_markup=kb_main,
                           parse_mode=messages.MARKDOWN)
            return MAIN_MENU
        if threshold > 100:
            msg.reply_text(messages.SET_THRESHOLD_BAD_VALUE_TEXT,
                           reply_markup=kb_main,
                           parse_mode=messages.MARKDOWN)
            return MAIN_MENU
        mq.update_setting(msg.chat_id,
                          setting=base_config.THRESHOLD,
                          value=threshold)
        msg.reply_text(messages.SET_THRESHOLD_SUCC_TEXT.format(threshold),
                       reply_markup=kb_main,
                       parse_mode=messages.MARKDOWN)
        return MAIN_MENU
    except (IndexError, ValueError):
        msg.reply_text(messages.SET_THRESHOLD_HELP_TEXT,
                       reply_markup=kb_main,
                       parse_mode=messages.MARKDOWN)
        return MAIN_MENU
    except OverflowError:
        msg.reply_text(messages.SET_THRESHOLD_BIG_VALUE_EXCEPTION,
                       reply_markup=kb_main,
                       parse_mode=messages.MARKDOWN)
        return MAIN_MENU
Ejemplo n.º 4
0
def set_interval(bot, update, args, chat_data, job_queue):
    """Set interval between notifications in seconds"""

    msg = update.message
    try:
        interval = int(args[0])
        if interval <= 0:
            msg.reply_text(messages.SET_INTERVAL_BAD_VALUE_TEXT,
                           reply_markup=kb_main,
                           parse_mode=messages.MARKDOWN)
            return MAIN_MENU
        mq.update_setting(msg.chat_id,
                          setting=base_config.INTERVAL,
                          value=interval)
        if mq.get_user_settings(msg.chat_id)[base_config.NOTIFICATIONS]:
            chat_data['job'].schedule_removal()
            job = job_queue.run_repeating(notify,
                                          interval,
                                          context={'chat_id': msg.chat_id})
            chat_data['job'] = job
        msg.reply_text(messages.SET_INTERVAL_SUCC_TEXT.format(interval),
                       reply_markup=kb_main,
                       parse_mode=messages.MARKDOWN)
        return MAIN_MENU
    except (IndexError, ValueError, IndexError):
        msg.reply_text(messages.SET_INTERVAL_HELP_TEXT,
                       reply_markup=kb_main,
                       parse_mode=messages.MARKDOWN)
        return MAIN_MENU
    except OverflowError:
        msg.reply_text(messages.SET_INTERVAL_BIG_VALUE_EXCEPTION,
                       reply_markup=kb_main,
                       parse_mode=messages.MARKDOWN)
        return MAIN_MENU
Ejemplo n.º 5
0
def switch_off(bot, update, job_queue, chat_data):
    """Remove the job if the user changed their mind"""

    msg = update.message
    if not mq.get_user_settings(msg.chat_id)[base_config.NOTIFICATIONS]:
        msg.reply_text(messages.ALREADY_OFF_TEXT, reply_markup=kb_main, parse_mode=messages.MARKDOWN)
        return MAIN_MENU
    mq.update_setting(msg.chat_id, setting=base_config.NOTIFICATIONS, value=False)
    chat_data['job'].schedule_removal()
    msg.reply_text(messages.NOTIFICATIONS_OFF_TEXT, parse_mode=messages.MARKDOWN)
Ejemplo n.º 6
0
def switch_on(bot, update, job_queue, chat_data):
    """Add the job to the queue"""

    msg = update.message
    if mq.get_user_settings(msg.chat_id)[base_config.NOTIFICATIONS]:
        msg.reply_text(messages.ALREADY_ON_TEXT, reply_markup=kb_main, parse_mode=messages.MARKDOWN)
        return MAIN_MENU
    mq.update_setting(msg.chat_id, setting=base_config.NOTIFICATIONS, value=True)
    if 'job' in chat_data:
        chat_data['job'].schedule_removal()
    job = job_queue.run_repeating(notify, int(mq.get_user_settings(msg.chat_id)[base_config.INTERVAL]),
                                  context={'chat_id': msg.chat_id})
    chat_data['job'] = job
    msg.reply_text(messages.NOTIFICATIONS_ON_TEXT, parse_mode=messages.MARKDOWN)
Ejemplo n.º 7
0
def set_threshold_dialog(bot, update):
    """
    Begin the dialog with for setting an threshold value
    
    Returns:
        :return: settings menu identifier
    """

    msg = update.message
    threshold = float(msg.text)
    if threshold <= 0:
        msg.reply_text(messages.SET_THRESHOLD_BAD_VALUE_TEXT,
                       parse_mode=messages.MARKDOWN)
    else:
        mq.update_setting(msg.chat_id,
                          setting=base_config.THRESHOLD,
                          value=threshold)
        msg.reply_text(messages.SET_THRESHOLD_SUCC_TEXT.format(threshold),
                       reply_markup=kb_settings,
                       parse_mode=messages.MARKDOWN)
        return SETTINGS_MENU
    return SET_THRESHOLD