Ejemplo n.º 1
0
def send_description(update: Update, context: CallbackContext):
    day = context.user_data['day']
    message = update.message.text
    user = update.message.from_user
    logger.info(
        "User %s %s username:%s: send_description %s",
        user.first_name,
        user.last_name,
        user.username,
        message,
    )

    needed_description_number = int(message.split('c')[-1])  # descN
    context.user_data['description_number'] = needed_description_number
    event = next(
        (ev for ev in context.user_data['event_list']
         if (isinstance(ev, FullEvent) or isinstance(ev, Other))
         and ev.number == needed_description_number),
        None,
    )

    reply_message = context.user_data['localisation'][str(day)] + '\n'
    if context.user_data['lang'] == 'ru':
        reply_message += (event.full_str_ru()) + '\n'
    else:
        reply_message += (event.full_str_en()) + '\n'

    if (context.user_data['type'] == 'sections'
            or context.user_data['type'] == 'time'
            or context.user_data['type'] == 'current'):
        reply_keyboard = keyboards.back_to_begin_keyboard(context)
    elif context.user_data['type'] == 'search':
        reply_keyboard = keyboards.to_begin_keyboard(context)

    update.message.reply_text(
        reply_message,
        parse_mode=telegram.ParseMode.HTML,  # this is needed for bold text
        reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                         one_time_keyboard=True,
                                         resize_keyboard=True),
    )

    if context.user_data['type'] == 'sections':
        return dk.SENDING_DESCRIPTION
    elif context.user_data['type'] == 'time':
        return dk.SENDING_DESCRIPTION_TIME
    elif context.user_data['type'] == 'current':
        return dk.SENDING_DESCRIPTION_CURRENT
    elif context.user_data['type'] == 'search':
        update.message.reply_text(
            context.user_data['localisation']['WHATSEARCH'])
        return dk.SEARCHING
Ejemplo n.º 2
0
def search_program(update: Update, context: CallbackContext):
    user = update.message.from_user
    logger.info("User %s %s username:%s: search_program.", user.first_name,
                user.last_name, user.username)

    reply_keyboard = keyboards.to_begin_keyboard(context)
    update.message.reply_text(
        context.user_data['localisation']['WHATSEARCH'],
        reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                         one_time_keyboard=True,
                                         resize_keyboard=True),
    )
    return dk.SEARCHING
Ejemplo n.º 3
0
def back_to_message_current(update: Update, context: CallbackContext):
    messages_to_send = context.user_data['messages']
    reply_keyboard = keyboards.to_begin_keyboard(context)
    for message in messages_to_send:
        update.message.reply_text(
            message,
            parse_mode=telegram.ParseMode.HTML,  # this is needed for bold text
            reply_markup=ReplyKeyboardMarkup(
                reply_keyboard, one_time_keyboard=True, resize_keyboard=True
            ),
        )

    return dk.SENDING_CURRENT
Ejemplo n.º 4
0
def leave_feedback(update: Update, context: CallbackContext):
    user = update.message.from_user
    logger.info(
        "User %s %s username:%s: leave_feedback", user.first_name, user.last_name, user.username
    )

    reply_keyboard = keyboards.to_begin_keyboard(context)

    update.message.reply_text(
        context.user_data['localisation']['FEEDBACKMESSAGE'],
        reply_markup=ReplyKeyboardMarkup(
            reply_keyboard, one_time_keyboard=True, resize_keyboard=True
        ),
    )

    return dk.FEEDBACK
Ejemplo n.º 5
0
def show_current(update: Update, context: CallbackContext):
    user = update.message.from_user
    logger.info(
        "User %s %s username:%s: show_current", user.first_name, user.last_name, user.username
    )
    current_moment = time.time()
    #current_moment = 1569240000  # 23.09.19 15:00
    day = 23
    current_sections_messages = []
    for event in context.user_data['event_list']:
        if event.ts_begin <= current_moment <= event.ts_end:
            # found current event
            day = event.get_date()
            if context.user_data['lang'] == 'ru':
                event_message = event.str_ru() + '\n'
            else:
                event_message = event.str_en() + '\n'

            if isinstance(event, FullEvent) or isinstance(event, Other):
                event_message += (
                    context.user_data['localisation']['DETAILS']
                    + '/desc'
                    + str(event.number)
                    + '\n'
                )
            current_sections_messages.append(event_message)
    context.user_data['day'] = day

    if not current_sections_messages:
        current_sections_messages.append(context.user_data['localisation']['NOCURRENT'])

    reply_keyboard = keyboards.to_begin_keyboard(context)

    for message in current_sections_messages:
        context.bot.send_message(
            chat_id=update.message.chat_id,
            text=message,
            parse_mode=telegram.ParseMode.HTML,
            reply_markup=ReplyKeyboardMarkup(
                reply_keyboard, one_time_keyboard=True, resize_keyboard=True
            ),
        )

    context.user_data['type'] = 'current'
    context.user_data['messages'] = current_sections_messages

    return dk.SENDING_CURRENT
Ejemplo n.º 6
0
def search_more(update: Update, context: CallbackContext):
    reply_messages = context.user_data.get('reply_messages', None)
    reply_keyboard = keyboards.to_begin_keyboard(context)
    if not reply_messages:
        reply_messages = [context.user_data['localisation']['NOTFOUND']]
    else:
        if len(reply_messages) > 5:
            context.user_data['reply_messages'] = reply_messages[5:]
            reply_keyboard = keyboards.more_to_begin_keyboard(context)
            reply_messages = reply_messages[:5]
        else:
            context.user_data['reply_messages'] = []

    for reply_message in reply_messages:
        update.message.reply_text(
            reply_message,
            parse_mode=telegram.ParseMode.HTML,  # this is needed for bold text
            reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                             one_time_keyboard=True,
                                             resize_keyboard=True),
        )
Ejemplo n.º 7
0
def back_to_marked(update: Update, context: CallbackContext):
    marked_list = context.user_data['marked_list']
    if find_time_intersections(context):
        reply_keyboard = keyboards.conflicts_to_begin_keyboard(context)
    else:
        reply_keyboard = keyboards.to_begin_keyboard(context)
    for marked_talk in marked_list:
        if context.user_data['lang'] == 'ru':
            reply_message = marked_talk.str_ru()
        else:
            reply_message = marked_talk.str_en()

        update.message.reply_text(
            reply_message,
            parse_mode=telegram.ParseMode.HTML,  # this is needed for bold text
            reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                             one_time_keyboard=True,
                                             resize_keyboard=True),
        )

    return dk.MARKED
Ejemplo n.º 8
0
def show_marked_talks(update: Update, context: CallbackContext):
    user = update.message.from_user
    logger.info("User %s %s username:%s: show_marked_talks", user.first_name,
                user.last_name, user.username)
    context.user_data['type'] = 'marked'

    marked_list = context.user_data['marked_list']
    if find_time_intersections(context):
        reply_keyboard = keyboards.conflicts_to_begin_keyboard(context)
    else:
        reply_keyboard = keyboards.to_begin_keyboard(context)

    for marked_talk in marked_list:
        if context.user_data['lang'] == 'ru':
            reply_message = marked_talk.str_ru()
        else:
            reply_message = marked_talk.str_en()

        update.message.reply_text(
            reply_message,
            parse_mode=telegram.ParseMode.HTML,  # this is needed for bold text
            reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                             one_time_keyboard=True,
                                             resize_keyboard=True),
        )

    if not marked_list:
        update.message.reply_text(
            context.user_data['localisation']['NOMARKED'],
            parse_mode=telegram.ParseMode.HTML,  # this is needed for bold text
            reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                             one_time_keyboard=True,
                                             resize_keyboard=True),
        )

    return dk.MARKED
Ejemplo n.º 9
0
def perform_search(update: Update, context: CallbackContext):
    message = update.message.text.lower()
    user = update.message.from_user
    logger.info(
        "User %s %s username:%s: search: %s",
        user.first_name,
        user.last_name,
        user.username,
        message,
    )

    # add transliteration
    message_variants = {message}
    try:
        alt_message = translit(message, 'ru')
        message_variants.add(alt_message)
    except:
        pass

    try:
        alt_message = translit(message, 'ru', reversed=True)
        message_variants.add(alt_message)
    except:
        pass

    logger.info('Searching for {}'.format(message_variants))
    context.user_data['reply_messages'] = []
    reply_keyboard = keyboards.to_begin_keyboard(context)
    reply_messages = []
    for event in context.user_data['event_list']:
        if isinstance(event, FullEvent):
            if any(message in event.full_str_ru().lower()
                   for message in message_variants):
                if any(message in event.str_ru().lower()
                       for message in message_variants):
                    reply = (context.user_data['localisation'][str(
                        event.get_date())] + '\n' + event.str_ru() + '\n')
                    reply += (context.user_data['localisation']['DETAILS'] +
                              '/desc' + str(event.number) + '\n')
                    context.user_data['day'] = event.get_date()
                    reply_messages.append(reply)
                else:
                    for talk in event.talks_list:
                        for part in talk.__dict__.values():
                            if any(message in str(part).lower()
                                   for message in message_variants):
                                number = event.talks_list.index(talk)
                                reply = (context.user_data['localisation'][str(
                                    event.get_date())] + '\n' +
                                         event.one_talk_str_ru(number))
                                reply_messages.append(reply)
                                break  # needed to give one talk only once
            elif any(message in event.full_str_en().lower()
                     for message in message_variants):
                if any(message in event.str_en().lower()
                       for message in message_variants):
                    reply = (context.user_data['localisation'][str(
                        event.get_date())] + '\n' + event.str_en() + '\n')
                    reply += (context.user_data['localisation']['DETAILS'] +
                              '/desc' + str(event.number) + '\n')
                    context.user_data['day'] = event.get_date()
                    reply_messages.append(reply)
                else:
                    for talk in event.talks_list:
                        for part in talk.__dict__.values():
                            if any(message in str(part).lower()
                                   for message in message_variants):
                                number = event.talks_list.index(talk)
                                reply = (context.user_data['localisation'][str(
                                    event.get_date())] + '\n' +
                                         event.one_talk_str_en(number))
                                reply_messages.append(reply)
                                break
        else:
            if any(message in event.str_ru().lower()
                   for message in message_variants):
                reply = (
                    context.user_data['localisation'][str(event.get_date())] +
                    '\n' + event.str_ru())
                reply_messages.append(reply)
            elif any(message in event.str_en().lower()
                     for message in message_variants):
                reply = (
                    context.user_data['localisation'][str(event.get_date())] +
                    '\n' + event.str_en())
                reply_messages.append(reply)

    if len(reply_messages) > 5:
        context.user_data['reply_messages'] = reply_messages[5:]
        reply_keyboard = keyboards.more_to_begin_keyboard(context)
        reply_messages = reply_messages[:5]

    if len(reply_messages) == 0:
        reply_messages.append(context.user_data['localisation']['NOTFOUND'])

    reply_messages.append(context.user_data['localisation']['WHATSEARCH'])
    # save messages for mark_and_unmark_talk function
    context.user_data['search_reply_messages'] = reply_messages
    context.user_data['type'] = 'search'
    for reply_message in reply_messages:
        update.message.reply_text(
            reply_message,
            parse_mode=telegram.ParseMode.HTML,  # this is needed for bold text
            reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                             one_time_keyboard=True,
                                             resize_keyboard=True),
        )

    return dk.SEARCHING
Ejemplo n.º 10
0
def mark_and_unmark_talk(update: Update, context: CallbackContext):
    message = update.message.text
    user = update.message.from_user
    logger.info(
        "User %s %s username:%s: mark_talk %s",
        user.first_name,
        user.last_name,
        user.username,
        message,
    )

    mark_added = False
    added_message = None

    needed_number = int(message.split('k')[-1])  # markN
    for event in context.user_data['event_list']:
        if isinstance(event, FullEvent):
            for talk in event.talks_list:
                if needed_number == talk.talk_number:
                    if not talk.is_marked:
                        talk.is_marked = True
                        context.user_data['marked_list'].append(talk)
                        mark_added = True
                    else:
                        talk_to_unmark = next(
                            (talk for talk in context.user_data['marked_list']
                             if talk.talk_number == needed_number),
                            None,
                        )
                        if talk_to_unmark:
                            context.user_data['marked_list'].remove(
                                talk_to_unmark)
                            talk_to_unmark.is_marked = False

                        talk_to_unnotify = next(
                            (talk
                             for talk in context.user_data['notified_list']
                             if talk.talk_number == needed_number),
                            None,
                        )
                        if talk_to_unnotify:
                            context.user_data['notified_list'].remove(
                                talk_to_unnotify)
                            talk_to_unnotify.notified = False

                    dk.update_marks_and_notifications(
                        update.message.chat_id,
                        context.user_data['marked_list'])

    # Sending previous message again, but updated
    if (context.user_data['type'] == 'sections'
            or context.user_data['type'] == 'time'
            or context.user_data['type'] == 'current'):
        day = context.user_data['day']
        needed_description_number = context.user_data['description_number']
        event = next(
            (ev for ev in context.user_data['event_list']
             if (isinstance(ev, FullEvent) or isinstance(ev, Other))
             and ev.number == needed_description_number),
            None,
        )

        reply_message = context.user_data['localisation'][str(day)] + '\n'
        if context.user_data['lang'] == 'ru':
            reply_message += (event.full_str_ru()) + '\n'
        else:
            reply_message += (event.full_str_en()) + '\n'
        reply_keyboard = keyboards.back_to_begin_keyboard(context)

        if mark_added:
            added_message = context.user_data['localisation']['MARKADDED']

        update.message.reply_text(
            reply_message,
            parse_mode=telegram.ParseMode.HTML,  # this is needed for bold text
            reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                             one_time_keyboard=True,
                                             resize_keyboard=True),
        )
        if added_message:
            update.message.reply_text(
                added_message,
                parse_mode=telegram.ParseMode.
                HTML,  # this is needed for bold text
                reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                                 one_time_keyboard=True,
                                                 resize_keyboard=True),
            )

        if context.user_data['type'] == 'sections':
            return dk.SENDING_DESCRIPTION
        elif context.user_data['type'] == 'time':
            return dk.SENDING_DESCRIPTION_TIME
        elif context.user_data['type'] == 'current':
            return dk.SENDING_DESCRIPTION_CURRENT

    elif context.user_data['type'] == 'search':
        reply_messages = context.user_data['search_reply_messages']
        # update the needed message
        for i in range(len(reply_messages)):
            if "mark" + str(needed_number) in reply_messages[i]:
                needed_talk = next(
                    (talk for event in context.user_data['event_list']
                     if isinstance(event, FullEvent)
                     for talk in event.talks_list
                     if talk.talk_number == needed_number),
                    None,
                )
                if context.user_data['lang'] == 'ru':
                    reply_messages[i] = needed_talk.str_ru(
                    ) if needed_talk else ""

                else:
                    reply_messages[i] = context.user_data['marked_list'][
                        -1].str_en()

        if mark_added:
            reply_messages.append(
                context.user_data['localisation']['MARKADDED'])

        reply_keyboard = keyboards.to_begin_keyboard(context)

        for reply_message in reply_messages:
            update.message.reply_text(
                reply_message,
                parse_mode=telegram.ParseMode.
                HTML,  # this is needed for bold text
                reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                                 one_time_keyboard=True,
                                                 resize_keyboard=True),
            )

        return dk.SEARCHING

    elif context.user_data['type'] == 'marked':
        marked_list = context.user_data['marked_list']
        if find_time_intersections(context):
            reply_keyboard = keyboards.conflicts_to_begin_keyboard(context)
        else:
            reply_keyboard = keyboards.to_begin_keyboard(context)
        for marked_talk in marked_list:
            if context.user_data['lang'] == 'ru':
                reply_message = marked_talk.str_ru()
            else:
                reply_message = marked_talk.str_en()
            update.message.reply_text(
                reply_message,
                parse_mode=telegram.ParseMode.
                HTML,  # this is needed for bold text
                reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                                 one_time_keyboard=True,
                                                 resize_keyboard=True),
            )

        if mark_added:
            added_message = context.user_data['localisation']['MARKADDED']
            update.message.reply_text(
                added_message,
                parse_mode=telegram.ParseMode.
                HTML,  # this is needed for bold text
                reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                                 one_time_keyboard=True,
                                                 resize_keyboard=True),
            )

        return dk.MARKED