Parsemode html python telegram bot

Message¶

class telegram. Message ( message_id , date , chat , from_user = None , forward_from = None , forward_from_chat = None , forward_from_message_id = None , forward_date = None , reply_to_message = None , edit_date = None , text = None , entities = None , caption_entities = None , audio = None , document = None , game = None , photo = None , sticker = None , video = None , voice = None , video_note = None , new_chat_members = None , caption = None , contact = None , location = None , venue = 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 , forward_signature = None , author_signature = None , media_group_id = None , connected_website = None , animation = None , passport_data = None , poll = None , forward_sender_name = None , reply_markup = None , dice = None , via_bot = None , proximity_alert_triggered = None , sender_chat = None , video_chat_started = None , video_chat_ended = None , video_chat_participants_invited = None , message_auto_delete_timer_changed = None , video_chat_scheduled = None , is_automatic_forward = None , has_protected_content = None , web_app_data = None , is_topic_message = None , message_thread_id = None , forum_topic_created = None , forum_topic_closed = None , forum_topic_reopened = None , forum_topic_edited = None , general_forum_topic_hidden = None , general_forum_topic_unhidden = None , write_access_allowed = None , has_media_spoiler = None , user_shared = None , chat_shared = None , * , api_kwargs = None ) [source] ¶ Bases: telegram.TelegramObject This object represents a message. Objects of this class are comparable in terms of equality. Two objects of this class are considered equal, if their message_id and chat are equal.

  • telegram.Bot.edit_message_caption()
  • telegram.Bot.edit_message_live_location()
  • telegram.Bot.edit_message_media()
  • telegram.Bot.edit_message_reply_markup()
  • telegram.Bot.edit_message_text()
  • telegram.Bot.forward_message()
  • telegram.Bot.send_animation()
  • telegram.Bot.send_audio()
  • telegram.Bot.send_contact()
  • telegram.Bot.send_dice()
  • telegram.Bot.send_document()
  • telegram.Bot.send_game()
  • telegram.Bot.send_invoice()
  • telegram.Bot.send_location()
  • telegram.Bot.send_message()
  • telegram.Bot.send_photo()
  • telegram.Bot.send_poll()
  • telegram.Bot.send_sticker()
  • telegram.Bot.send_venue()
  • telegram.Bot.send_video_note()
  • telegram.Bot.send_video()
  • telegram.Bot.send_voice()
  • telegram.Bot.set_game_score()
  • telegram.Bot.stop_message_live_location()
  • The arguments and attributes voice_chat_scheduled , voice_chat_started and voice_chat_ended , voice_chat_participants_invited were renamed to video_chat_scheduled / video_chat_scheduled , video_chat_started / video_chat_started , video_chat_ended / video_chat_ended and video_chat_participants_invited / video_chat_participants_invited , respectively, in accordance to Bot API 6.0.
  • The following are now keyword-only arguments in Bot methods: _timeout , api_kwargs , contact , quote , filename , loaction , venue . Use a named argument for those, and notice that some positional arguments changed position as a result.
  • message_id ( int ) – Unique message identifier inside this chat.
  • from_user ( telegram.User , optional) – Sender of the message; empty for messages sent to channels. For backward compatibility, this will contain a fake sender user in non-channel chats, if the message was sent on behalf of a chat.
  • sender_chat ( telegram.Chat , optional) – Sender of the message, sent on behalf of a chat. For example, the channel itself for channel posts, the supergroup itself for messages from anonymous group administrators, the linked channel for messages automatically forwarded to the discussion group. For backward compatibility, from_user contains a fake sender user in non-channel chats, if the message was sent on behalf of a chat.
  • date ( datetime.datetime ) – Date the message was sent in Unix time. Converted to datetime.datetime .
Читайте также:  Python есть ли строка в строке

Changed in version 20.3: The default timezone of the bot is used for localization, which is UTC unless telegram.ext.Defaults.tzinfo is used.

Changed in version 20.3: The default timezone of the bot is used for localization, which is UTC unless telegram.ext.Defaults.tzinfo is used.

Changed in version 20.3: The default timezone of the bot is used for localization, which is UTC unless telegram.ext.Defaults.tzinfo is used.

Changed in version 20.0: Accepts any collections.abc.Sequence as input instead of just a list. The input is converted to a tuple.

Changed in version 20.0: Accepts any collections.abc.Sequence as input instead of just a list. The input is converted to a tuple.

Changed in version 20.0: Accepts any collections.abc.Sequence as input instead of just a list. The input is converted to a tuple.

Changed in version 20.0: Accepts any collections.abc.Sequence as input instead of just a list. The input is converted to a tuple.

Changed in version 20.0: Accepts any collections.abc.Sequence as input instead of just a list. The input is converted to a tuple.

Источник

How should I use parse_mode=’HTML’ in telegram python bot?

I’m trying to send a message in a channel with a bot, using Telegram API’s send_photo() method. It takes a caption parameter (type String ) but I can’t format it through parse_mode=’HTML’ parameter. If I use something like this:

send_photo(chat_id, photo, caption="Some text", parse_mode='HTML') 

4 Answers 4

First, you need to import ParseMode from telegram like this:

from telegram import ParseMode 

Then, all you need is to specify parse_mode=ParseMode.HTML . Here’s a working example:

def jordan(bot, update): chat_id = update.message.chat.id with open('JordanPeterson.jpg', 'rb') as jordan_picture: caption = "Jordan B. Peterson" bot.send_photo( chat_id, photo=jordan_picture, caption=caption, parse_mode=ParseMode.HTML ) 

And we can see that it works:

works

Update: Actually, both parse_mode=’html’ (as suggested by @slackmart) and parse_mode=’HTML’ that you used yourself work for me!

Another Update (as per your comment): You can use multiple tags. Here’s an example of one, with hyperlink , bold , and italic :

multiple tags

Yet Another Update: Regarding your comment:

. do I have any limitations on HTML tags? I can’t use something like or
to draw a line

try and find out

Now you’re trying to format the caption of an image, using HTML , meaning you’re formatting a text , so obviously, you can’t use «something like .» It has to be a «text formatting tag» (plus ). And not even all of them! I believe you can only use these: , , , and .

If you try to use a text-formatting tag like , it will give you this error:

Can’t parse entities: unsupported start tag «del» at byte offset 148

Which is a shame! I’d love to be able to do something like this in captions of images. or something like this!

Источник

How should I use parse_mode=’HTML’ in telegram python bot?

First, you need to import ParseMode from telegram like this:

from telegram import ParseMode 

Then, all you need is to specify parse_mode=ParseMode.HTML . Here’s a working example:

def jordan(bot, update): chat_id = update.message.chat.id with open('JordanPeterson.jpg', 'rb') as jordan_picture: caption = "Jordan B. Peterson" bot.send_photo( chat_id, photo=jordan_picture, caption=caption, parse_mode=ParseMode.HTML ) 

And we can see that it works:

works

Update: Actually, both parse_mode=’html’ (as suggested by @slackmart) and parse_mode=’HTML’ that you used yourself work for me!

Another Update (as per your comment): You can use multiple tags. Here’s an example of one, with hyperlink , bold , and italic :

multiple tags

Yet Another Update: Regarding your comment:

. do I have any limitations on HTML tags? I can’t use something like or
to draw a line

try and find out

Now you’re trying to format the caption of an image, using HTML , meaning you’re formatting a text , so obviously, you can’t use «something like .» It has to be a «text formatting tag» (plus ). And not even all of them! I believe you can only use these: , , , and .

If you try to use a text-formatting tag like , it will give you this error:

Can’t parse entities: unsupported start tag «del» at byte offset 148

Which is a shame! I’d love to be able to do something like this in captions of images. or something like this!

Solution 2

It works for me! Here’s the code I’m using:

>>> from telegram import Bot >>> tkn = '88888:199939393'; chid = '-31828' >>> bot = Bot(tkn) >>> with open('ye.jpeg', 'rb') as fme: . bot.send_photo(chid, fme, caption='Hallo', parse_mode='html') .

Of course, you must use your own telegram token and channel id. Also notice I’m using parse_mode=’html’ # lowercase

Источник

Оцените статью