Reputation: 1
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.
Upvotes: 0
Views: 2414
Reputation: 220
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."
)
Upvotes: 0
Reputation: 34
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):
from telegram import *
from telegram.ext import *
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.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')
Upvotes: 0