G
G
gnvs2021-05-17 11:25:31
1C-Bitrix
gnvs, 2021-05-17 11:25:31

Export news from the site to the Telegram channel?

Good time, I need to export the news feed from the site to the Telegram channel. I decided to do it through the OnAfterIBlockElementAdd event - i.e. When creating a new infoblock element (in my case, news), you need to export the preview text, picture, and link to the website news.

<?

define('TELEGRAM_TOKEN', 'Тут мой токен');
define('TELEGRAM_CHATID', 'Тут id моего паблика');

AddEventHandler("iblock", "OnAfterIBlockElementAdd", Array("MyAddElement", "OnAfterIBlockElementAddHandler"));

class MyAddElement
{

    function OnAfterIBlockElementAddHandler(&$arFields)
    {
        if($arFields["ID"]>0 && $arFields["IBLOCK_ID"] == 1)
             
        {

        $ch = curl_init();
            curl_setopt_array(
                $ch,
                array(
                    CURLOPT_URL => 'https://api.telegram.org/bot' . TELEGRAM_TOKEN . '/sendMessage',
                    CURLOPT_POST => TRUE,
                    CURLOPT_RETURNTRANSFER => TRUE,
                    CURLOPT_TIMEOUT => 10,
                    CURLOPT_POSTFIELDS => array(
                        'chat_id' => TELEGRAM_CHATID,
                        'text' => $arFields['PREVIEW_TEXT'],
                    ),
                )
            );

        curl_exec($ch);
         
      }
    }
}


This example sends a preview text perfectly, but I don’t understand how to give it a picture and a url. Tried several options from what came across in Google.

The debug command shows the picture field

<? echo "<pre>"; print_r($arFields); echo "</pre>";?>


but what about how to feed him, for example, this? Maybe there is a smarter option?

'picture' => $arFields['PREVIEW_PICTURE']['SRC'],

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2021-05-17
@anton99zel

'photo'     => new CURLFile(realpath("/path/to/image.png"))
и
CURLOPT_HTTPHEADER CURLOPT_URL => "Content-Type:multipart/form-data"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question