Telegram bot html new line

Bot API 5.6

This object represents one special entity in a text message. For example, hashtags, usernames, URLs, etc.

Field Type Description
type String Type of the entity. Currently, can be “mention” ( @username ), “hashtag” ( #hashtag ), “cashtag” ( $USD ), “bot_command” ( /start@jobs_bot ), “url” ( https://telegram.org ), “email” ( do-not-reply@telegram.org ), “phone_number” ( +1-212-555-0123 ), “bold” (bold text), “italic” (italic text), “underline” (underlined text), “strikethrough” (strikethrough text), “spoiler” (spoiler message), “code” (monowidth string), “pre” (monowidth block), “text_link” (for clickable text URLs), “text_mention” (for users without usernames)

sendMessage

Use this method to send text messages. On success, the sent Message is returned.

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of sent messages from forwarding and saving

Formatting options

The Bot API supports basic formatting for messages. You can use bold, italic, underlined, strikethrough, and spoiler text, as well as inline links and pre-formatted code in your bots’ messages. Telegram clients will render them accordingly. You can use either markdown-style or HTML-style formatting.

Читайте также:  Php send response to post

Message entities can be nested, providing following restrictions are met:
— If two entities have common characters then one of them is fully contained inside another.
bold, italic, underline, strikethrough, and spoiler entities can contain and can be part of any other entities, except pre and code.
— All other entities can’t contain each other.

MarkdownV2 style

To use this mode, pass MarkdownV2 in the parse_mode field. Use the following syntax in your message:

*bold \*text* _italic \*text_ __underline__ ~strikethrough~ ||spoiler|| *bold _italic bold ~italic bold strikethrough ||italic bold strikethrough spoiler||~ __underline italic bold___ bold*
HTML style

To use this mode, pass HTML in the parse_mode field. The following tags are currently supported:

bold, bold italic, italic underline, underline strikethrough, strikethrough, strikethrough italic bold italic bold strikethrough  underline italic bold bold

forwardMessage

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the forwarded message from forwarding and saving

copyMessage

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendPhoto

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendAudio

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendDocument

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendVideo

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving
Читайте также:  Python list to byte array

sendAnimation

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendVoice

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendVideoNote

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendMediaGroup

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent messages from forwarding and saving

sendLocation

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendVenue

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendContact

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendPoll

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendDice

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding

sendSticker

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendInvoice

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

sendGame

Parameter Type Required Description
protect_content Boolean Optional Protects the contents of the sent message from forwarding and saving

Источник

Как сделать перенос строки в сообщении от Telegram Bot?

Не могу понять как сделать перенос строки в сообщениях от бота.

public function actionSendMessage() < file_get_contents('https://api.telegram.org/botMY_TOKEN' . '/sendMessage?chat_id=' . MY_CHAT_ID . '&text=' . urlencode('привет "\n" тебе')); >

A R: если пойдете в VK посылать сообщения из php, там используйте одинарные кавычки. (По стандарту, перенос \n должен работать только в двойных кавычках).

Сережа Ахен: А можно тогда еще вопрос. А как в этом же сообщении использовать жирный шрифт ?

попробовал и и * как указано в их документации, не срабатывает

A R: ‘parse_mode’ => ‘Markdown’ и **жирный**
или ‘parse_mode’ => ‘HTML’ и жирный

public function actionSendMessage() < file_get_contents('https://api.telegram.org/botTOKEN' . '/sendMessage?chat_id=' . CHAT_ID . '?parse_mode=Markdown&text=' . urlencode("*привет* \nтебе")); >

A R: вам бы теорию подтянуть. Параметр chat_id Вы указываете с вопросительным знаком, так как это первый параметр, далее же Вы добавляете (должны добавлять) параметры знаком & .

1BX_host

abdurohman

$data[‘parse_mode’] = ‘HTML’;
У меня настройка стоит форматирует как ХТМЛ и в итоге просто как обычный текст передается любое из значений. Все перепробовал!
А тег
вообще не работает.

Источник

‘PHP: Telegram Bot: Insert line break to text message

«\n» and «\r\n» , tested in text message sent by telegram bot, to create line break. Instead of showing line break, underline _ will appear after using them.

How I could printing line feed in telegram message sent by bot?

$txt = 'با تشکر از عضویت شما، هر روز حدود ساعت 10 شب یک ویدئوی جالب برای شما ارسال خواهد شد.'; $txt .= " \n "; $txt .= 'Thanks for joining, Every day at almost 18:30 GMT an intersting video will be sent'; 

enter image description here

Message Demo

Any help will be appreciated.

Solution 1: [1]

There is a better way! The problem is because of URL encodings. You can use normal PHP text using \n but by passing it to urlencode method, as follows:

$txt = urlencode("here is my text.\n and this is a new line \n another new line"); 

Solution 2: [2]

1) If you develop your code in Windows/Linux OS, you can simply use enter in text:

$text = 'test 123 another text'; 

2) If your code run on Windows/Linux server, you can use PHP_EOL constant instead of \n :

$text = 'text 123 '.PHP_EOL.'yet another text'; 

3) And if you search for an OS independent soloution, you can use %0A or chr(10) for this purpose:

$text = 'text 123 '.chr(10).'yet another text'; 

Solution 3: [3]

For future visitor just I quote @Dagon answer in comments:

Using %0A will make line feed in telegram messages

Solution 4: [4]

After reading and trying all of these answers, I just wanted to post my own solution. I have an application in Laravel 5.8 that sends the reservation both by e-mail and a Telegram message.

$telegramMessage = "Reservation Request\n". 'Name: ' . $reservation->reserv_name . "\n". 'E-mail: email . '"> ' . $reservation->email . "\n". 'Phone: ' . $reservation->phone . "\n". 'Reservation Date/Time: ' . $reservation->reserv_date_time->format('d-m-Y H:i') . "\n". 'Number of people: ' . $reservation->number_of_people . "\n". 'Message: ' . $reservation->reserv_message . "\n"; Telegram::sendMessage([ 'chat_id' => env('TELEGRAM_CHAT_ID', ''), 'parse_mode' => 'HTML', 'text' => $telegramMessage, ]); 

More or less I have used all the html tags that Telegram API allows. You should pay attention \n must be in double quotes.

Solution 5: [5]

You can use %0A instead of \n.

Solution 6: [6]

it may be not show result as you wants in Unicode languages like Persian! you can prepare your text and use this:

$txt = implode("\n", explode('\n', $txt)); 

Solution 7: [7]

To avoid coding and encoding issues, I used this simple solution in my code:

  • First, I send my text message in HTML format by setting the parse_mode=HTML argument in the «sendMessage» URL.
  • Then, I insert the following code for each line break:
    \n
. sendMessage?parse_mode=HTML&text=". paragraph1
\n

paragraph2 . "

Of course the text variable was curl escaped before appended to the URL:

$text = curl_escape($handle, $text); 

Solution 8: [8]

All these answers are at the same time «right» and «wrong». In fact in depend a lot of the input you have. For example if you have a text area for input and then send the content to Telegram, if the user write in the text area and press return, the text in Telegram will be

Performing URL encode will change nothing. After struggling a lot I discover a conflict with the fact sending the text area data from a page to another page escape some data.

The way I solve that is to remplace the escaped \n by a non-escaped one. So:

 $my_msg = str_replace("\\n","\n",$my_msg); 

It works on Mac, PC and so on with text from text area.

Solution 9: [9]

$txt = "Thanks for joining, Every day at
almost 18:30 GMT an intersting video will be sent";
$txt = "Thanks for joining, Every day \r\n at almost 18:30 GMT an intersting video will be sent"; 

Solution 10: [10]

I solved the problem in this way :

$txt = 'aaaaaaaaa\nnew line1 \nnewline2'; $parameters = array('chat_id' => $chatId, "text" => $txt); $parameters["method"] = "sendMessage"; echo json_encode($parameters); 

Solution 11: [11]

you should use urlencode to solve this problem:

$text ="sth sth sth sth"; $text = urlencode($text); 

Solution 12: [12]

for me this solution works: use double quotation mark

$message='Hi' $message=$message."\n"; $message=$message.'Guys' 

Solution 13: [13]

try something like this, its work for me, and you need to add parameter parse_mode.

$text = "exampletest \n example" 
$text1 = 'example'; $text2 = 'next'; $data = $text1 . "\n" . $text2; 

Solution 14: [14]

It it easy just copy break line from anywhare to pass in code. enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add new line or HTML in reply. #988

How to add new line or HTML in reply. #988

Comments

❓ Support Question

Summary

How can I add new line character in response to messages. Or other HTML tags as well e.g. bold, italics etc.

The text was updated successfully, but these errors were encountered:

@noplanman thanks for quick reply. I didn’t find this parse_mode in readme.

I can send PR for readme to include this example if you wish to.

Thanks for the kind suggestion, but the main documentation for the bot API you should consult is: https://core.telegram.org/bots/api

It doesn’t make sense to duplicate that documentation and list everything in the readme.

Thanks for the kind suggestion, but the main documentation for the bot API you should consult is: https://core.telegram.org/bots/api

It doesn’t make sense to duplicate that documentation and list everything in the readme.

I just meant an example that shows parse_mode only. Not whole documentation 😁

What about all the other parameters for all the other methods?
This library is simply a wrapper for the official API, all specifics should be looked for there 👍

You might be interested in exploring the example-bot repo too, which has a few commands that make use of a bunch of features that this library provides.

Источник

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