Joywork/cron/sendWhatsappMessages.php
2026-05-22 21:21:54 +03:00

70 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
set_time_limit(0);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Отсылка массовых сообщений Whatsapp через wazzap</title>
</head>
<body>
<?php
$sql = "SELECT * from whatsapp_list WHERE status = 'ready' AND wazzap_channel_id is not null ORDER BY id DESC";
$rez_list = mysql_query($sql);
while ($list = mysql_fetch_assoc($rez_list)) {
$listId = $list['id'];
$channelId = $list['wazzap_channel_id'];
$message = $list['text'];
$url = $list['url'];
$userId = $list['created_by'];
if ($message) {
$url = null;
}
$agencyId = User::getAgencyIdForUser($userId);
$sqlPhone = "SELECT * from whatsapp_list_phones WHERE whatsapp_list_id = $listId AND sended = 0 ORDER BY id DESC";
$rez_phone = mysql_query($sqlPhone);
while ($phone = mysql_fetch_assoc($rez_phone)) {
$phoneNum = $phone['phone'];
$phoneRecId = $phone['id'];
if ($phoneNum) {
$phoneNum = str_replace("+", "", $phoneNum);
$wazzap = new Wazzap($agencyId);
$result = $wazzap->sendMessage($channelId, $phoneNum, $message, $url, $phoneRecId);
if ($result === "done") {
$sqlUpd = "UPDATE whatsapp_list_phones SET sended = 1, error = NULL WHERE id = $phoneRecId";
mysql_query($sqlUpd);
echo "Отправлено " . $phoneNum;
} else {
$sqlUpd = "UPDATE whatsapp_list_phones SET error = '$result' WHERE id = $phoneRecId";
mysql_query($sqlUpd);
echo "Не отправлено " . $phoneNum . ", " . $result;
}
}
}
//проверяем, все ли отправлено в списке
$sqlPhoneCheck = "SELECT * from whatsapp_list_phones WHERE whatsapp_list_id = $listId AND sended = 0";
$rez_phone_cnt = mysql_query($sqlPhoneCheck);
if (mysql_num_rows($rez_phone_cnt) == 0) {
$sqlUpd = "UPDATE whatsapp_list SET status = 'done' WHERE id = $listId";
mysql_query($sqlUpd);
}
}
?>
</body>
</html>