Answer the question
In order to leave comments, you need to log in
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);
}
}
}
<? echo "<pre>"; print_r($arFields); echo "</pre>";?>
'picture' => $arFields['PREVIEW_PICTURE']['SRC'],
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question