Created
March 1, 2020 19:14
-
-
Save xTCry/e9a5a25d32632a8bc304ce09cbd07c16 to your computer and use it in GitHub Desktop.
[Bitrix] Simple notifications in Telegram bot about new reviews on the site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php"); | |
require_once("./tg.api.php"); | |
// get data from $_REQUEST | |
$P_NAME = 'XX Name'; | |
$P_TEXT = 'Ohh myy megga supperRrrandom review msg -> ' . rand(95454, 465464); | |
$P_STARS = 4; | |
CModule::IncludeModule('iblock'); | |
$el = new CIBlockElement; | |
$IBLOCK_TYPE = 'news'; // IB Artiicles content | |
$IBLOCK_ID = 30; // IB Artiicles content | |
$PROPERTY_NAME = 73; // Name | |
$PROPERTY_TEXT = 74; // Text review | |
$PROPERTY_STARS = 75; // Starts review | |
$PROPERTY_PM = 76; // Pass moderation - 0/1 (default: 0) | |
$fileds = [ | |
"IBLOCK_ID" => $IBLOCK_ID, | |
"NAME" => "New review from {$P_NAME}", | |
"PROPERTY_VALUES" => [ | |
$PROPERTY_NAME => ["VALUE" => $P_NAME], | |
$PROPERTY_TEXT => ["VALUE" => ["TEXT" => $P_TEXT, "TYPE" => "text"]], | |
$PROPERTY_STARS => ["VALUE" => $P_STARS], | |
$PROPERTY_PM => ["VALUE" => 0], | |
], | |
"DATE_ACTIVE_FROM" => date("d.n.Y H:i:s"), | |
]; | |
$newID = $el->Add($fileds); | |
if ($newID) { | |
$msg = ""; | |
$msg .= "New review from <i>{$P_NAME}</i>\n"; | |
// $msg .= "Start: <b>{$P_STARS}/5</b>\n"; | |
$msg .= "Text:\n<code>". htmlspecialchars($P_TEXT) ."</code>\n"; | |
$repluMarkup = [ | |
"inline_keyboard" => [ | |
[ | |
["text" => "View", "url" => "https://{$_SERVER["HTTP_HOST"]}/bitrix/admin/iblock_element_edit.php?IBLOCK_ID={$IBLOCK_ID}&type={$IBLOCK_TYPE}&ID=".$newID], | |
], | |
[ | |
["text" => "⭐ {$P_STARS}/5", "callback_data" => 'null'] | |
] | |
] | |
]; | |
echo "O"; | |
try { | |
$tgAPI->sendMessage("<CLIENT_ID>", $msg, $repluMarkup); | |
echo "K"; | |
} catch (Exception $e) { | |
// echo "[ERROR_TG]: ". $e->getMessage(); | |
} | |
} | |
else { | |
echo "Error: ".$el->LAST_ERROR; | |
} | |
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_after.php"); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TelegramAPI { | |
private $token = null; | |
function __construct($token = null) { | |
$this->token = $token; | |
} | |
public function sendMessage($chatID, $message, $replyMarkup = null) { | |
if(!$this->token) return false; | |
$url = 'https://api.telegram.org/bot' . $this->token . '/sendMessage'; | |
$params = [ | |
"chat_id" => $chatID, | |
"text" => ($message), | |
"parse_mode" => 'HTML' | |
]; | |
if ($replyMarkup) { | |
$params["reply_markup"] = (is_array($replyMarkup) ? json_encode($replyMarkup) : $replyMarkup); | |
} | |
$url .= '?'.http_build_query($params); | |
$ch = curl_init(); | |
$optArray = array( | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true | |
); | |
curl_setopt_array($ch, $optArray); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
} | |
$tgAPI = new TelegramAPI("<TG_TOKEN>"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment