Ejemplo n.º 1
0
def start(update: Update, context: CallbackContext):
    user = update.message.from_user.id
    bot = context.bot
    if utils.select_user_attribute(user, 'tg_id'):
        if not utils.select_user_attribute(user, 'state'):
            utils.assign_state(user, mg.States.START.value)
        state = utils.select_user_attribute(user, 'state')
        logger.info("User {} in state {}".format(user, int(state)))
        if update.message.text == new_comment['RU']:
            enter_new_comment(update, context)
            return mg.States.NO_ATTACH
        elif update.message.text == predict_menu['RU']:
            select_model(update, context)
            return mg.States.EDIT_MODERATOR
        elif update.message.text == reset_menu['RU']:
            reset_comments(update, context)
            return mg.States.MENU
        else:
            reply_markup = ReplyKeyboardMarkup([[back2menu['RU']]],
                                               one_time_keyboard=True,
                                               resize_keyboard=True)
            bot.send_message(chat_id=user,
                             text=state_menu['RU'],
                             reply_markup=reply_markup)
            return mg.States.MENU
    else:
        bot.send_message(chat_id=user, text=access_denied['RU'])
        return mg.States.START
Ejemplo n.º 2
0
def comment_entered(update: Update, context: CallbackContext):
    user = update.message.from_user
    bot = context.bot
    text = update.message.text
    author_id = utils.select_user_attribute(user.id, '_id')
    logger.info("Comment added {}".format(user.id))
    utils.assign_state(user.id, mg.States.ADD_COMMENT.value)
    if mg.select_one('comments', 'comment', **{'author_id': author_id}):
        mg.add_to_array('comments', '_id', author_id, **{'comment': text})
    else:
        mg.insert('comments', **{'author_id': author_id, 'comment': text})
    bot.send_message(chat_id=user.id, text=comment_added['RU'])
    return mg.States.MENU
Ejemplo n.º 3
0
def menu(update: Update, context: CallbackContext):
    user = update.message.from_user
    bot = context.bot
    logger.info("Menu by user {}.".format(user.id))
    try:
        tg_id = utils.select_user_attribute(user.id, 'tg_id')
        if tg_id:
            keyboard = [[new_comment['RU']], [predict_menu['RU']],
                        [reset_menu['RU']]]
            reply_markup = ReplyKeyboardMarkup(keyboard,
                                               one_time_keyboard=True,
                                               resize_keyboard=True)
            bot.send_message(chat_id=user.id,
                             text=menu_text['RU'],
                             reply_markup=reply_markup)
            utils.assign_state(user.id, mg.States.MENU.value)
            return mg.States.SET_STATE
        else:
            bot.send_message(user.id, text=access_denied['RU'])
    except AttributeError:
        bot.send_message(chat_id=user.id, text=access_denied['RU'])
        return mg.States.START
Ejemplo n.º 4
0
def make_prediction(update: Update, context: CallbackContext):
    user = update.message.from_user
    bot = context.bot
    logger.info("Make prediction by user {}.".format(user.id))
    utils.assign_state(user.id, mg.States.MAKE_PREDICT.value)
    author_id = utils.select_user_attribute(user.id, '_id')
    selected_user = '******'
    selected_model = selected_user + '_model.pkl'
    with open(selected_model, 'rb') as fid:
        model = pickle.load(fid)
    user_comments = mg.select_many('comments', 'comment',
                                   **{'author_id': ObjectId(author_id)})
    user_comments = np.array(user_comments).reshape(len(user_comments), )
    result_transformer = selected_user + '_transformer.pkl'
    with open(result_transformer, 'rb') as bt:
        transformer = pickle.load(bt)
    result_encoder = selected_user + '_labelencoder.pkl'
    with open(result_encoder, 'rb') as le:
        encoder = pickle.load(le)
    model_result = encoder.inverse_transform(
        model.predict(transformer.transform(user_comments)))
    model_result = max(set(list(model_result)), key=list(model_result).count)
    model_result = 'Скорее всего, Вы - {RESULT}'.format(RESULT=model_result)
    mg.insert(
        'predictions', **{
            'author_id': author_id,
            'model': selected_model,
            'model_result': model_result
        })
    reply_markup = ReplyKeyboardMarkup([[back2menu['RU']]],
                                       one_time_keyboard=True,
                                       resize_keyboard=True)
    bot.send_message(chat_id=user.id,
                     text=model_result,
                     reply_markup=reply_markup)
    return mg.States.MENU
Ejemplo n.º 5
0
def select_model(update: Update, context: CallbackContext):
    user = update.message.from_user
    bot = context.bot
    logger.info("Select model by user {}.".format(user.id))
    author_id = utils.select_user_attribute(user.id, '_id')
    comments = mg.select_many('comments', 'comment',
                              **{'author_id': ObjectId(author_id)})
    if any(comments):
        reply_markup = ReplyKeyboardMarkup(
            [[model_1['RU'], model_2['RU']], [model_3['RU'], model_4['RU']]],
            one_time_keyboard=True,
            resize_keyboard=True)
        bot.send_message(chat_id=user.id,
                         text=choose_model['RU'],
                         reply_markup=reply_markup)
        return mg.States.MAKE_PREDICT
    else:
        reply_markup = ReplyKeyboardMarkup([[back2menu['RU']]],
                                           one_time_keyboard=True,
                                           resize_keyboard=True)
        bot.send_message(chat_id=user.id,
                         text=no_comments['RU'],
                         reply_markup=reply_markup)
        return mg.States.MENU