I’m developing a bot, which creates games in Telegram.
When a user issues a /start_game
command, the bot replies to user’s command with a message, which includes a link to the created game.
Now when the same user issues the /start_game
command again, the bot responds with something along the lines:
Would you like to delete your currently active game and replace it with a new one?
The currently active game
text should link to previously created game’s message via Telegram Deep Linking.
The way I do this currently is by using this deep link template from the docs:
f'<a href="tg://privatepost?channel={chat_id}&post={message_id}&single">{text}</a>'
Which worked fine under Telethon
, but now that I switched over to Python-Telegram-Bot
, it does not.
When clicked, such link says "Unfortunately you can’t access this message. You aren’t a member of the chat where it was posted.".
Well, I thought that the problem was because Telegram Bot API prefixes groups’ ids with -
and channels’ ids with -100
, so I got rid of those.
And now it says: "Message doesn’t exist".
Actual code:
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>'
What am I missing here?
The message_id
must be correct, because later in the same function if the user clicked "Yes, delete the old game", it deletes the mentioned message fine and dandy.
UPD:
Minimal reproducible example, which attempts to link to a command just issued by the user.
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())
Results in the client app:
2
Answers
The problem is that you try to link to a message in a private chat with the bot or a non-super group. The message links documented here are only available for supergroups and channels as can be infered from the description and the description of the
channel
parameter.In fact, if run the example in a group or if I replace your
CommandHandler
with aMessageHandler
and run the example in a channel, it works perfectly fine.Disclaimer: I’m cururently the maintainer of
python-telegram-bot
.I am not sure if this will work or not but you can read this telegram-chatbot-using-python-telegram-bot. Just go to Github code given in this post and compare your code and check if that works