如何使用aiogram3在电报聊天机器人中制作下拉列表

问题描述 投票:0回答:1

我正在尝试创建一个下拉列表来选择一个国家/地区。由于列表非常大,我无法使用内联键盘。为此,我想在桌面、移动、网络等应用程序中使用下拉列表。

python-3.x telegram-bot chatbot aiogram
1个回答
0
投票

由于即使在电报官方文档中也找不到实现下拉列表的可能性,因此决定要求用户手动输入国家名称,然后使用thefuzz模糊文本比较库来检查列表中的所有国家。向用户显示得分最高的国家/地区以供澄清(使用母语的旗帜和标签)。

from rapidfuzz import fuzz


def comparison(target: str, texts: list[str]) -> None | str:
    candidate: dict = {"score": 0, "name": ""}
    pre_candidate: dict = {"score": 0, "name": ""}

    for text in texts:
        score = fuzz.ratio(target, text)

        if score > candidate["score"]:
            candidate = {"score": score, "name": text}
        elif score == candidate["score"]:
            pre_candidate = {"score": score, "name": text}

    if candidate["score"] == pre_candidate["score"]:
        return None

    return candidate["name"]
© www.soinside.com 2019 - 2024. All rights reserved.