325 lines
16 KiB
PHP
325 lines
16 KiB
PHP
<?php
|
|
/*error_reporting(E_ALL | E_STRICT);
|
|
ini_set('display_errors', 1);*/
|
|
|
|
/*if(isset($_GET['chat_id'])){
|
|
echo "Привет ".$_GET['chat_id'];
|
|
}*/
|
|
//set_time_limit(0);
|
|
|
|
//var_dump($_POST);
|
|
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
|
|
|
|
//Получаем chatId
|
|
if(isset($_GET['chat_id'])){
|
|
$chatId = $_GET['chat_id'];
|
|
$result['ok'] = 'false';
|
|
if($chatId > 0){
|
|
$result['ok'] = 'true';
|
|
$sql = "SELECT `id`, `telegramm_chat_id`, `agency_name`, `telegramm_kod`,`telegramm_notice`,`telegramm_chat_id`, `first_name`, `last_name`, `middle_name` FROM `users` WHERE `telegramm_chat_id` = ".$chatId;
|
|
$q = mysql_query($sql);
|
|
$row = mysql_num_rows($q);
|
|
if($row == 0){
|
|
if(isset($_GET['phone'])){
|
|
$phone = urlencode($_GET['phone']);
|
|
|
|
$sql_u = "SELECT `id`, `agency_name`, `first_name`, `last_name`,`telegramm_kod`,`telegramm_notice`,`telegramm_chat_id`, `middle_name` FROM `users` WHERE `phone` = '".$phone."'";
|
|
|
|
$q_u = mysql_query($sql_u);
|
|
$row_u = mysql_num_rows($q_u);
|
|
if($row_u == 0){
|
|
$result['user'] = 0;
|
|
$result['phone'] = 0;
|
|
} else {
|
|
$u = mysql_fetch_assoc($q_u);
|
|
mysql_query("UPDATE users SET telegramm_chat_id = {$chatId} WHERE id = ".$u['id']);
|
|
$result['user'] = trim($u['last_name'] . ' ' . $u['first_name'] . ' ' . $u['middle_name']);
|
|
$result['notice'] = 2;
|
|
|
|
|
|
|
|
/* $u = mysql_fetch_assoc($q_u);
|
|
mysql_query("UPDATE users SET telegramm_chat_id = {$chatId}, telegramm_notice = 1 WHERE id = ".$u['id']);
|
|
$result['user'] = trim($u['last_name'] . ' ' . $u['first_name'] . ' ' . $u['middle_name']);
|
|
$result['notice'] = 1;*/
|
|
}
|
|
} else {
|
|
$result['user'] = 0;
|
|
}
|
|
} else {
|
|
|
|
$r = mysql_fetch_assoc($q);
|
|
if($r['telegramm_notice'] == 0){
|
|
if($r['telegramm_kod'] == trim($_GET['phone'])){
|
|
mysql_query("UPDATE users SET telegramm_notice = 1 WHERE id = ".$r['id']);
|
|
$result['user'] = trim($r['last_name'] . ' ' . $r['first_name'] . ' ' . $r['middle_name']);
|
|
$result['notice'] = 1;
|
|
} else {
|
|
$result['notice'] = 3;
|
|
}
|
|
} else {
|
|
|
|
mysql_query("UPDATE users SET telegramm_notice = 1 WHERE id = ".$r['id']);
|
|
$result['user'] = trim($r['last_name'] . ' ' . $r['first_name'] . ' ' . $r['middle_name']);
|
|
$result['notice'] = 1;
|
|
}
|
|
}
|
|
}
|
|
echo json_encode($result, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
if(isset($_POST['send_mess'])){
|
|
$chatId = $_POST['chat_id'];
|
|
$result['ok'] = 'false';
|
|
if($chatId > 0){
|
|
$result['ok'] = 'true';
|
|
$sql = "SELECT `id`, `telegramm_chat_id`, `agency_name`, `telegramm_kod`,`telegramm_notice`,`telegramm_chat_id`, `first_name`, `last_name`, `middle_name` FROM `users` WHERE `telegramm_chat_id` = ".$chatId;
|
|
$q = mysql_query($sql);
|
|
$row = mysql_num_rows($q);
|
|
if($row == 0){
|
|
if(isset($_POST['text'])){
|
|
$phone = '+'.str_replace(['(', ')', ' ', '-', '+'], '', trim($_POST['text']));
|
|
|
|
$sql_u = "SELECT `id`, `agency_name`, `first_name`, `last_name`,`telegramm_kod`,`telegramm_notice`,`telegramm_chat_id`, `middle_name` FROM `users` WHERE `phone` = '".$phone."'";
|
|
|
|
$q_u = mysql_query($sql_u);
|
|
$row_u = mysql_num_rows($q_u);
|
|
if($row_u == 0){
|
|
$result['user'] = 0;
|
|
$result['phone'] = 0;
|
|
} else {
|
|
$u = mysql_fetch_assoc($q_u);
|
|
mysql_query("UPDATE users SET telegramm_chat_id = {$chatId} WHERE id = ".$u['id']);
|
|
$result['user'] = trim($u['last_name'] . ' ' . $u['first_name'] . ' ' . $u['middle_name']);
|
|
$result['notice'] = 2;
|
|
|
|
|
|
|
|
/* $u = mysql_fetch_assoc($q_u);
|
|
mysql_query("UPDATE users SET telegramm_chat_id = {$chatId}, telegramm_notice = 1 WHERE id = ".$u['id']);
|
|
$result['user'] = trim($u['last_name'] . ' ' . $u['first_name'] . ' ' . $u['middle_name']);
|
|
$result['notice'] = 1;*/
|
|
}
|
|
} else {
|
|
$result['user'] = 0;
|
|
}
|
|
} else {
|
|
|
|
$r = mysql_fetch_assoc($q);
|
|
if($r['telegramm_notice'] == 0){
|
|
if($r['telegramm_kod'] == trim($_POST['text'])){
|
|
mysql_query("UPDATE users SET telegramm_notice = 1 WHERE id = ".$r['id']);
|
|
$result['user'] = trim($r['last_name'] . ' ' . $r['first_name'] . ' ' . $r['middle_name']);
|
|
$result['notice'] = 1;
|
|
} else {
|
|
$result['notice'] = 3;
|
|
}
|
|
} else {
|
|
$sql_ev = "SELECT * FROM telegram_events WHERE chat_id = {$chatId} AND status = 0 ORDER BY id DESC LIMIT 1";
|
|
$q_ev = mysql_query($sql_ev);
|
|
$mess = str_replace(["\r\n", "\r", "\n"], ' ', mysql_real_escape_string($_POST['text']));
|
|
|
|
if(mysql_num_rows($q_ev) > 0){
|
|
$r_ev = mysql_fetch_assoc($q_ev);
|
|
if($r_ev['event'] == 'answer'){
|
|
$chatClass = new ChatsCianAvito();
|
|
$chatClass->setService($r_ev['service']);
|
|
$chatClass->send_answer($r_ev['mess_id'], $mess);
|
|
$description = mysql_real_escape_string($r_ev['description'] . ' ' . date('d.m.Y H:i:s') . ' - Отправлено сообщение: ' . $mess);
|
|
mysql_query("UPDATE telegram_events SET description = '{$description}', status = 1 WHERE id = {$r_ev['id']}");
|
|
$result['result'] = 'answer';
|
|
} else if ($r_ev['event'] == 'add_req'){
|
|
$description = mysql_real_escape_string($r_ev['description'] . ' ' . date('d.m.Y H:i:s') . ' - Отправлен текст: ' . $mess);
|
|
mysql_query("UPDATE telegram_events SET description = '{$description}' WHERE id = {$r_ev['id']}");
|
|
$chatClass = new ChatsCianAvito();
|
|
$chatClass->setService($r_ev['service']);
|
|
$result = $chatClass->add_req($r_ev['mess_id'],$chatId, $r_ev['stage'], $mess, $r_ev);
|
|
} else if ($r_ev['event'] == 'task_completed'){
|
|
$description = mysql_real_escape_string($r_ev['description'] . ' ' . date('d.m.Y H:i:s') . ' - Отправлен комментарий: ' . $mess);
|
|
mysql_query("UPDATE telegram_events SET description = '{$description}', status = 1 WHERE id = {$r_ev['id']}");
|
|
$telega = new Telegram();
|
|
$result = $telega->send_comment_task($chatId, $mess, $r_ev);
|
|
} else if ($r_ev['event'] == 'task_completed_object'){
|
|
$description = mysql_real_escape_string($r_ev['description'] . ' ' . date('d.m.Y H:i:s') . ' - Отправлен комментарий: ' . $mess);
|
|
mysql_query("UPDATE telegram_events SET description = '{$description}', status = 1 WHERE id = {$r_ev['id']}");
|
|
$telega = new Telegram();
|
|
$result = $telega->send_comment_task($chatId, $mess, $r_ev, 'object');
|
|
}
|
|
}
|
|
mysql_query("UPDATE users SET telegramm_notice = 1 WHERE id = ".$r['id']);
|
|
$result['user'] = trim($r['last_name'] . ' ' . $r['first_name'] . ' ' . $r['middle_name']);
|
|
$result['notice'] = 1;
|
|
}
|
|
}
|
|
}
|
|
echo json_encode($result, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
if(isset($_GET['delete_id'])){
|
|
$chatId = $_GET['delete_id'];
|
|
$sql = "UPDATE users SET `telegramm_notice` = 0, `telegramm_chat_id` = 0 WHERE `telegramm_chat_id` = ".$chatId;
|
|
mysql_query($sql);
|
|
}
|
|
|
|
if(isset($_GET['stop_id'])){
|
|
$chatId = $_GET['stop_id'];
|
|
$sql = "UPDATE users SET telegramm_notice = 0 WHERE `telegramm_chat_id` = ".$chatId;
|
|
mysql_query($sql);
|
|
}
|
|
//var_dump($_POST);
|
|
if(isset($_POST['request_bot'])){
|
|
|
|
$request = $_POST['request_bot'];
|
|
if(($_POST['service'] > 0 && $_POST['mess_id'] > 0) || (isset($_POST['event_id']) && $_POST['event_id'] > 0)){
|
|
$mess_id = 0; if(isset($_POST['mess_id'])) $mess_id = (int)$_POST['mess_id'];
|
|
$service = 0; if(isset($_POST['service'])) $service = (int)$_POST['service'];
|
|
$event_id = 0; if(isset($_POST['event_id'])) $event_id = (int)$_POST['event_id'];
|
|
switch ($request){
|
|
case 'read': {
|
|
$chatClass = new ChatsCianAvito();
|
|
$chatClass->setService($service);
|
|
echo json_encode($chatClass->update_read_message($mess_id), JSON_UNESCAPED_UNICODE);
|
|
break;
|
|
}
|
|
case 'answer': {
|
|
$chat_id = $_POST['chat_id'];
|
|
if($chat_id > 0){
|
|
$chatClass = new ChatsCianAvito();
|
|
$chatClass->setService($service);
|
|
echo json_encode($chatClass->query_send_answer($mess_id, $chat_id), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
case 'no_answer': {
|
|
$chat_id = $_POST['chat_id'];
|
|
if($chat_id > 0){
|
|
$chatClass = new ChatsCianAvito();
|
|
$chatClass->setService($service);
|
|
echo json_encode($chatClass->cancel_answer($mess_id, $chat_id), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'add_req': {
|
|
$chat_id = $_POST['chat_id'];
|
|
$stage = $_POST['stage'];
|
|
if($chat_id > 0){
|
|
$chatClass = new ChatsCianAvito();
|
|
$chatClass->setService($service);
|
|
echo json_encode($chatClass->add_req($mess_id, $chat_id, $stage), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
case 'add_funnel_req': {
|
|
$chat_id = $_POST['chat_id'];
|
|
$stage = $_POST['stage'];
|
|
if($chat_id > 0){
|
|
$chatClass = new ChatsCianAvito();
|
|
$chatClass->setService($service);
|
|
echo json_encode($chatClass->add_req($mess_id, $chat_id, $stage, $_POST['funnel_id']), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
case 'add_funnel_client': {
|
|
$chat_id = $_POST['chat_id'];
|
|
$stage = $_POST['stage'];
|
|
if($chat_id > 0){
|
|
$chatClass = new ChatsCianAvito();
|
|
$chatClass->setService($service);
|
|
echo json_encode($chatClass->add_req($mess_id, $chat_id, $stage, $_POST['funnel_id']), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
case 'no_add_req': {
|
|
$chat_id = $_POST['chat_id'];
|
|
if($chat_id > 0){
|
|
$chatClass = new ChatsCianAvito();
|
|
$chatClass->setService($service);
|
|
echo json_encode($chatClass->cancel_add_req($mess_id, $chat_id), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'task_completed': {
|
|
$chat_id = $_POST['chat_id'];
|
|
if($chat_id > 0){
|
|
$telega = new Telegram();
|
|
|
|
echo json_encode($telega->task_completed($chat_id, $event_id), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'task_completed_comment': {
|
|
$chat_id = $_POST['chat_id'];
|
|
if($chat_id > 0){
|
|
$telega = new Telegram();
|
|
|
|
echo json_encode($telega->task_completed($chat_id, $event_id, 'client_req', true), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'no_task_completed_comment': {
|
|
$chat_id = $_POST['chat_id'];
|
|
if($chat_id > 0){
|
|
$telega = new Telegram();
|
|
|
|
echo json_encode($telega->no_task_completed($chat_id, $event_id), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'task_completed_object': {
|
|
$chat_id = $_POST['chat_id'];
|
|
if($chat_id > 0){
|
|
$telega = new Telegram();
|
|
|
|
echo json_encode($telega->task_completed($chat_id, $event_id, 'object'), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'task_completed_comment_object': {
|
|
$chat_id = $_POST['chat_id'];
|
|
if($chat_id > 0){
|
|
$telega = new Telegram();
|
|
|
|
echo json_encode($telega->task_completed($chat_id, $event_id, 'object', true), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'no_task_completed_comment_object': {
|
|
$chat_id = $_POST['chat_id'];
|
|
if($chat_id > 0){
|
|
$telega = new Telegram();
|
|
|
|
echo json_encode($telega->no_task_completed($chat_id, $event_id, 'object'), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'accept_work': {
|
|
$chat_id = $_POST['chat_id'];
|
|
if($chat_id > 0){
|
|
$telega = new Telegram();
|
|
|
|
echo json_encode($telega->accept_work($chat_id, $event_id), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 'accept_work_object': {
|
|
$chat_id = $_POST['chat_id'];
|
|
if($chat_id > 0){
|
|
$telega = new Telegram();
|
|
|
|
echo json_encode($telega->accept_work($chat_id, $event_id, 'object'), JSON_UNESCAPED_UNICODE);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|