@hecby_v, <?php
$auth_token = "435435435";
$send_name = "vcdvcbf";
$is_log = true;
$host = '543t';
$dbname = '43543';
$user = '432432';
$pass = '4324';
$db_order = new PDO('mysql:host=' . $host . ';dbname=' . $dbname . '', $user, $pass);
$db_order->exec("set names utf8");
if (!$db_order) {
exit('Ошибка подключения к базе данных');
}
function put_log_in($data)
{
global $is_log;
if($is_log) {file_put_contents("logs/tmp_in.txt", $data."\n", FILE_APPEND);}
}
function put_log_out($data)
{
global $is_log;
if($is_log) {file_put_contents("logs/tmp_out.txt", $data."\n", FILE_APPEND);}
}
function sendReq($data)
{
$request_data = json_encode($data);
put_log_out($request_data);
//here goes the curl to send data to user
$ch = curl_init("https://chatapi.viber.com/pa/send_message");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if($err) {return $err;}
else {return $response;}
}
function sendMsg($sender_id, $text, $type, $tracking_data = Null, $arr_asoc = Null)
{
global $auth_token, $send_name;
$data['auth_token'] = $auth_token;
$data['receiver'] = $sender_id;
if($text != Null) {$data['text'] = $text;}
$data['type'] = $type;
//$data['min_api_version'] = $input['sender']['api_version'];
$data['sender']['name'] = $send_name;
//$data['sender']['avatar'] = $input['sender']['avatar'];
if($tracking_data != Null) {$data['tracking_data'] = $tracking_data;}
if($arr_asoc != Null)
{
foreach($arr_asoc as $key => $val) {$data[$key] = $val;}
}
return sendReq($data);
}
function sendMsgText($sender_id, $text, $tracking_data = Null)
{
return sendMsg($sender_id, $text, "text", $tracking_data);
}
$request = file_get_contents("php://input");
$input = json_decode($request, true);
put_log_in($request);
$type = $input['message']['type']; //тип полученного сообщения (текст / картинка)
$text = $input['message']['text']; //фактическое сообщение, отправленное пользователем
$sender_id = $input['sender']['id']; //уникальный идентификатор пользователя, отправившего сообщение
$sender_name = $input['sender']['name']; //имя пользователя, отправившего сообщение
if($input['event'] == 'webhook')
{
$webhook_response['status'] = 0;
$webhook_response['status_message'] = "ok";
$webhook_response['event_types'] = 'delivered';
echo json_encode($webhook_response);
die;
}
else if($input['event'] == "subscribed")
{
sendMsgText($sender_id, "Спасибо, что подписались на нас!");
}
else if($input['event'] == "conversation_started")
{
sendMsgText($sender_id, "Беседа началась!");
}
elseif($input['event'] == "message")
{
elseif ($input['message']['text'] == 'Да'){
sendMsg($sender_id, 'Ура', $type);
}
elseif ($input['message']['text'] == 'Нет') {
sendMsg($sender_id, 'Жаль', $type);
}
else{
sendMsg($sender_id, 'Неизвестная команда!', $type);
}
}
?>
elseif ($input['message']['text'] == 'Да'){
sendMsg($sender_id, 'Ура', $type);
$queryUrl = 'https://{адрес вашего битрикс24}/rest/{токен, который вы получите, создав в битриксе входящий вебхук}/crm.deal.update/';
$queryData = [
'id' => {Ид сделки},
'fields' => [
'STAGE_ID' => "Стадия"
]
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $queryUrl,
CURLOPT_POSTFIELDS => $queryData,
));
$result = curl_exec($curl);
if($result === false){
echo 'Ошибка CURL: ' . curl_errno($curl);
}
$result = json_decode($result, 1);
return $result;
}
$bitrix24GetData = array(
'LOGIN' => $bitrix24login,
'PASSWORD' => $bitrix24password,
'TITLE' => "Заказ №".$arr_order[$i],
'NAME' => $data['firstname'],
'LAST_NAME' => $data['lastname'],
'PHONE_OTHER' => $data['telephone'],
'OPPORTUNITY' => $data['total'],
'CURRENCY_ID' => 'UAH',
'SOURCE_ID' => 'WEB',
'SOURCE_DESCRIPTION' => $data['store_url'],
'COMMENTS' => $stackBitrix[$key].$bitrix24data
);
if (!empty($bitrix24email)) { $bitrix24GetData['EMAIL_OTHER'] = $bitrix24email; }
if (!empty($bitrix24address)) { $bitrix24GetData['ADDRESS'] = $bitrix24address; }
$bitrix24GetData = http_build_query($bitrix24GetData);
file_get_contents("https://".$bitrix24domen."/crm/configs/import/lead.php?".$bitrix24GetData);