def cmd_start(message): state = db.get_current_state(message.chat.id) if state == config.States.S_ENTER_PHRASE.value: bot.send_message(message.chat.id, "Жду ввода фразы...") elif state == config.States.S_ENTER_LANG.value: bot.send_message(message.chat.id, "Жду ввода языка...") else: bot.send_message(message.chat.id, "Привет! Введи фразу, которую хочешь перевести") db.set_state(message.chat.id, config.States.S_ENTER_PHRASE.value)
def dict_pos(message): pos = db.get_current_state(str(message.chat.id), config.DB_cols.POS.value) pos_input = message.text.lower().strip() # check if input is a correct POS-tag if pos_input in pos: db.del_state(str(message.chat.id), config.DB_cols.LOOKUP.value) word = db.get_current_state(str(message.chat.id), config.DB_cols.WORD.value).strip() # get the definition and audio my_def, audio = dict_func_get_definition(word, pos_input) if my_def: bot.send_audio(message.chat.id, audio, '*' + word + '*', parse_mode='Markdown') bot.send_message(message.chat.id, my_def) db.set_property(str(message.chat.id), config.DB_cols.LOOKUP.value, config.LookUp.START.value) bot.send_message( message.chat.id, "Type /look_up to search for another word.\n" "Type /info or /commands to revise what I can do and see my commands.\n" "Type /reset to start anew.") else: bot.send_message( message.chat.id, "Sorry, I am afraid your word is not in Merriam-Webster's Dictionary" ) bot.send_message( message.chat.id, "Type /look_up to search for another word.\n" "Type /info or /commands to revise what I can do and see my commands.\n" "Type /reset to start anew.") db.set_property(str(message.chat.id), config.DB_cols.LOOKUP.value, config.LookUp.START.value) else: markup = mark_up(pos.split(',')) bot.send_message( message.chat.id, "Seems like there was a typo in your input.\n" "Please, select a part of speech and press the corresponding button.", reply_markup=markup)
def vid_check_exercise(message): db.del_state(str(message.chat.id), config.DB_cols.STATE.value) correct = db.get_current_state(str(message.chat.id), config.DB_cols.EX_KEY.value) if message.text.strip().lower() == correct: bot.send_message(message.chat.id, 'Correct!') counter = db.get_current_state(str(message.chat.id), config.DB_cols.COUNTER.value) + 1 db.set_property(str(message.chat.id), config.DB_cols.COUNTER.value, counter) else: bot.send_message(message.chat.id, 'Sorry, the correct answer is ' + correct) if db.get_current_state(str(message.chat.id), config.DB_cols.EXERCISES.value) == '{}': counter = db.get_current_state(str(message.chat.id), config.DB_cols.COUNTER.value) # if got more than half - well done and get a cat, otherwise - not so well and get a penguin if counter >= 3: bot.send_photo(message.chat.id, config.well_done_img) bot.send_message(message.chat.id, '{} out of 5\nGood job!'.format(counter)) else: bot.send_photo(message.chat.id, config.not_so_well_img) bot.send_message( message.chat.id, '{} out of 5\nNice, but seems like you got a bit tangled up with words.\n' 'I\'m sure next time will be better!'.format(counter)) db.set_property(str(message.chat.id), config.DB_cols.STATE.value, config.States.START.value) bot.send_message(message.chat.id, 'Would you like to /reset?') else: db.set_property(message.chat.id, config.DB_cols.STATE.value, config.States.EXERCISE_CHECKED.value) vid_send_exercise(message)
def vid_send_exercise(message): exercises = json.loads( db.get_current_state(str(message.chat.id), config.DB_cols.EXERCISES.value)) for k, v in exercises.items(): markup = mark_up(v[1]) # send exercises and variants as buttons (added to markup) bot.send_message(message.chat.id, v[0], reply_markup=markup) db.set_property(str(message.chat.id), config.DB_cols.EX_KEY.value, k) exercises.pop(k) break #convert to json before putting to db to avoid problems with quotes when retrieving from db exercises = json.dumps(exercises) db.set_property(str(message.chat.id), config.DB_cols.EXERCISES.value, exercises) db.set_property(str(message.chat.id), config.DB_cols.STATE.value, config.States.EXERCISE_SENT.value)
def sayHi(message): db.set_state(message.chat.id, config.States.S_START.value) state = db.get_current_state(message.chat.id) if state == config.States.S_ENTER_CATEGORY.value: bot.send_message(message.chat.id, "Ожидаю выбора категории:)", reply_markup=keyboard.categoriesListKeyboard()) elif state == config.States.S_ENTER_CITY.value: bot.send_message(message.chat.id, "Ожидаю ваш город:)") elif state == config.States.S_SEND_TITLE.value: bot.send_message(message.chat.id, "Ожидаю ваш заголовок:)") elif state == config.States.S_SEND_DESCRIPTION.value: bot.send_message(message.chat.id, "Ожидаю ваше описание:)") elif state == config.States.S_SEND_PRICE.value: bot.send_message(message.chat.id, "Ожидаю вашу цены на товар:)") elif state == config.States.S_SEND_PHOTO.value: bot.send_message(message.chat.id, "Ожидаю фото товара:)") else: bot.send_message(message.chat.id, "Рад встрече, чего желаете?", reply_markup=keyboard.menuKeyboard()) db.set_state(message.chat.id, config.States.S_START.value)
def vid_transcript_or_exercise(message): mg = message.text.strip().lower() if mg in ['transcript', 'exercise']: db.del_state(str(message.chat.id), config.DB_cols.STATE.value) url = db.get_current_state(str(message.chat.id), config.DB_cols.URL.value) transcript = vid_func_get_transcript(url) if transcript: if mg == 'transcript': bot.send_message(message.chat.id, 'OK, transcript\'s coming.') # in case transcript is longer than message max length # a user can send a link to a very long video if len(transcript) > 2000: sents = nltk.sent_tokenize(transcript) print(sents) part = '' parts = [] for sent in sents: part = part + ' ' + sent.strip() if len(part) < 2000: pass else: parts.append(part) part = '' for p in parts: bot.send_message(message.chat.id, p) bot.send_message( message.chat.id, 'Kind reminder: you can always /look_up a new word.\n' 'Would you like to /reset?') db.set_property(str(message.chat.id), config.DB_cols.STATE.value, config.States.START.value) else: bot.send_message(message.chat.id, transcript) bot.send_message( message.chat.id, 'Kind reminder: you can always /look_up a new word.\n' 'Would you like to /reset?') db.set_property(str(message.chat.id), config.DB_cols.STATE.value, config.States.START.value) elif mg == 'exercise': bot.send_message( message.chat.id, 'OK, exercises are on the way.\n' 'If you don\'t know a word or two -\n' ' just /look_up :-)\n') exercises = vid_func_get_exercise(transcript) if exercises: exercises = json.dumps(exercises) db.set_property(str(message.chat.id), config.DB_cols.EXERCISES.value, exercises) db.set_property(str(message.chat.id), config.DB_cols.STATE.value, config.States.EXERCISE_CHECKED.value) db.set_property(str(message.chat.id), config.DB_cols.COUNTER.value, "0") vid_send_exercise(message) else: bot.send_message( message.chat.id, "I beg your pardon!\n" "For some reason no exercises were found.\n" "Maybe it's the tricks of Don Rigatoni, he is such a mobster.\n" "Anyway, we can always start anew with /reset!") bot.send_photo(message.chat.id, config.rigatoni_img) db.set_property(str(message.chat.id), config.DB_cols.STATE.value, config.States.START.value) else: bot.send_message( message.chat.id, "I beg your pardon!\n" "For some reason no transcript was found.\n" "Maybe it's the tricks of Don Rigatoni, he is such a mobster.\n" "But we can always start anew with /reset!") bot.send_photo(message.chat.id, config.rigatoni_img) db.set_property(str(message.chat.id), config.DB_cols.STATE.value, config.States.START.value) else: markup = mark_up(['exercise', 'transcript']) bot.send_message(message.chat.id, "Seems like there was a typo in your input.\n" "Please, press \"exercise\" to train your vocabulary" "or \"transcript\" to get the transcript.", reply_markup=markup)
if subsense[0] == 'sense': my_def = my_def + u"\n\U00002022 " + clean_dict_entry( subsense[1]['dt'][0][1].strip()) else: s = get_sense(subsense, 'sense') if s != list(): my_def = my_def + u"\n\U00002022 " + clean_dict_entry( s[1]['dt'][0][1].strip()) if my_def != '': return (my_def, audio) else: return False @bot.message_handler(func=lambda message: ( db.get_current_state(str(message.chat.id), config.DB_cols.LOOKUP.value) == config.LookUp.LOOK_UP.value) and message.text.strip().lower() != '/look_up' ) def dict_enter_word(message): db.del_state(str(message.chat.id), config.DB_cols.LOOKUP.value) # get available POS-tags from the dictionary and give user the choice pos = dict_func_get_pos(message.text.lower().strip()) if pos: # make everything a list as for only POS you get strings pos = list(pos) markup = mark_up(pos) bot.send_message(message.chat.id, "Please select the part of speech:\n", reply_markup=markup) db.set_property(str(message.chat.id), config.DB_cols.WORD.value, message.text.lower().strip())
@bot.message_handler(commands=["start"]) def cmd_start(message): bot.send_message(message.chat.id, config.message_library["start_message"]) db.set_state(message.chat.id, config.States.S_PERSONAL_DATA_REQUEST.value) user_personal_data_permission(message) @bot.message_handler(commands=["reset"]) def cmd_reset(message): bot.send_message(message.chat.id, config.message_library["reset_message"]) db.set_state(message.chat.id, config.States.S_START.value) @bot.message_handler(func=lambda message: db.get_current_state(message.chat.id) == config.States.S_PERSONAL_DATA_REQUEST.value) def user_personal_data_permission(message): keyboard = types.InlineKeyboardMarkup() key_yes = types.InlineKeyboardButton(text='Так', callback_data='yes') keyboard.add(key_yes) key_no = types.InlineKeyboardButton(text='Ні', callback_data='no') keyboard.add(key_no) bot.send_message(message.chat.id, text=config.message_library["personal_data_permission"], reply_markup=keyboard) @bot.callback_query_handler(func=lambda call: db.get_current_state( call.message.chat.id) == config.States.S_PERSONAL_DATA_REQUEST.value) def callback_personal_data(call):
elif state == config.States.S_ENTER_LANG.value: bot.send_message(message.chat.id, "Жду ввода языка...") else: bot.send_message(message.chat.id, "Привет! Введи фразу, которую хочешь перевести") db.set_state(message.chat.id, config.States.S_ENTER_PHRASE.value) @bot.message_handler(commands=["restart"]) def cmd_restart(message): bot.send_message(message.chat.id, "Привет! Введи фразу, которую хочешь перевести") db.set_state(message.chat.id, config.States.S_ENTER_PHRASE.value) @bot.message_handler(func=lambda message: db.get_current_state(message.chat.id) .decode("utf-8") == config.States.S_ENTER_PHRASE.value) def get_phrase(message): message_phrase = message.text detect_lang = requests.post(detect_url, data={ 'key': key, 'text': message.text }) bot.send_message( message.chat.id, "Введите название языка, на который хотите перевести фразу " + "\"" + message_phrase + "\"") db.set_state(message.chat.id, config.States.S_ENTER_LANG.value) db.set_phrase(message.chat.id, message.text)
bot.send_message(message.chat.id, "Ожидаю ваш заголовок:)") elif state == config.States.S_SEND_DESCRIPTION.value: bot.send_message(message.chat.id, "Ожидаю ваше описание:)") elif state == config.States.S_SEND_PRICE.value: bot.send_message(message.chat.id, "Ожидаю вашу цены на товар:)") elif state == config.States.S_SEND_PHOTO.value: bot.send_message(message.chat.id, "Ожидаю фото товара:)") else: bot.send_message(message.chat.id, "Рад встрече, чего желаете?", reply_markup=keyboard.menuKeyboard()) db.set_state(message.chat.id, config.States.S_START.value) # Questions flow @bot.message_handler(func=lambda message: db.get_current_state(message.chat.id) == config.States.S_ENTER_CATEGORY.value) def user_entering_category(message): db.update_data(message.from_user.id, 'category', message.text) bot.send_message(message.chat.id, "Супер. Напишите ваш город:", reply_markup=keyboard.hideKeyboard()) db.set_state(message.chat.id, config.States.S_ENTER_CITY.value) @bot.message_handler(func=lambda message: db.get_current_state(message.chat.id) == config.States.S_ENTER_CITY.value) def user_entering_city(message): if len(message.text) < 2 or len(message.text) > 30: bot.send_message(message.chat.id, "Введите настоящий город!") return