Views: 18619
Last Modified: 12.09.2024

Message text can be formatted by applying bold or strikethrough script or quote style. This lesson will show how to use user commands and REST API to format chat messages.

Note: restCommand function is used here for illustration purposes only. It is taken from the EchoBot example; it is used. You can send a REST command with your own function, or use the BX24.callMethod or bitrix24-php-sdkmethods.


Formatting Tags

Consider the following example:

[B]bold[/B] text
[U]underline[/U] text
[I]oblique[/I] text
[S]strikethrough[/S] text

When posted to the chat, the message will be rendered like this:

Here's how we can use REST API to format the message:

restCommand('imbot.message.add', Array(
   "DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
   "MESSAGE" => "[B]bold[/B] text
[U]underline[/U] text
[I]oblique[/I] text
[S]strikethrough[/S] text",
), $_REQUEST["auth"]);

Line Break

To add a line break, add one of these sequences to the code:

[BR]
# BR # (no spaces)
\n

Any of these codes create a line break:

Using line breaks in a REST API call:

restCommand('imbot.message.add', Array(
   "DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
   "MESSAGE" => "First line[BR]Second line",
), $_REQUEST["auth"]);

Quotes

To format text as a quote, use a double closing angle bracket sequence or enclose text in two lines formed by the minus sign:

>>first quote line
>>second quote line

------------------------------------------------------
John Doe [08.04.2016 13:06:49]
Hello everyone!
------------------------------------------------------

The second method produces a result that looks a bit different because the first line contains the author name and time stamp:

Creating a quote using a REST API call:

restCommand('imbot.message.add', Array(
   "DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
   "MESSAGE" => ">>first quote line
>>second quote line

------------------------------------------------------
John Doe [08.04.2016 13:06:49]
Hello everyone!
------------------------------------------------------",
), $_REQUEST["auth"]);

Hyperlinks

Any text that conforms to the hyperlink pattern will be converted to a hyperlink. If the hyperlink URL resolves to a page containing rich formatting, it will be recognized by the system and will be added to the message:

Adding a hyperlink using a REST API call:

restCommand('imbot.message.add', Array(
   "DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
   "MESSAGE" => "http://bitrix24.com",
), $_REQUEST["auth"]);

Note: To disable rich formatting, use 'URL_PREVIEW' => 'N' key when adding a message:
restCommand('imbot.message.add', Array(
"DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
"MESSAGE" => "http://bitrix24.com",
'URL_PREVIEW' => 'N' 
), $_REQUEST["auth"]);


If the message contains a link to an image file http://shelenkov.com/bitrix/images/mantis.jpg (with a .png, .jpg or .gif extension), it will be posted as an image:


A BB code URL is also supported - [URL=http://bitrix24.com]Link to Bitrix24[/URL]:

This code uses REST API to add a BB URL code to the message:

restCommand('imbot.message.add', Array(
   "DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
   "MESSAGE" => "[URL=http://bitrix24.com]Link to Bitrix24[/URL]",
), $_REQUEST["auth"]);

IM supports special codes similar to the URL code.

  • [USER=5]Alexandra[/USER] - adds a link to a user.
  • [CALL=18012334455]call[/CALL] - adds a button to make a call using Bitrix24.
  • [CHAT=12]link[/CHAT] - adds a chat link.

These codes are also supported by REST API:

restCommand('imbot.message.add', Array(
   "DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
   "MESSAGE" => "[USER=5]Alexandra[/USER]
[CALL=18012334455]call[/CALL]
[CHAT=12]link[/CHAT]",
), $_REQUEST["auth"]);

Indents

Use the tab character to indent the text:

Using indents in a REST API call:

restCommand('imbot.message.add', Array(
   "DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
   "MESSAGE" => "
Indent
   Indent
      Indent",
), $_REQUEST["auth"]);

Active Links (commands)

To have a link send some text to the bot, use the SEND tag:

[send=text]button caption[/send] - sends text to the bot.

You can use this tag to force a user to send a hard-coded text to the bot. However, a preferable method is by using the Keyboards

Adding a command using REST API:

restCommand('imbot.message.add', Array(
   "DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
   "MESSAGE" => "[send=text]button caption[/send] - sends text to the bot",
), $_REQUEST["auth"]);

If additional user input is required by the command, use the PUT tag:

[put=/search]Enter search text[/put]

Use this tag to automatically add the command text to the input field. Once a user clicks the PUT link, the text that is specified after the equal sign will be added to the text input field. Now the user has to enter additional information and click the Send button. Again, a preferable method is by using the Keyboards)

Using REST API to prompt the user to enter text:

restCommand('imbot.message.add', Array(
   "DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
   "MESSAGE" => "[put=/search]Enter search text[/put]",
), $_REQUEST["auth"]);

Attachments

Use attachments to make a message more pronounced and comprehensive.

To embed an attachment to the message text, add a special BB code ATTACH to the message specified in the MESSAGE key. The [ATTACH=1] code will be replaced with the attachment when showing the message. If no ATTACH code is specified in the message text while the actual attachment exists, it will be shown at the end of the message.

restCommand('imbot.message.add', Array(
   "DIALOG_ID" => $_REQUEST['data']['PARAMS']['DIALOG_ID'],
   "MESSAGE" => "Message with middle attach [ATTACH=1] from bot",
   "ATTACH" => Array(
      "ID" => 1,
      "COLOR" => "#29619b",
      "BLOCKS" => Array(
         Array("LINK" => Array(
            "NAME" => "Ticket #12345: new IM module for API",
            "DESC" => "Need this done before the release!",
            "LINK" => "https://api.bitrix24.com/"
         )),
         Array("IMAGE" => Array(
            "NAME" => "Example",
            "LINK" => "https://training.bitrix24.com/bitrix/templates/b24_en_new/img/en/main/logo_rich.png",
         ))
      )
   )
), $_REQUEST["auth"]);

Note For detailed description of extended attachments, please refer to this lesson.





Courses developed by Bitrix24