def check(bot, update): user = update.message.from_user logger.info("{} отправил логин.".format(user.id)) sh.set_state(sh.db_name, user.id, sh.States.CHECK.value) uid = update.message.text.lower() auth = db.SQLighter('db.sqlite') all_users, all_bosses = [], [] for i in range(len(auth.select_no_cond('user', 'user_boss'))): all_users.extend([auth.select_no_cond('user', 'user_boss')[i][0]]) for i in range(len(auth.select_no_cond('boss', 'user_boss'))): all_bosses.extend([auth.select_no_cond('boss', 'user_boss')[i][0]]) if auth.select_person(uid): if uid in all_users and uid in all_bosses: logger.info("Новый uid сотрудника-руководителя") auth.update_cond('user_boss', 'user_id', user.id, 'user', uid) auth.update_cond('user_boss', 'boss_id', user.id, 'boss', uid) elif uid in all_users: logger.info("Новый uid сотрудника") auth.update_cond('user_boss', 'user_id', user.id, 'user', uid) elif uid in all_bosses: logger.info("Новый uid руководителя") auth.update_cond('user_boss', 'boss_id', user.id, 'boss', uid) else: pass bot.send_message(chat_id=update.message.chat_id, text=login_check_suc['RU']) return sh.States.MENU else: bot.send_message(chat_id=update.message.chat_id, text=login_check_fail['RU']) return sh.States.CHECK
def answer_mod(bot, update): user = update.message.from_user logger.info("Руководитель {} отправил комментарии модератору.".format(user.id)) sh.set_state(sh.db_name, user.id, sh.States.ANSWER_MOD.value) post_time = calendar.timegm(time.gmtime()) auth = db.SQLighter('db.sqlite') number_answers = len(auth.check_news(boss_id=user.id)) answer_text = auth.check_news(user.id)[-1 * number_answers] checked_user = auth.select_cond('user_id', 'news_base', 'news_text', answer_text[0])[0][0] auth.update_cond('news_base', 'answer', answer_text[0], 'news_text', answer_text[0]) auth.update_cond('news_base', 'answer_time', post_time, 'news_text', answer_text[0]) auth.update_cond('news_base', 'status', 1, 'news_text', answer_text[0]) bot.send_message(chat_id=user.id, text=answer_sent_mods['RU']) bot.send_message(chat_id=user.id, text='Непроверенных новостей: {}' .format(len(auth.check_news(boss_id=user.id)))) bot.send_message(chat_id=user.id, text=back2menu['RU']) bot.send_message(chat_id=checked_user, text=answer_sent_mods['RU']) if auth.select_cond('file_id', 'news_base', 'news_text', answer_text[0])[0][0]: photo = str(auth.select_cond('file_id', 'news_base', 'news_text', answer_text[0])[0][0]) bot.send_photo(chat_id=channel_name, photo=photo) else: pass auth.update_cond('news_base', 'news_text', update.message.text, 'news_text', answer_text[0]) bot.send_message(chat_id=channel_name, text=update.message.text) return sh.States.MENU
def answer_where(bot, update): user = update.message.from_user logger.info("Руководитель {} выбирает, кому отправить комментарии.".format(user.id)) sh.set_state(sh.db_name, user.id, sh.States.WHERE.value) if update.message.text == 'Модератор': bot.send_message(chat_id=user.id, text=write_mod['RU']) return sh.States.ANSWER_MOD else: bot.send_message(chat_id=user.id, text=write_us['RU']) return sh.States.ANSWER_USER
def send_news(bot, update): user = update.message.from_user auth = db.SQLighter('db.sqlite') post_time = calendar.timegm(time.gmtime()) boss_uid = auth.select_cond('boss_id', 'user_boss', 'user_id', user.id)[0][0] logger.info("%s отправил текст новости", user.id) sh.set_state(sh.db_name, user.id, sh.States.SENT.value) auth.update_cond('news_base', 'time', post_time, 'id', unique_token) auth.update_cond('news_base', 'news_text', update.message.text, 'id', unique_token) bot.send_message(chat_id=boss_uid, text=new_news['RU']) bot.send_message(chat_id=user.id, text=news_acq['RU']) bot.send_message(chat_id=user.id, text=news_sent['RU']) bot.send_message(chat_id=user.id, text=back2menu['RU']) return sh.States.MENU
def no_attachment(bot, update): user = update.message.from_user logger.info("{} начал писать новость без приложения.".format(user.id)) sh.set_state(sh.db_name, user.id, sh.States.NO_ATTACH.value) auth = db.SQLighter('db.sqlite') if auth.select_cond('boss_id', 'user_boss', 'user_id', user.id)[0][0]: # есть ли в базе руководитель юзера boss_uid = auth.select_cond('boss_id', 'user_boss', 'user_id', user.id)[0][0] global unique_token unique_token = update.message.message_id bot.send_message(chat_id=update.message.chat_id, text=news_req['RU']) auth.insert_news_start(unique_id=unique_token, status=0, user_id=user.id, boss_id=boss_uid) else: bot.send_message(chat_id=update.message.chat_id, text=boss_not_found['RU']) return sh.States.SENT
def set_state(bot, update): user = update.message.from_user sh.set_state(sh.db_name, user.id, sh.States.SET_STATE.value) if update.message.text == login_menu['RU']: login(bot, update) return sh.States.CHECK elif update.message.text == attach_menu['RU']: news(bot, update) return sh.States.ATTACH elif update.message.text == answer_menu['RU']: answer(bot, update) return sh.States.RESULT elif update.message.text == news_menu['RU']: no_attachment(bot, update) return sh.States.SENT else: sh.set_state(sh.db_name, user.id, sh.States.MENU.value) return sh.States.MENU
def answer_user(bot, update): user = update.message.from_user logger.info("Руководитель {} отправил комментарии сотруднику.".format(user.id)) sh.set_state(sh.db_name, user.id, sh.States.ANSWER_USER.value) post_time = calendar.timegm(time.gmtime()) auth = db.SQLighter('db.sqlite') number_answers = len(auth.check_news(boss_id=user.id)) answer_text = auth.check_news(user.id)[-1 * number_answers] checked_user = auth.select_cond('user_id', 'news_base', 'news_text', answer_text[0])[0][0] auth.update_cond('news_base', 'answer', update.message.text, 'news_text', answer_text[0]) auth.update_cond('news_base', 'answer_time', post_time, 'news_text', answer_text[0]) bot.send_message(chat_id=checked_user, text=answer_acq_user['RU']) bot.send_message(chat_id=checked_user, text=update.message.text) bot.send_message(chat_id=user.id, text=answer_sent_user['RU']) bot.send_message(chat_id=user.id, text='Непроверенных новостей: {}' .format(len(auth.check_news(boss_id=user.id)))) bot.send_message(chat_id=user.id, text=back2menu['RU']) return sh.States.MENU
def menu(bot, update): keyboard = [[login_menu['RU'], news_menu['RU']], [answer_menu['RU'], attach_menu['RU']]] reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True, resize_keyboard=True) user = update.message.from_user logger.info("Меню вызвано пользователем {}.".format(user.id)) sh.set_state(sh.db_name, user.id, sh.States.MENU.value) auth = db.SQLighter('db.sqlite') if auth.select_cond('user_id', 'user_boss', 'user_id', user.id): update.message.reply_text(menu_text['RU'], reply_markup=reply_markup) return sh.States.SET_STATE else: bot.send_message(chat_id=user.id, text=user_init_auth['RU']) login(bot, update) return sh.States.CHECK
def answer_result(bot, update): user = update.message.from_user auth = db.SQLighter('db.sqlite') post_time = calendar.timegm(time.gmtime()) number_answers = len(auth.check_news(boss_id=user.id)) if number_answers > 0: answer_text = auth.check_news(user.id)[-1 * number_answers] checked_user = auth.select_cond('user_id', 'news_base', 'news_text', answer_text[0])[0][0] logger.info("Ответ руководителя %s: %s", user.id, update.message.text) sh.set_state(sh.db_name, user.id, sh.States.RESULT.value) if update.message.text == 'ОК': logger.info("2 шаг согласования - OK") auth.update_cond('news_base', 'status', 1, 'news_text', answer_text[0]) auth.update_cond('news_base', 'answer_time', post_time, 'news_text', answer_text[0]) bot.send_message(chat_id=user.id, text=answer_sent_mods['RU']) bot.send_message(chat_id=user.id, text='Непроверенных новостей: {}' .format(len(auth.check_news(boss_id=user.id)))) bot.send_message(chat_id=user.id, text=back2menu['RU']) bot.send_message(chat_id=checked_user, text=answer_sent_mods['RU']) if auth.select_cond('file_id', 'news_base', 'news_text', answer_text[0])[0][0]: photo = str(auth.select_cond('file_id', 'news_base', 'news_text', answer_text[0])[0][0]) bot.send_photo(chat_id=channel_name, photo=photo) else: pass bot.send_message(chat_id=channel_name, text=answer_text[0]) return sh.States.MENU else: logger.info("2 шаг согласования - else") keyboard = [['Модератор', 'Сотрудник']] reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True, resize_keyboard=True) user = update.message.from_user logger.info("Меню модератора вызвано пользователем {}.".format(user.id)) bot.send_message(chat_id=user.id, text=press_mod['RU'], reply_markup=reply_markup) return sh.States.WHERE else: bot.send_message(chat_id=user.id, text=answer_neg['RU']) return sh.States.MENU
def answer(bot, update): user = update.message.from_user logger.info("Руководитель проверил обновления %s", user.id) # руководитель заходит в согласование sh.set_state(sh.db_name, user.id, sh.States.ANSWER.value) auth = db.SQLighter('db.sqlite') user_uid = auth.select_cond('user_id', 'user_boss', 'boss_id', user.id) if user_uid: # является ли пользователь руководителем number_answers = len(auth.check_news(boss_id=user.id)) # чтобы начать проверять с самой ранней новости if number_answers > 0: # проверка по пустым новостям answer_text = auth.check_news(user.id)[-1 * number_answers] if answer_text[0]: checked_user = auth.select_cond('user_id', 'news_base', 'news_text', answer_text[0])[0][0] bot.send_message(chat_id=user.id, text='Новость получена от {}' .format(auth.select_cond('user', 'user_boss', 'user_id', checked_user)[0][0])) bot.send_message(chat_id=user.id, text=''.join(answer_text[0])) if auth.select_cond('file_id', 'news_base', 'news_text', answer_text[0])[0][0]: photo = str(auth.select_cond('file_id', 'news_base', 'news_text', answer_text[0])[0][0]) bot.send_photo(chat_id=user.id, photo=photo) else: pass keyboard = [['ОК', 'Не ОК']] reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True, resize_keyboard=True) update.message.reply_text(text=press_ok['RU'], reply_markup=reply_markup) logger.info("1 шаг согласования - проверка") return sh.States.RESULT else: bot.send_message(chat_id=user.id, text=news_null['RU']) logger.info("1 шаг согласования - пустая новость") return sh.States.MENU else: bot.send_message(chat_id=user.id, text=answer_neg['RU']) logger.info("1 шаг согласования - нет новостей") return sh.States.MENU else: bot.send_message(chat_id=user.id, text=answer_rights['RU']) return sh.States.MENU
def send_photo(bot, update): user = update.message.from_user auth = db.SQLighter('db.sqlite') logger.info("%s отправил фотографию", user.id) sh.set_state(sh.db_name, user.id, sh.States.ATTACH.value) photo = str(update.message.photo[-1].file_id) auth.update_cond('news_base', 'file_id', photo, 'id', unique_token) bot.send_message(chat_id=user.id, text=attach_acq['RU']) post_time = calendar.timegm(time.gmtime()) caption = update.message.caption if caption and len(caption) < 200: logger.info("%s отправил текст в caption", user.id) auth.update_cond('news_base', 'time', post_time, 'id', unique_token) auth.update_cond('news_base', 'news_text', str(caption), 'id', unique_token) boss_uid = auth.select_cond('boss_id', 'user_boss', 'user_id', user.id)[0][0] bot.send_message(chat_id=boss_uid, text=new_news['RU']) bot.send_message(chat_id=user.id, text=news_acq['RU']) bot.send_message(chat_id=user.id, text=news_sent['RU']) bot.send_message(chat_id=user.id, text=back2menu['RU']) return sh.States.MENU else: bot.send_message(chat_id=user.id, text=news_req['RU']) return sh.States.SENT
def cancel(bot, update): user = update.message.from_user logger.info("Пользователь {} прервал диалог.".format(user.id)) update.message.reply_text(text=cancel_text['RU'], reply_markup=ReplyKeyboardRemove()) sh.set_state(sh.db_name, user.id, sh.States.START.value) return ConversationHandler.END
def login(bot, update): user = update.message.from_user logger.info("{} пытается авторизоваться.".format(user.id)) sh.set_state(sh.db_name, user.id, sh.States.LOGIN.value) bot.send_message(chat_id=user.id, text=login_req['RU']) return sh.States.CHECK