skip to Main Content

I am trying to get my telegram bot mention users on telegram who don’t have username and want to mention them using userID.
Below is my python code.. it doesn’t tags those who don’t have username.

def welcome(update, context, new_member):
    # Greets a person who joins the chat
    message = update.message
    chat_id = message.chat.id
    if new_member.username is None:
        username = "["+new_member.first_name+"](tg://user?id="+str(new_member.id)+")"
    else:
        username = new_member.username
    logger.info(
        "%s joined to chat %d (%s)",
        escape(username),
        chat_id,
        escape(message.chat.title),
    )

    text = (
        f"Hello @{username}! Welcome to the {message.chat.title} "
        "telegram group!n"
        "Please introduce yourself."
    )

    context.bot.send_message(chat_id=chat_id, text=text)

according to link : it is possible to mention user by its numerical id with markup:
Markdown style

To use this mode, pass Markdown in the parse_mode field when using sendMessage. Use the following syntax in your message:

inline mention of a user

I am not able to apply this in my code and get it to work. Can someone pls help me?
The feature has been applied successfully here

https://github.com/domdorn/telegram/commit/ea308cadb739a9a018c7fbabc16824e2ff82d415

I wanna apply the same in my code so that it will mention users who don’t have username with their ID.

2

Answers


  1. You have to specify parse_mode=telegram.ParseMode.MARKDOWN or parse_mode='markdown' in send_message method.
    Your edited code should look like this:

    def welcome(update, context, new_member):
        # Greets a person who joins the chat
        message = update.message
        chat_id = message.chat.id
        if new_member.username is None:
            username = "["+new_member.first_name+"](tg://user?id="+str(new_member.id)+")"
        else:
            username = new_member.username
        logger.info(
            "%s joined to chat %d (%s)",
            escape(username),
            chat_id,
            escape(message.chat.title),
        )
    
        text = (
            f"Hello @{username}! Welcome to the {message.chat.title} "
            "telegram group!n"
            "Please introduce yourself."
        )
    
        context.bot.send_message(
            chat_id=chat_id,
            text=text,
            parse_mode=telegram.ParseMode.MARKDOWN,
        )
    

    You can also use HTML or MARKDOWN_V2.

    Link to documentation and full info:
    https://python-telegram-bot.readthedocs.io/en/stable/telegram.parsemode.html#telegram.ParseMode
    https://core.telegram.org/bots/api#formatting-options

    Tips to make your code better and help you code easier (I considered you’re using python-telegram-bot):

    • import your library like this. It makes it easier for you to code:
      from telegram import *
      from telegram.ext import *
    • (considering you’ve done the previous tip) If you’re coding in VSCode or similar, you can replace def welcome(update, context, new_member) with def welcome(update: Update, context: CallbackContext, new_member: User). So your editor/IDE will understand the type of variable and recommend/auto-fill your code.
    • instead of context.bot.send_message use update.message.reply_text so you can omit chat_id=chat_id. exmaple:
      update.message.reply_text(text, parse_mode='markdown')
    Login or Signup to reply.
  2. You should specify a parse_mode param:

    context.bot.send_message(
        chat_id=chat_id,
        text=text,
        parse_mode='markdown',
    )
    

    Btw it’s not necessary to write ‘@’ when you use an inline link.

    Also, I’d recommend you use telegram-text module to write a link to a user:

    from telegram_text import InlineUser, User
    
    
    def welcome(update, context, new_member):
        ...
        if new_member.username is None:
            user = InlineUser(new_member.first_name, new_member.id)
        else:
            user = User(new_member.username)
    
        text = (
            f"Hello {user}! Welcome to the {message.chat.title} "
            "telegram group!n"
            "Please introduce yourself."
        )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search