Esempio n. 1
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'<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)
Esempio n. 2
0
def inline_query(  # pylint: disable=R0915
        update: Update,
        _: CallbackContext,
        threshold: int = 15) -> None:
    query = cast(InlineQuery, update.inline_query).query
    results_list = list()

    if len(query) > 0:
        if query.startswith('#'):
            hints = taghints.get_hints(query, full_match=False)
            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 or 'ptbcontrib/' 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:
                assert replaced is not None  # for mypy
                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:
                assert replaced is not None  # for mypy
                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 question in faq:
                    results_list.append(
                        article(
                            title=f'{question[0]}',
                            description="Github wiki for python-telegram-bot",
                            message_text=f'Wiki of <i>python-telegram-bot</i>\n'
                            f'<a href="{question[1]}">{question[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:
                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 '
                        f'<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:
                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 '
                    f'<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>',
                ))

    ilq = cast(InlineQuery, update.inline_query)
    try:
        ilq.answer(
            results=results_list,
            switch_pm_text='Help',
            switch_pm_parameter='inline-help',
            cache_time=0,
            auto_pagination=True,
        )
    except BadRequest as exc:
        if "can't parse entities" not in exc.message:
            raise exc
        ilq.answer(
            results=[],
            switch_pm_text='❌ Invalid entities. Click me.',
            switch_pm_parameter='inline-entity-parsing',
        )
Esempio n. 3
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)
Esempio n. 4
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)