如何使 Telegram 深度链接适用于消息?

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

我正在开发一个机器人,它可以在 Telegram 中创建游戏。

当用户发出

/start_game
命令时,机器人会通过一条消息回复用户的命令,其中包含指向所创建游戏的链接。

现在,当同一用户再次发出

/start_game
命令时,机器人会做出如下响应:

您想删除您的当前活动的游戏并将其替换为新游戏吗?

currently active game
文本应通过 Telegram 深度链接链接到之前创建的游戏消息。

我目前执行此操作的方法是使用文档中的深层链接模板

f'<a href="tg://privatepost?channel={chat_id}&post={message_id}&single">{text}</a>'

Telethon
下工作得很好,但现在我切换到
Python-Telegram-Bot
,它就不行了。

单击后,此类链接会显示“很遗憾,您无法访问此消息。您不是发布该消息的聊天室的成员。”。

嗯,我认为问题是因为 Telegram Bot API 在组的 id 前面加上

-
,频道的 id 加上
-100
,所以我去掉了这些。

现在它说:“消息不存在”。

实际代码:

def message_mention(text: str, chat_id: int, message_id: int, escape: EscapeType | None = 'html'):
    text = escape_string(text, escape)
    if chat_id is None or message_id is None:
        return text

    chat_id_string = str(chat_id)
    if chat_id_string.startswith('-100'):
        chat_id_string = chat_id_string[4:]
    elif chat_id_string.startswith('-'):
        chat_id_string = chat_id_string[1:]

    return f'<a href="tg://privatepost?channel={chat_id_string}&post={message_id}&single">{text}</a>'

我在这里缺少什么?

message_id
必须正确,因为稍后在同一功能中,如果用户单击“是,删除旧游戏”,则会删除上述消息。

更新:

最小可重现示例,尝试链接到用户刚刚发出的命令。

async def main():
    from telegram import Update
    from telegram.ext import ContextTypes
    from telegram.ext import CommandHandler
    from telegram.ext import Application, Defaults


    app = (
        Application
        .builder()
        .defaults(
            Defaults(
                parse_mode="html"
            )
        )
        .token(BOT_TOKEN)
        .build()
    )

    def message_mention_lonami(text: str, chat_id: int, message_id: int):
        chat_id_string = str(chat_id)
        if chat_id_string.startswith('-100'):
            chat_id_string = chat_id_string[4:]
        elif chat_id_string.startswith('-'):
            chat_id_string = chat_id_string[1:]

        return f'<a href="https://t.me/c/{chat_id_string}/{message_id}">{text}</a>'

    def message_mention(text: str, chat_id: int, message_id: int):
        chat_id_string = str(chat_id)
        if chat_id_string.startswith('-100'):
            chat_id_string = chat_id_string[4:]
        elif chat_id_string.startswith('-'):
            chat_id_string = chat_id_string[1:]

        return f'<a href="tg://privatepost?channel={chat_id_string}&post={message_id}&single">{text}</a>'


    async def handler(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
        chat_id = update.effective_chat.id
        message_id = update.effective_message.id

        mention1 = message_mention_lonami("Solution by lonami", chat_id=chat_id, message_id=message_id)
        mention2 = message_mention("Default solution", chat_id=chat_id, message_id=message_id)

        await ctx.bot.send_message(
            chat_id,
            "\n".join([
                "is this your message?",
                mention1,
                mention2,
            ]),
        )

    app.add_handler(CommandHandler("test_mention", handler))

    async with app:
        try:
            await app.start()
            await app.updater.start_polling()
            await asyncio.Future()
        finally:
            await app.updater.stop()
            await app.stop()


if __name__ == '__main__':
    import asyncio
    asyncio.new_event_loop().run_until_complete(main())

客户端应用程序中的结果:

python telegram telegram-bot python-telegram-bot telethon
1个回答
0
投票

问题在于您尝试链接到与机器人的私人聊天中的消息。 此处记录的消息链接仅适用于超级组和通道,这可以从描述和

channel
参数的描述中推断出来。 事实上,如果我将您的
CommandHandler
替换为
MessageHandler
并在通道中运行该示例,那么效果非常好。


免责声明:我目前是

python-telegram-bot
的维护者。

© www.soinside.com 2019 - 2024. All rights reserved.