Beispiel #1
0
def random(update: Update, context: CallbackContext):
    # get a word from your Sheets
    log(update, func_name="random")

    with request.urlopen(dconfig("SPREADSHEET_URL")) as response:
        if response.getcode() == 200:
            source = response.read()
            data = json.loads(source)

            word_cache = get_words_for_user(update.effective_user.id)

            if len(word_cache) == len(data['values']):
                clear_words_for_user(update.effective_user.id)

            while True:
                chosen = choice(range(len(data['values'])))
                if data['values'][chosen][0] not in word_cache:
                    MESSAGE = f"""
*{data['values'][chosen][0]}*                  _{data['values'][chosen][2]}_
------------------------------------------------
`{data['values'][chosen][1]}`   

{data['values'][chosen][3]}

Hint=_{data['values'][chosen][4] if len(data['values'][chosen])==5 else ""}_
            
            """
                    if len(word_cache) == 0:
                        update.message.reply_markdown(f"[None in cache]")
                    else:
                        update.message.reply_markdown(f"_{word_cache}_")

                    update.message.reply_markdown(MESSAGE)
                    add_word_for_user(update.effective_user.id,
                                      data['values'][chosen][0])
                    break
                else:
                    continue
        else:
            MESSAGE = f"""Sheets API high or something..idk"""
            update.message.reply_markdown(MESSAGE)
Beispiel #2
0
def allWords(update: Update, context: CallbackContext):
    # get all words from Sheets
    log(update, func_name="all")
    with request.urlopen(dconfig('SPREADSHEET_URL')) as response:
        if response.getcode() == 200:
            source = response.read()
            data = json.loads(source)
            for i in range(1, len(data['values'])):

                MESSAGE = f"""
{i}) *{data['values'][i][0]}*                  _{data['values'][i][2]}_
------------------------------------------------
`{data['values'][i][1]}`   

{data['values'][i][3]}

Hint=_{data['values'][i][4] if len(data['values'][i])==5 else ""}_
            
            """
                update.message.reply_markdown(MESSAGE)
        else:
            MESSAGE = f"""Sheets API high or something..idk"""
            update.message.reply_markdown(MESSAGE)
Beispiel #3
0
def search(update: Update, context: CallbackContext):
    # get substring matches from word / meaning field
    log(update, func_name="search")
    with request.urlopen(dconfig('SPREADSHEET_URL')) as response:
        if response.getcode() == 200:
            if context.args:
                count = 0
                source = response.read()
                data = json.loads(source)
                for i in range(1, len(data['values'])):
                    if (data['values'][i][0].find(context.args[0]) != -1 or
                            data['values'][i][1].find(context.args[0]) != -1):
                        MESSAGE = f"""
{i}) *{data['values'][i][0]}*                  _{data['values'][i][2]}_
------------------------------------------------
`{data['values'][i][1]}`   

{data['values'][i][3]}

Hint=_{data['values'][i][4] if len(data['values'][i])==5 else ""}_
            
                        """
                        count = count + 1
                        update.message.reply_markdown(MESSAGE)

                if count == 0:
                    MESSAGE = f"""_Could not find requested word_"""
                    update.message.reply_markdown(MESSAGE)
                else:
                    MESSAGE = f"""_{count} matches found_"""
                    update.message.reply_markdown(MESSAGE)
            else:
                MESSAGE = f"""Uhhh.... I could search, but what?"""
                update.message.reply_markdown(MESSAGE)
        else:
            MESSAGE = f"""Sheets API high or something..idk"""
            update.message.reply_markdown(MESSAGE)
Beispiel #4
0
def clear(update: Update, context: CallbackContext):
    # clear cache
    log(update, func_name="clear")
    clear_words_for_user(update.effective_user.id)
    text_blob = f"""_Cleared cache_"""
    update.message.reply_markdown(text_blob)
Beispiel #5
0
def help(update: Update, context: CallbackContext):
    # display help message
    log(update, func_name="help")

    text_blob = "The commands literally do stuff as stated."
    update.message.reply_markdown(text_blob)
Beispiel #6
0
def start(update: Update, context: CallbackContext):
    # start message
    log(update, func_name="start")
    update.message.reply_markdown(START_TEXT)