Beispiel #1
0
def wiki(bot, update, args, chat_data, threshold=80):
    """ Wiki search """
    query = ' '.join(args)
    if search != '':
        best = search.wiki(query, amount=1, threshold=threshold)

        if best:
            text = (f'Github wiki for _python-telegram-bot_\n'
                    f'[{best[0][0]}]({best[0][1]})')
        else:
            text = "Sorry, your search term didn't match anything, please edit your message to search again."

        reply_or_edit(bot, update, chat_data, text)
def fuzzy_replacements_markdown(query, threshold=95, official_api_links=True):
    """ Replaces the enclosed characters in the query string with hyperlinks to the documentations """
    symbols = re.findall(ENCLOSED_REGEX, query)

    if not symbols:
        return None, None

    replacements = list()
    for s in symbols:
        # Wiki first, cause with docs you can always prepend telegram. for better precision
        wiki = search.wiki(s.replace('_', ' '), amount=1, threshold=threshold)
        if wiki:
            name = wiki[0][0].split(ARROW_CHARACTER)[-1].strip()
            text = f'<a href="{wiki[0][1]}">{name}</a>'
            replacements.append((wiki[0][0], s, text))
            continue

        doc = search.docs(s, threshold=threshold)
        if doc:
            text = f'<a href="{doc.url}">{doc.short_name}</a>'

            if doc.tg_url and official_api_links:
                text += f' <a href="{doc.tg_url}">{TELEGRAM_SUPERSCRIPT}</a>'

            replacements.append((doc.short_name, s, text))
            continue

        # not found
        replacements.append((s + '❓', s, escape_markdown(s)))

    result = query
    for name, symbol, text in replacements:
        char = ENCLOSING_REPLACEMENT_CHARACTER
        result = result.replace(f'{char}{symbol}{char}', text)

    result_changed = [x[0] for x in replacements]
    return result_changed, result
Beispiel #3
0
def fuzzy_replacements_markdown(query, threshold=95, official_api_links=True):
    """ Replaces the enclosed characters in the query string with hyperlinks to the documentations """
    symbols = re.findall(ENCLOSED_REGEX, query)

    if not symbols:
        return None, None

    replacements = list()
    for s in symbols:
        # Wiki first, cause with docs you can always prepend telegram. for better precision
        wiki = search.wiki(s.replace('_', ' '), amount=1, threshold=threshold)
        if wiki:
            name = wiki[0][0].split(ARROW_CHARACTER)[-1].strip()
            text = f'[{name}]({wiki[0][1]})'
            replacements.append((wiki[0][0], s, text))
            continue

        doc = search.docs(s, threshold=threshold)
        if doc:
            text = f'[{doc.short_name}]({doc.url})'

            if doc.tg_url and official_api_links:
                text += f' [{TELEGRAM_SUPERSCRIPT}]({doc.tg_url})'

            replacements.append((doc.short_name, s, text))
            continue

        # not found
        replacements.append((s + '❓', s, escape_markdown(s)))

    result = query
    for name, symbol, text in replacements:
        char = ENCLOSING_REPLACEMENT_CHARACTER
        result = result.replace(f'{char}{symbol}{char}', text)

    result_changed = [x[0] for x in replacements]
    return result_changed, result
def inline_query(update: Update, context: CallbackContext, threshold=20):
    query = update.inline_query.query
    results_list = list()

    if len(query) > 0:
        if query.startswith('#'):
            hints = taghints.get_hints(query)
            results_list.extend([
                article(f'Send hint on {key.capitalize()}',
                        hint.help,
                        hint.msg,
                        key=key,
                        reply_markup=hint.reply_markup)
                for key, hint in hints.items()
            ])

        if '#' in query or '@' in query:
            results_list.extend(inline_github(query))

        if ENCLOSING_REPLACEMENT_CHARACTER in query:
            modified, replaced = fuzzy_replacements_markdown(
                query, official_api_links=True)
            if modified:
                results_list.append(
                    article(
                        title=
                        "Replace links and show official Bot API documentation",
                        description=', '.join(modified),
                        message_text=replaced))

            modified, replaced = fuzzy_replacements_markdown(
                query, official_api_links=False)
            if modified:
                results_list.append(
                    article(title="Replace links",
                            description=', '.join(modified),
                            message_text=replaced))

        # If no results so far then search wiki and docs
        if not results_list:
            doc = search.docs(query, threshold=threshold)
            if doc:
                text = f'<b>{doc.short_name}</b>\n' \
                    f'<i>python-telegram-bot</i> documentation for this {doc.type}:\n' \
                    f'<a href="{doc.url}">{doc.full_name}</a>'
                if doc.tg_name:
                    text += f'\n\nThe official documentation has more info about <a href="{doc.tg_url}">{doc.tg_name}</a>.'

                results_list.append(
                    article(
                        title=f'{doc.full_name}',
                        description="python-telegram-bot documentation",
                        message_text=text,
                    ))

            wiki_pages = search.wiki(query, amount=4, threshold=threshold)
            if wiki_pages:
                # Limit number of search results to maximum (-1 cause we might have added a doc above)
                wiki_pages = wiki_pages[:49]
                for wiki_page in wiki_pages:
                    results_list.append(
                        article(
                            title=f'{wiki_page[0]}',
                            description="Github wiki for python-telegram-bot",
                            message_text=f'Wiki of <i>python-telegram-bot</i>\n'
                            f'<a href="{wiki_page[1]}">{wiki_page[0]}</a>'))

        # If no results even after searching wiki and docs
        if not results_list:
            results_list.append(
                article(
                    title='❌ No results.',
                    description='',
                    message_text=
                    f'<a href="{WIKI_URL}">GitHub wiki</a> of <i>python-telegram-bot</i>',
                ))

    else:
        # If no query then add all wiki pages (max 50)
        for name, link in search.all_wiki_pages()[:50]:
            results_list.append(
                article(
                    title=name,
                    description='Wiki of python-telegram-bot',
                    message_text=f'Wiki of <i>python-telegram-bot</i>\n'
                    f'<a href="{link}">{escape_markdown(name)}</a>',
                ))

    update.inline_query.answer(results=results_list,
                               switch_pm_text='Help',
                               switch_pm_parameter='inline-help',
                               cache_time=0)
Beispiel #5
0
def inline_query(bot, update, threshold=20):
    query = update.inline_query.query
    results_list = list()

    if len(query) > 0:
        modified, replaced = fuzzy_replacements_markdown(
            query, official_api_links=True)
        if modified:
            results_list.append(
                article(
                    title=
                    "Replace links and show official Bot API documentation",
                    description=', '.join(modified),
                    message_text=replaced))

        modified, replaced = fuzzy_replacements_markdown(
            query, official_api_links=False)
        if modified:
            results_list.append(
                article(title="Replace links",
                        description=', '.join(modified),
                        message_text=replaced))

        wiki_pages = search.wiki(query, amount=4, threshold=threshold)
        doc = search.docs(query, threshold=threshold)

        if doc:
            text = f'*{doc.short_name}*\n' \
                   f'_python-telegram-bot_ documentation for this {doc.type}:\n' \
                   f'[{doc.full_name}]({doc.url})'
            if doc.tg_name:
                text += f'\n\nThe official documentation has more info about [{doc.tg_name}]({doc.tg_url}).'

            results_list.append(
                article(
                    title=f'{doc.full_name}',
                    description="python-telegram-bot documentation",
                    message_text=text,
                ))

        if wiki_pages:
            for wiki_page in wiki_pages:
                results_list.append(
                    article(title=f'{wiki_page[0]}',
                            description="Github wiki for python-telegram-bot",
                            message_text=f'Wiki of _python-telegram-bot_\n'
                            f'[{wiki_page[0]}]({wiki_page[1]})'))

        # "No results" entry
        if len(results_list) == 0:
            results_list.append(
                article(
                    title='❌ No results.',
                    description='',
                    message_text=
                    f'[GitHub wiki]({WIKI_URL}) of _python-telegram-bot_',
                ))

    else:  # no query input
        # add all wiki pages
        for name, link in search._wiki.items():
            results_list.append(
                article(
                    title=name,
                    description='Wiki of python-telegram-bot',
                    message_text=f'Wiki of _python-telegram-bot_\n'
                    f'[{escape_markdown(name)}]({link})',
                ))

    bot.answerInlineQuery(update.inline_query.id,
                          results=results_list,
                          switch_pm_text='Help',
                          switch_pm_parameter='inline-help')
Beispiel #6
0
def inline_query(update: Update, context: CallbackContext, threshold=15):
    query = update.inline_query.query
    results_list = list()

    if len(query) > 0:
        if query.startswith('#'):
            hints = taghints.get_hints(query)
            results_list.extend([
                article(f'Send hint on {key.capitalize()}',
                        hint.help,
                        hint.msg,
                        key=key,
                        reply_markup=hint.reply_markup)
                for key, hint in hints.items()
            ])

        if '#' in query or '@' in query:
            results_list.extend(inline_github(query))

        if ENCLOSING_REPLACEMENT_CHARACTER in query:
            modified, replaced = fuzzy_replacements_html(
                query, official_api_links=True)
            if modified:
                results_list.append(
                    article(
                        title=
                        "Replace links and show official Bot API documentation",
                        description=', '.join(modified),
                        message_text=replaced))

            modified, replaced = fuzzy_replacements_html(
                query, official_api_links=False)
            if modified:
                results_list.append(
                    article(title="Replace links",
                            description=', '.join(modified),
                            message_text=replaced))

        if query.lower() == 'faq':
            for name, link in search.all_faq():
                results_list.append(
                    article(
                        title=name,
                        description='Wiki of python-telegram-bot',
                        message_text=f'Wiki of <i>python-telegram-bot</i>\n'
                        f'<a href="{link}">{escape(name)}</a>',
                    ))
        if query.lower().startswith('faq') and len(query.split(' ')) > 1:
            faq = search.faq(query.split(' ', 1)[1],
                             amount=20,
                             threshold=threshold)
            if faq:
                for q in faq:
                    results_list.append(
                        article(
                            title=f'{q[0]}',
                            description="Github wiki for python-telegram-bot",
                            message_text=f'Wiki of <i>python-telegram-bot</i>\n'
                            f'<a href="{q[1]}">{q[0]}</a>'))

        if query.lower() == 'snippets':
            for name, link in search.all_code_snippets():
                results_list.append(
                    article(
                        title=name,
                        description='Wiki of python-telegram-bot',
                        message_text=f'Wiki of <i>python-telegram-bot</i>\n'
                        f'<a href="{link}">{escape(name)}</a>',
                    ))
        if query.lower().startswith('snippets') and len(query.split(' ')) > 1:
            snippets = search.code_snippets(query.split(' ', 1)[1],
                                            amount=20,
                                            threshold=threshold)
            if snippets:
                snippets = snippets
                for snippet in snippets:
                    results_list.append(
                        article(
                            title=f'{snippet[0]}',
                            description="Github wiki for python-telegram-bot",
                            message_text=f'Wiki of <i>python-telegram-bot</i>\n'
                            f'<a href="{snippet[1]}">{snippet[0]}</a>'))

        # If no results so far then search wiki and docs
        if not results_list:
            doc = search.docs(query, threshold=threshold)
            if doc:
                text = f'<b>{doc.short_name}</b>\n' \
                    f'<i>python-telegram-bot</i> documentation for this {doc.type}:\n' \
                    f'<a href="{doc.url}">{doc.full_name}</a>'
                if doc.tg_name:
                    text += f'\n\nThe official documentation has more info about <a href="{doc.tg_url}">{doc.tg_name}</a>.'

                results_list.append(
                    article(
                        title=f'{doc.full_name}',
                        description="python-telegram-bot documentation",
                        message_text=text,
                    ))

            wiki_pages = search.wiki(query, amount=4, threshold=threshold)
            if wiki_pages:
                wiki_pages = wiki_pages
                for wiki_page in wiki_pages:
                    results_list.append(
                        article(
                            title=f'{wiki_page[0]}',
                            description="Github wiki for python-telegram-bot",
                            message_text=f'Wiki of <i>python-telegram-bot</i>\n'
                            f'<a href="{wiki_page[1]}">{wiki_page[0]}</a>'))

        # If no results even after searching wiki and docs
        if not results_list:
            results_list.append(
                article(
                    title='❌ No results.',
                    description='',
                    message_text=
                    f'<a href="{WIKI_URL}">GitHub wiki</a> of <i>python-telegram-bot</i>',
                ))

    else:
        for name, link in search.all_wiki_pages():
            results_list.append(
                article(
                    title=name,
                    description='Wiki of python-telegram-bot',
                    message_text=f'Wiki of <i>python-telegram-bot</i>\n'
                    f'<a href="{link}">{escape(name)}</a>',
                ))

    update.inline_query.answer(results=results_list,
                               switch_pm_text='Help',
                               switch_pm_parameter='inline-help',
                               cache_time=0,
                               auto_pagination=True)
Beispiel #7
0
def inline_query(update: Update, context: CallbackContext, threshold=20):
    query = update.inline_query.query
    results_list = list()

    if len(query) > 0:
        if query.startswith('#'):
            hints = taghints.get_hints(query)
            results_list.extend([article(f'Send hint on {key.capitalize()}',
                                         hint.help,
                                         hint.msg,
                                         key=key,
                                         reply_markup=hint.reply_markup) for key, hint in hints.items()])

        if '#' in query or '@' in query:
            results_list.extend(inline_github(query))

        if ENCLOSING_REPLACEMENT_CHARACTER in query:
            modified, replaced = fuzzy_replacements_markdown(query, official_api_links=True)
            if modified:
                results_list.append(article(
                    title="Replace links and show official Bot API documentation",
                    description=', '.join(modified),
                    message_text=replaced))

            modified, replaced = fuzzy_replacements_markdown(query, official_api_links=False)
            if modified:
                results_list.append(article(
                    title="Replace links",
                    description=', '.join(modified),
                    message_text=replaced))

        # If no results so far then search wiki and docs
        if not results_list:
            doc = search.docs(query, threshold=threshold)
            if doc:
                text = f'*{doc.short_name}*\n' \
                    f'_python-telegram-bot_ documentation for this {doc.type}:\n' \
                    f'[{doc.full_name}]({doc.url})'
                if doc.tg_name:
                    text += f'\n\nThe official documentation has more info about [{doc.tg_name}]({doc.tg_url}).'

                results_list.append(article(
                    title=f'{doc.full_name}',
                    description="python-telegram-bot documentation",
                    message_text=text,
                ))

            wiki_pages = search.wiki(query, amount=4, threshold=threshold)
            if wiki_pages:
                # Limit number of search results to maximum (-1 cause we might have added a doc above)
                wiki_pages = wiki_pages[:49]
                for wiki_page in wiki_pages:
                    results_list.append(article(
                        title=f'{wiki_page[0]}',
                        description="Github wiki for python-telegram-bot",
                        message_text=f'Wiki of _python-telegram-bot_\n'
                        f'[{wiki_page[0]}]({wiki_page[1]})'
                    ))

        # If no results even after searching wiki and docs
        if not results_list:
            results_list.append(article(
                title='❌ No results.',
                description='',
                message_text=f'[GitHub wiki]({WIKI_URL}) of _python-telegram-bot_',
            ))

    else:
        # If no query then add all wiki pages (max 50)
        for name, link in search.all_wiki_pages()[:50]:
            results_list.append(article(
                title=name,
                description='Wiki of python-telegram-bot',
                message_text=f'Wiki of _python-telegram-bot_\n'
                f'[{escape_markdown(name)}]({link})',
            ))

    update.inline_query.answer(results=results_list, switch_pm_text='Help',
                               switch_pm_parameter='inline-help', cache_time=0)