Reputation: 157
This is not a duplicated question. I want to use html codes in my telegram bot that is written by c#. I searched in SO but I did not find any answer. How to do that? I used TelegramBotSharp. Here is my code related to the part that I explained:
MessageTarget target = (MessageTarget)update.Chat ?? update.From;
if(Text.StartsWith("Hello")) {
bot.SendMessage(target, "Hello <a href='http://google.com'> dear</a>", true);
}
Upvotes: 15
Views: 57437
Reputation: 1
from bs4 import BeautifulSoup
def for_telegrtam_html_text_normalizer(text):
soup = BeautifulSoup(text, 'lxml')
text = soup.get_text().replace('<', '≺').replace('>', '≻')
to_set_tags = ['b', 'strong', 'i', 'em', 'u', 'ins', 's', 'strike', 'del', 'tg-spoiler', 'a', 'code', 'pre', 'blockquote', 'blockquote expandable', 'tg-emoji', ]
for tag in to_set_tags:
if tag == 'a':
elems = soup.find_all(tag, href=True)
else:
elems = soup.find_all(tag)
for elem in elems:
if tag == 'a':
text = text.replace(elem.text, f'<{tag} href="{elem["href"]}">{elem.text}</{tag}>')
elif tag == 'tg-emoji':
try:
text = text.replace(elem.text, f'<{tag} emoji-id="{elem["emoji-id"]}">{elem.text}</{tag}>')
except:
pass
elif tag == 'span':
try:
cl_name = elem['class'][0]
if cl_name == "tg-spoiler":
text = text.replace(elem.text, f'<{tag} class="{cl_name}">{elem.text}</{tag}>')
except:
pass
elif tag == 'code':
try:
cl_name = elem['class'][0]
except:
cl_name = None
if cl_name:
text = text.replace(elem.text, f'<{tag} class="{cl_name}">{elem.text}</{tag}>')
else:
text = text.replace(elem.text, f'<{tag}>{elem.text}</{tag}>')
else:
text = text.replace(elem.text, f'<{tag}>{elem.text}</{tag}>')
return text
Upvotes: -3
Reputation: 16076
If you need to format using html you need to set "parse_mode" as "html";
https://api.telegram.org/bot1328912345:BBCCDDExVAKD2GOEA1mXWfXfhQ_z8Y6rRh8/sendmessage?chat_id=12345678&text=Sample message&parse_mode=html)
As of now only following tags are supported by Bot:
<b>bold</b>, <strong>bold</strong>
<i>italic</i>, <em>italic</em>
<u>underline</u>, <ins>underline</ins>
<s>strikethrough</s>, <strike>strikethrough</strike>, <del>strikethrough</del>
<b>bold <i>italic bold <s>italic bold strikethrough</s> <u>underline italic bold</u></i> bold</b>
<a href="http://www.example.com/">inline URL</a>
<a href="tg://user?id=123456789">inline mention of a user</a>
<code>inline fixed-width code</code>
<pre>pre-formatted fixed-width code block</pre>
<pre><code class="language-python">pre-formatted fixed-width code block written in the Python programming language</code></pre>
Source: https://core.telegram.org/bots/api#formatting-options
Upvotes: 8
Reputation: 1790
Use the following syntax in your message:
*bold text*
_italic text_
[inline URL](http://www.example.com/)
[inline mention of a user](tg://user?id=123456789)
pre-formatted fixed-width code block
Note: Only the tags mentioned above are currently supported.
Tags must not be nested.
All <, > and & symbols that are not a part of a tag or an HTML entity must be replaced with the corresponding HTML entities (< with <, > with > and & with &).
All numerical HTML entities are supported.
The API currently supports only the following named HTML entities: <, >, & and ".
Upvotes: 10