def start(update, context):
    """Start the data collection process with user."""
    cid = update.message.chat_id
    user = ClassDB.get_user(cid)
    user.next_question = 0
    ClassDB.set_user(user)
    return collect(update, context)
def collect(update, context):
    """Store the response and ask the next question.

    First, if there was a previous question, the result is stored in
    user-class. Then if there is a next question it will be asked.
    otherwise, will return to MENU.
    """
    try:
        cid = update.message.chat_id
        user = ClassDB.get_user(cid)

        if user.next_question > 0:
            user.store_prev_question_response(update.message.text)

        question = user.get_next_question()

    except:
        cid = update.message.chat_id
        user = ClassDB.get_user(cid)
        text = 'You are done! Congrats for another successful ' \
               'day!'
        markup = General.markup_menu
        update.message.reply_text(text, reply_markup=markup)
        return General.MENU
    else:
        reply_text = question.text
        markup = question.get_markup()
        update.message.reply_text(reply_text, reply_markup=markup)
        return General.MINING
def set_key(update, context):
    """Set the received msg as key, print current keyboard + cmds."""
    row = context.chat_data["row"]
    col = context.chat_data["col"]

    cid = update.message.chat_id
    user = ClassDB.get_user(cid)
    user.temp_question.keyboard[row].insert(col, update.message.text)

    next_col(update, context)

    reply_text = user.temp_question.sprint_keyboard() + '\n' + \
                 ClassDB.sprint_cmd_list(create_keyb_cmd_list)
    update.message.reply_text(reply_text)
def question_text_defined(update, context):
    try:
        cid = update.message.chat_id
        user = ClassDB.get_user(cid)
        user.temp_question = ClassDB.Question(update.message.text,
                                              user.question_id)

        text = 'Choose a Keyboard or create a new one!'
        markup = General.markup_keyboard_overview
        update.message.reply_text(text, reply_markup=markup)

        return General.ADD2

    except Exception as E:
        print(E)
def std_keyb_used(update, context):
    try:
        cid = update.message.chat_id
        user = ClassDB.get_user(cid)
        user.temp_question.is_custom = False

        return done(update, context)

    except Exception as E:
        print(E)
def sprint_keyboard(update, context):
    """Returns multi-line string of current keyboard."""
    cid = update.message.chat_id
    user = ClassDB.get_user(cid)
    new_question = user.temp_question
    ret = ''
    for row in new_question.keyboard:
        for key in row:
            ret += '[' + key + '] '
        ret += '\n'
    return ret
def use_custom_keyb(update, context):
    try:
        cid = update.message.chat_id
        user = ClassDB.get_user(cid)
        user.temp_question.is_custom = True

        context.chat_data["row"] = 0
        context.chat_data["col"] = 0

        reply_text = 'You are something else! So let me ' \
                     'explain.\nAdd a ' \
                     'button by just sending the text it should say. ' \
                     'Then you have the commands below to control ' \
                     'the process.\n'
        reply_text += ClassDB.sprint_cmd_list(create_keyb_cmd_list)
        update.message.reply_text(reply_text)

        return General.ADD3

    except Exception as E:
        print(E)
def done(update, context):
    try:
        cid = update.message.chat_id
        user = ClassDB.get_user(cid)
        user.add_question(user.temp_question)
        text = "Great, I like it!"
        markup = General.markup_menu
        update.message.reply_text(text, reply_markup=markup)
        return General.MENU

    except Exception as E:
        print(E)
def next_row(update, context):
    """Increase the row, append one if necessary."""
    cid = update.message.chat_id
    user = ClassDB.get_user(cid)

    row = context.chat_data["row"] + 1
    context.chat_data["col"] = 0

    # append row when index exceeded
    if row == len(user.temp_question.keyboard):
        user.temp_question.keyboard.append([])

    context.chat_data["row"] = row
Пример #10
0
def time_valid(update, context):
    try:
        cid = update.message.chat_id
        user = ClassDB.get_user(cid)

        utc_time = get_time(update.message.text)
        user.set_notify_time(utc_time)

        user.timer_job = context.job_queue.run_daily(callback=send_reminder,
                                                     time=utc_time,
                                                     context=cid)

        text = "time valid! So I will text you everyday at " + \
               str(utc_time) + " (UTC)"
        update.message.reply_text(text)
        return General.MENU

    except Exception as E:
        print(E)
Пример #11
0
def start_conversation(update, context):
    """Start the conversation for the first time with the bot."""
    try:
        cid = update.message.chat_id

        # create user in database if not existing
        if cid not in ClassDB.actual_users:
            # store the user obj in users
            u = ClassDB.User(cid)
            ClassDB.actual_users[cid] = u
            ClassDB.actual_db.add_user(u)

        reply_text = 'Hello my friend! here is the menu!\n\n'
        # reply_text += ClassDB.sprint_cmd_list(General.menu_cmd_list)
        markup = General.markup_menu
        update.message.reply_text(reply_text, reply_markup=markup)

        return General.MENU

    except Exception as E:
        print(E)
Пример #12
0
def start(update, context):
    """Starting point of the timer conversation."""
    try:
        cid = update.message.chat_id
        user = ClassDB.get_user(cid)

        text = "Ready to set a timer eh? lets do it.\nEither send " \
               "the time when you want to be notified to set the " \
               "timer or press\n\n" + '/' + General.cmd_stop_timer + \
               "\n\nto disable it.\n\nSend me the time in HH:MM so " \
               "that I can understand.\n\nlike this: 04:20\n\nOh " \
               "and before I forget. I only know the UTC timezone, " \
               "because everything else makes my head hurt. So here " \
               "a little help for you to deal with this " \
               "bullshit:\n\nSummer: AUT = 14:00; UTC = " \
               "12:00\nWinter: AUT = 13:00; UTC = 12:00."
        update.message.reply_text(text)

        return General.TIMER1

    except Exception as E:
        print(E)
Пример #13
0
def stop(update, context):
    """Stops the timer_job of current user if existing."""
    try:
        cid = update.message.chat_id
        user = ClassDB.get_user(cid)

        if user.timer_job is not None:
            user.timer_job.schedule_removal()
            text = "Ok. I removed the timer. You have to create one " \
                   "again, otherwise, you will forget it all the " \
                   "time!"
        else:
            text = "Wow. You funny human dont even have a timer. " \
                   "Sometimes I am laughing at how bad you are at " \
                   "remembering stuff."

        markup = General.markup_menu
        update.message.reply_text(text, reply_markup=markup)

        return General.MENU

    except Exception as E:
        print(E)
def main():
    updater = Updater(credentials.bot_token, use_context=True)
    dispatcher = updater.dispatcher
    job_queue = updater.job_queue
    job_queue.set_dispatcher(dispatcher)

    ClassDB.init(credentials.connection_string, credentials.database_name,
                 credentials.collection_name)

    conversation_handler = ConversationHandler(

        # A list of Handler objects that can trigger
        # the start of the conversation.
        # Type:	List[telegram.ext.Handler]
        entry_points=[
            CommandHandler(General.cmd_start, basic.start_conversation),
            MessageHandler(Filters.all, basic.default)
        ],

        # A dict that defines the different states of conversation
        # a user can be in and one or more associated Handler
        # objects that should be used in that state.
        # Type:	Dict[object, List[telegram.ext.Handler]]
        states={
            General.MENU: [
                CommandHandler(General.cmd_mining, mining.start),
                CommandHandler(General.cmd_add, add.start),
                CommandHandler(General.cmd_edit, basic.default),
                CommandHandler(General.cmd_timer, timer.start),
                CommandHandler(General.delay5, timer.delay5),
                CommandHandler(General.delay60, timer.delay60),
                CommandHandler(General.delay180, timer.delay180),
                CommandHandler(General.delay_today, timer.delay_today),
                MessageHandler(Filters.all, basic.use_custom_keyb),
            ],
            General.MINING: [MessageHandler(Filters.text, mining.collect)],
            General.ADD1:
            [MessageHandler(Filters.text, add.question_text_defined)],
            General.ADD2: [
                MessageHandler(Filters.text(General.normal),
                               add.std_keyb_used),
                MessageHandler(Filters.text(General.new_custom),
                               add.use_custom_keyb),
            ],
            General.ADD3: [
                CommandHandler(General.cmd_next_row, add.next_row),
                CommandHandler(General.cmd_prev_col, add.prev_col),
                CommandHandler(General.cmd_prev_row, add.prev_row),
                CommandHandler(General.cmd_keyboard_finished, add.done),
                MessageHandler(Filters.text, add.set_key),
            ],
            General.TIMER1: [
                CommandHandler(General.cmd_stop_timer, timer.stop),
                MessageHandler(
                    Filters.regex('^([0-1]?[0-9]|2['
                                  '0-3]):[0-5]['
                                  '0-9]$'), timer.time_valid),
                MessageHandler(Filters.all, timer.time_invalid)
            ],
        },

        # A list of handlers that might be used if the user is in a
        # conversation, but every handler for their current
        # state returned False on check_update.
        # Type:	List[telegram.ext.Handler]
        fallbacks=[CommandHandler('cancel', basic.fallback)])
    dispatcher.add_handler(conversation_handler)
    print("start polling...")
    updater.start_polling()
    updater.idle()
Пример #15
0
def delay(update, context, seconds):
    cid = update.message.chat_id
    user = ClassDB.get_user(cid)
    context.job_queue.run_once(send_reminder, seconds, context=cid)