Exemple #1
0
def remove_group(chat_id, group_name, callback_query):
    """
    Removes the group and sends a confirmation notification

    :param chat_id: Telegram chat id
    :param group_name: group name to be removed
    :param callback_query: callback query
    """
    db_connection = DatabaseConnection()
    if group_name not in db_connection.get_groups_names(chat_id):
        get_bot().send_message(chat_id=chat_id,
                               text=tr('removegroup_not_found', chat_id))
    else:
        stations = db_connection.get_group(chat_id, group_name)['stations']
        db_connection.delete_group(chat_id=chat_id, name=group_name)
        message = tr('removegroup_removed', chat_id).format(
            group_name, ', '.join(str(station) for station in stations))
        get_bot().answer_callback_query(callback_query.id, text=message)
        get_bot().edit_message_text(
            chat_id=chat_id,
            text=message,
            message_id=callback_query.message.message_id)
    db_connection.close()
Exemple #2
0
def newgroup_command(chat_id, text):
    """
    Manages the workflow to create a new group.

    :param chat_id: Telegram chat id
    :param text: group command
    """
    group_status = get_group_status(chat_id)
    from bicingbot.commands import send_stations_status, COMMANDS
    db_connection = DatabaseConnection()
    if group_status == GROUP_STATUS_INIT:
        logger.info('COMMAND {}: chat_id={}'.format(text, chat_id))
        if len(db_connection.get_groups_names(chat_id)) < MAX_NUMBER_GROUPS:
            get_bot().send_message(chat_id=chat_id,
                                   text=tr('newgroup_name', chat_id))
            set_group_status(chat_id, GROUP_STATUS_NEWGROUP_NAME)
        else:
            get_bot().send_message(chat_id=chat_id,
                                   text=tr('newgroup_number_groups_limit',
                                           chat_id).format(MAX_NUMBER_GROUPS))
            del_group_status(chat_id)
    elif group_status == GROUP_STATUS_NEWGROUP_NAME:
        if text in COMMANDS['end']['alias']:
            get_bot().send_message(chat_id=chat_id,
                                   text=tr('newgroup_not_created', chat_id))
            del_group_status(chat_id)
        elif not is_valid_group_name(text):
            get_bot().send_message(chat_id=chat_id,
                                   text=tr('newgroup_name_format_error',
                                           chat_id))
        else:
            message = tr('newgroup_stations', chat_id)
            if text in db_connection.get_groups_names(chat_id):
                message = tr('newgroup_name_already_existing',
                             chat_id).format(message.lower())
            GROUPS_CACHE[chat_id]['name'] = text
            get_bot().send_message(chat_id=chat_id, text=message)
            set_group_status(chat_id, GROUP_STATUS_NEWGROUP_STATIONS)
    elif group_status == GROUP_STATUS_NEWGROUP_STATIONS:
        if text in COMMANDS['end']['alias']:
            if GROUPS_CACHE[chat_id]['stations']:
                db_connection.delete_group(chat_id=chat_id,
                                           name=GROUPS_CACHE[chat_id]['name'])
                db_connection.create_group(
                    chat_id=chat_id,
                    name=GROUPS_CACHE[chat_id]['name'],
                    stations=GROUPS_CACHE[chat_id]['stations'])
                get_bot().send_message(
                    chat_id=chat_id,
                    text=tr('newgroup_created',
                            chat_id).format(GROUPS_CACHE[chat_id]['name']))
                send_stations_status(chat_id,
                                     GROUPS_CACHE[chat_id]['stations'])
            else:
                if GROUPS_CACHE[chat_id][
                        'name'] in db_connection.get_groups_names(chat_id):
                    get_bot().send_message(
                        chat_id=chat_id,
                        text=tr('newgroup_not_overwrite',
                                chat_id).format(GROUPS_CACHE[chat_id]['name']))
                else:
                    get_bot().send_message(chat_id=chat_id,
                                           text=tr('newgroup_not_created',
                                                   chat_id))
            del_group_status(chat_id)
        elif is_integer(text):
            if len(GROUPS_CACHE[chat_id]['stations']) < MAX_NUMBER_STATIONS:
                GROUPS_CACHE[chat_id]['stations'].append(int(text))
            else:
                get_bot().send_message(
                    chat_id=chat_id,
                    text=tr('newgroup_number_stations_limit',
                            chat_id).format(MAX_NUMBER_STATIONS))
        else:
            get_bot().send_message(chat_id=chat_id,
                                   text=tr('newgroup_unknown_command',
                                           chat_id))

    db_connection.close()