@fr0s1ee

Как отличить гиперссылку от обычного текста в Telebot?

Столкнулся с такой проблемой что не могу скопировать сообщение от пользователя так , чтобы если надо было его отправить, то отправился обычный текст и гиперссылка в одном сообщении(делал я это с помощью команды:
text = '[<ваш текст>](<ссылка>)'
bot.send_message(message.chat.id, text, parse_mode='MarkdownV2')

Но у меня выделялся весь текст, вместо того который мне нужно сделать гиперссылкой.
Потом я решил написать команду "message" и посмотреть разницу обычного текста от текста с гиперссылкой, но в коде который мне выдавал бот я не нашел никаких отличий. Сам код который отправил мне бот:
{'content_type': 'text', 'id': 5910, 'message_id': 5910, 'from_user': {'id': 702409525, 'is_bot': False, 'first_name': 'forest', 'username': 'floychick', 'last_name': None, 'language_code': 'ru', 'can_join_groups': None, 'can_read_all_group_messages': None, 'supports_inline_queries': None, 'is_premium': True, 'added_to_attachment_menu': None}, 'date': 1686396584, 'chat': {'id': 702409525, 'type': 'private', 'title': None, 'username': 'floychick', 'first_name': 'forest', 'last_name': None, 'is_forum': None, 'photo': None, 'bio': None, 'join_to_send_messages': None, 'join_by_request': None, 'has_private_forwards': None, 'has_restricted_voice_and_video_messages': None, 'description': None, 'invite_link': None, 'pinned_message': None, 'permissions': None, 'slow_mode_delay': None, 'message_auto_delete_time': None, 'has_protected_content': None, 'sticker_set_name': None, 'can_set_sticker_set': None, 'linked_chat_id': None, 'location': None, 'active_usernames': None, 'emoji_status_custom_emoji_id': None, 'has_hidden_members': None, 'has_aggressive_anti_spam_enabled': None}, 'sender_chat': None, 'forward_from': None, 'forward_from_chat': None, 'forward_from_message_id': None, 'forward_signature': None, 'forward_sender_name': None, 'forward_date': None, 'is_automatic_forward': None, 'reply_to_message': None, 'via_bot': None, 'edit_date': None, 'has_protected_content': None, 'media_group_id': None, 'author_signature': None, 'text': 'Привет, вот ссылка', 'entities': [<telebot.types.MessageEntity object at 0x000001FBADA2E390>], 'caption_entities': None, 'audio': None, 'document': None, 'photo': None, 'sticker': None, 'video': None, 'video_note': None, 'voice': None, 'caption': None, 'contact': None, 'location': None, 'venue': None, 'animation': None, 'dice': None, 'new_chat_member': None, 'new_chat_members': None, 'left_chat_member': None, 'new_chat_title': None, 'new_chat_photo': None, 'delete_chat_photo': None, 'group_chat_created': None, 'supergroup_chat_created': None, 'channel_chat_created': None, 'migrate_to_chat_id': None, 'migrate_from_chat_id': None, 'pinned_message': None, 'invoice': None, 'successful_payment': None, 'connected_website': None, 'reply_markup': None, 'message_thread_id': None, 'is_topic_message': None, 'forum_topic_created': None, 'forum_topic_closed': None, 'forum_topic_reopened': None, 'has_media_spoiler': None, 'forum_topic_edited': None, 'general_forum_topic_hidden': None, 'general_forum_topic_unhidden': None, 'write_access_allowed': None, 'user_shared': None, 'chat_shared': None, 'json': {'message_id': 5910, 'from': {'id': 702409525, 'is_bot': False, 'first_name': 'forest', 'username': 'floychick', 'language_code': 'ru', 'is_premium': True}, 'chat': {'id': 702409525, 'first_name': 'forest', 'username': 'floychick', 'type': 'private'}, 'date': 1686396584, 'text': 'Привет, вот ссылка', 'entities': [{'offset': 12, 'length': 6, 'type': 'text_link', 'url': 'https://habr.com/'}]}}

Как мне отличить обычный текст от текста с гиперссылкой? Или как мне отправить сообщение с обычным текстом и текстом с гиперссылкой?
  • Вопрос задан
  • 266 просмотров
Решения вопроса 1
SoreMix
@SoreMix
yellow
Как не отличается, у вас есть entities. У сущности есть offset и length. Считаем их и получаем нужный кусок.

Можно сделать message.html_text и распарсить как html. В markdown, по-моему, тоже был конвертер

text = 'Regular text [follow me](https://habr.com) wow'
msg = bot.send_message(message.chat.id, text, parse_mode='MarkdownV2')
print(msg.html_text)
# Regular text <a href="https://habr.com/">follow me</a> wow
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы