query($sql); $r = $pdo->fetch_assoc($q); $user_id = 0; if($type == 'client'){ if($r['confirm'] == 0 || $r['who_work'] == 0){ $user_id = (int)$r['id_agent']; } else { $user_id = (int)$r['who_work']; } } else if($type == 'req'){ if($r['confirm'] == 0 || $r['who_work'] == 0){ $user_id = (int)$r['user_id']; } else { $user_id = (int)$r['who_work']; } } else if($type == 'object'){ $user_id = (int)$r['id_add_user']; } if($user_id > 0){ try{ $user = $pdo->fetch_assoc($pdo->query("SELECT * FROM users WHERE id = {$user_id}")); } catch (Exception $e) { echo $pdo->errorInfo(); } } if($user){ //var_dump($user); if($user['telegramm_notice'] == 1 && $user['telegramm_chat_id'] > 0){ $chatId = $user['telegramm_chat_id']; if($chatId > 0){ $phone = ''; if(isset($r['phone'])){ $phone = $r['phone']; if (strpos($phone, '+7') !== false) { $phone = substr($phone, 0, 2) . "(" . substr($phone, 2, 3) . ")" . substr($phone, 5, 3) . "-" . substr($phone, 8, 2) . "-" . substr($phone, 10, 2); } } $name = $contracts_type[$contract['type']]." №".$contract['number']." от ".date('d.m.Y',$contract['date_start']). " по ".date('d.m.Y', $contract['date_end']); $text = "Истекает срок договора\n"; $text .= $name . "\n"; if($type == 'client'){ $text .= "по клиенту " . $r['fio'] . " тел. " . $phone."\n"; } else if($type == 'req'){ $text .= "по заявке " .$r['name'] . "\n"; } else if($type == 'object'){ $text .= "по объекту ID:" . $r['id'] . " название: " . $r['nazv'] . "\n"; $text .= "адрес: " . $r['adres'] . "\n"; } } } } return array('text' => $text, 'chat_id' => $chatId); } const TOKEN = '793676666:AAGFo66CjsmO2KxZVRwQbH8pLTvenpsHiHI'; const BASE_URL = 'https://api.telegram.org/bot' . TOKEN . '/'; require_once $_SERVER['DOCUMENT_ROOT'] . "/telegramm/vendor/autoload.php"; header('Content-Type: application/json'); $bot = new \TelegramBot\Api\Client(TOKEN); if(date("H") == 12){ $pdo = new MysqlPdo(hst, ndb, user, pass); $sql = "SET NAMES 'utf8'"; $pdo->query($sql); $contracts_type = array(); $sql = "SELECT * FROM `contract_types`"; $q = $pdo->query($sql); while($r = $pdo->fetch_assoc($q)){ $contracts_type[$r['id']] = $r['type']; } $time_expired = (strtotime(date("d.m.Y 00:00:00")) - 3*86400); //Договора Клиентов $sql = "SELECT * FROM contracts_client WHERE send = 0"; $q = $pdo->query($sql); while($r = $pdo->fetch_assoc($q)){ if($r['date_end'] < $time_expired){ $sql_up = "UPDATE contracts_client SET send = 1 WHERE id = {$r['id']}"; $pdo->query($sql_up, true); } else if(($r['date_end'] - time())/86400 < (int)$r['days']){ $res = get_text_mes($r, 'client', $contracts_type); $text = $res['text']; $chatId = (int)$res['chat_id']; // echo $text; if ($text != '' && $chatId > 0) { try { $bot->sendMessage($chatId, $text, 'html'); } catch (Exception $e) { echo 'Выброшено исключение: ', $e->getMessage(), "\n"; // file_put_content($_SERVER['DOCUMENT_ROOT']."/".telegramm_error.log.$e->getMessage(), ) } mysql_query("UPDATE contracts_client SET send = 1 WHERE id = " . $r['id']); } } } //Договора Заявок $sql = "SELECT * FROM contracts_req WHERE send = 0"; $q = $pdo->query($sql); while($r = $pdo->fetch_assoc($q)){ if($r['date_end'] < $time_expired){ $sql_up = "UPDATE contracts_req SET send = 1 WHERE id = {$r['id']}"; $pdo->query($sql_up, true); } else if(($r['date_end'] - time())/86400 < (int)$r['days']){ $res = get_text_mes($r, 'req', $contracts_type); $text = $res['text']; $chatId = (int)$res['chat_id']; //echo $text; if ($text != '' && $chatId > 0) { try { $bot->sendMessage($chatId, $text, 'html'); } catch (Exception $e) { echo 'Выброшено исключение: ', $e->getMessage(), "\n"; // file_put_content($_SERVER['DOCUMENT_ROOT']."/".telegramm_error.log.$e->getMessage(), ) } mysql_query("UPDATE contracts_req SET send = 1 WHERE id = " . $r['id']); } } } //Договора объектов $sql = "SELECT * FROM contracts_object WHERE send = 0"; $q = $pdo->query($sql); while($r = $pdo->fetch_assoc($q)){ if($r['date_end'] < $time_expired){ $sql_up = "UPDATE contracts_object SET send = 1 WHERE id = {$r['id']}"; $pdo->query($sql_up, true); } else if(($r['date_end'] - time())/86400 < (int)$r['days']){ $res = get_text_mes($r, 'object', $contracts_type); $text = $res['text']; $chatId = (int)$res['chat_id']; //echo $text; if ($text != '' && $chatId > 0) { try { $bot->sendMessage($chatId, $text, 'html'); } catch (Exception $e) { echo 'Выброшено исключение: ', $e->getMessage(), "\n"; // file_put_content($_SERVER['DOCUMENT_ROOT']."/".telegramm_error.log.$e->getMessage(), ) } mysql_query("UPDATE contracts_object SET send = 1 WHERE id = " . $r['id']); } } } } $bot->run();