How to make that when you click on the text it was copied pytelegrambotapi
Asked Answered
M

6

12

I am writing a telegram to the bot. I ran into such a problem. I need the bot to send a message (text) when clicked on which it was copied (as a token from @BotFather)

Mahmud answered 13/1, 2020 at 9:40 Comment(0)
G
26

If I understand you correctly, you wish to send a message, that when the user presses it, the text is automatically copied to the user's clipboard, just like the BotFather sends the API token?

enter image description here


This is done by the MarkDown parse_mode;
Send a message with &parse_mode=MarkDown and wrap the 'pressable' text in back-ticks (`):

Hi. `Press me!`!
https://api.telegram.org/bot<token>/sendMessage?chat_id=<id>&text=Hi! `Press me!`&parse_mode=MarkDown

enter image description here


EDIT: Bases on OP's comment you're looking for a solution.
From there documentation;

bot.send_message(
    chat_id=chat_id, 
    text="*bold* _italic_ `fixed width font` [link](http://google.com).", 
    parse_mode=telegram.ParseMode.MARKDOWN
)
Gecko answered 13/1, 2020 at 10:38 Comment(3)
Yes, but I do not quite understand how to realize it in the python. Let's say, there is a code snippet: bot.send_message(message.chat.id, 'Hello, my owner') How to do that the fragment of the text (owner) could be copied?Mahmud
Maybe: bot.send_message(message.chat.id, 'Hello, my owner', parse_mode=Markdown) ?Mahmud
Maybe: bot.send_message(message.chat.id, 'Hello, my `owner`', parse_mode=Markdown) ?Mahmud
T
3

You can simply by editing the text in following manner Write ``` then write your text then again write that 3 character . Bingo!!!

Transversal answered 29/1, 2021 at 16:41 Comment(0)
N
3

This solution worked for me for the Telegram Android Client as well as the Telegram Desktop client for windows.

bot = telebot.TeleBot(bot_token) #where bot_token is your unique identifier
text = "`" + text + "`" #now make sure the text has the backticks (leading and ending). 

And then be sure to set the parse mode to markdown (I'm using V2)

bot.send_message(bot_chat_id, text, parse_mode='MarkdownV2') #chat_id is another unique identifier

I sent a message to myself to demonstrate:

check_all() failed: 'NoneType' object has no attribute 'driver'

and with a single click on the message, it gets copied to clipboard!

Text copied to clipboard

Nectarine answered 25/8, 2022 at 13:36 Comment(0)
C
1

I prefer to use HTML because MarkDownV2 requires a lot of escaping:

copy_info = f'<code>{chat_info}</code>'
Chemulpo answered 2/12, 2023 at 16:7 Comment(0)
C
0

await botClient.SendTextMessageAsync(chatId, "Здравствуйте `текст для копирования` ", parseMode: ParseMode.Markdown);

Cyndicyndia answered 26/9, 2023 at 8:39 Comment(0)
G
0

Following what @craziks and @a-p said, either if you are writing the text by programming or in telegram itself, you can use BackTick (`) in below fashion to make copy-by-click text in telegram:

`mytext`    # gives copy-by-click text by graying mytext

```mytext```  # like above but a label named "copy" will be shown above the message

enter image description here

Goraud answered 4/5, 2024 at 1:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.