Deploy: 2026-05-23 02:09:23
This commit is contained in:
@@ -17,14 +17,6 @@ class OTPController extends BaseController
|
|||||||
*/
|
*/
|
||||||
private function generateOtpImage(string $code): string
|
private function generateOtpImage(string $code): string
|
||||||
{
|
{
|
||||||
$dir = __DIR__ . '/../../public/otp';
|
|
||||||
if (!is_dir($dir)) {
|
|
||||||
mkdir($dir, 0755, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
$filename = 'otp_' . time() . '_' . rand(1000, 9999) . '.png';
|
|
||||||
$filepath = $dir . '/' . $filename;
|
|
||||||
|
|
||||||
if (function_exists('imagecreate')) {
|
if (function_exists('imagecreate')) {
|
||||||
// Base image
|
// Base image
|
||||||
$img = imagecreatetruecolor(400, 200);
|
$img = imagecreatetruecolor(400, 200);
|
||||||
@@ -74,21 +66,20 @@ class OTPController extends BaseController
|
|||||||
// Scale up the text image to the main image using resampled for smooth (non-pixelated) edges
|
// Scale up the text image to the main image using resampled for smooth (non-pixelated) edges
|
||||||
imagecopyresampled($img, $tmp, 0, 0, 0, 0, 400, 200, 100, 40);
|
imagecopyresampled($img, $tmp, 0, 0, 0, 0, 400, 200, 100, 40);
|
||||||
|
|
||||||
imagepng($img, $filepath);
|
ob_start();
|
||||||
|
imagepng($img);
|
||||||
|
$imageData = ob_get_clean();
|
||||||
|
|
||||||
imagedestroy($img);
|
imagedestroy($img);
|
||||||
imagedestroy($tmp);
|
imagedestroy($tmp);
|
||||||
|
|
||||||
|
return base64_encode($imageData);
|
||||||
} else {
|
} else {
|
||||||
// Fallback to placehold.co if PHP GD extension is missing
|
// Fallback to placehold.co if PHP GD extension is missing
|
||||||
$url = "https://placehold.co/400x200/e0f2fe/0f172a/png?text=" . $code . "&font=oswald";
|
$url = "https://placehold.co/400x200/e0f2fe/0f172a/png?text=" . $code . "&font=oswald";
|
||||||
$content = file_get_contents($url);
|
$content = file_get_contents($url);
|
||||||
file_put_contents($filepath, $content);
|
return base64_encode($content);
|
||||||
}
|
}
|
||||||
|
|
||||||
$appUrl = rtrim(getenv('APP_URL') ?: 'https://nabeh.intaleqapp.com', '/');
|
|
||||||
if (strpos($appUrl, 'localhost') !== false || strpos($appUrl, '127.0.0.1') !== false) {
|
|
||||||
$appUrl = 'https://nabeh.intaleqapp.com';
|
|
||||||
}
|
|
||||||
return $appUrl . '/otp/' . $filename;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -222,9 +213,9 @@ class OTPController extends BaseController
|
|||||||
// Send voice note
|
// Send voice note
|
||||||
ConversationFlowEngine::sendReply($session, $phone, '', null, $audioBase64, $mimeType);
|
ConversationFlowEngine::sendReply($session, $phone, '', null, $audioBase64, $mimeType);
|
||||||
} else if ($type === 'image') {
|
} else if ($type === 'image') {
|
||||||
// Generate OTP Image
|
// Generate OTP Image as Base64
|
||||||
$imageUrl = $this->generateOtpImage($code);
|
$imageBase64 = $this->generateOtpImage($code);
|
||||||
ConversationFlowEngine::sendReply($session, $phone, '', $imageUrl);
|
ConversationFlowEngine::sendReply($session, $phone, '', null, null, null, $imageBase64);
|
||||||
} else {
|
} else {
|
||||||
// Send text
|
// Send text
|
||||||
$textMsg = "رمز التحقق الخاص بك لمتجر نابه هو: *{$code}* \n الرجاء عدم مشاركته مع أي شخص.";
|
$textMsg = "رمز التحقق الخاص بك لمتجر نابه هو: *{$code}* \n الرجاء عدم مشاركته مع أي شخص.";
|
||||||
|
|||||||
@@ -213,7 +213,8 @@ class ConversationFlowEngine
|
|||||||
string $message,
|
string $message,
|
||||||
?string $mediaUrl = null,
|
?string $mediaUrl = null,
|
||||||
?string $audioBase64 = null,
|
?string $audioBase64 = null,
|
||||||
?string $mimetype = null
|
?string $mimetype = null,
|
||||||
|
?string $imageBase64 = null
|
||||||
): void {
|
): void {
|
||||||
$gatewayUrl = rtrim(getenv('WHATSAPP_GATEWAY_URL') ?: 'http://localhost:3722', '/');
|
$gatewayUrl = rtrim(getenv('WHATSAPP_GATEWAY_URL') ?: 'http://localhost:3722', '/');
|
||||||
if (substr($gatewayUrl, -4) === '/api') {
|
if (substr($gatewayUrl, -4) === '/api') {
|
||||||
@@ -239,6 +240,9 @@ class ConversationFlowEngine
|
|||||||
if ($mimetype !== null) {
|
if ($mimetype !== null) {
|
||||||
$payloadData['mimetype'] = $mimetype;
|
$payloadData['mimetype'] = $mimetype;
|
||||||
}
|
}
|
||||||
|
if ($imageBase64 !== null) {
|
||||||
|
$payloadData['image'] = $imageBase64;
|
||||||
|
}
|
||||||
|
|
||||||
$payload = json_encode($payloadData);
|
$payload = json_encode($payloadData);
|
||||||
|
|
||||||
|
|||||||
@@ -360,7 +360,7 @@ function convertToOggOpus(base64Audio) {
|
|||||||
/**
|
/**
|
||||||
* Send a message using an active session
|
* Send a message using an active session
|
||||||
*/
|
*/
|
||||||
async function sendMessage(session_key, phone, message, mediaUrl = null, audioBase64 = null, mimetype = null) {
|
async function sendMessage(session_key, phone, message, mediaUrl = null, audioBase64 = null, mimetype = null, imageBase64 = null) {
|
||||||
const sock = sessions.get(session_key);
|
const sock = sessions.get(session_key);
|
||||||
if (!sock) {
|
if (!sock) {
|
||||||
throw new Error(`Session ${session_key} is not active or connected`);
|
throw new Error(`Session ${session_key} is not active or connected`);
|
||||||
@@ -381,7 +381,13 @@ async function sendMessage(session_key, phone, message, mediaUrl = null, audioBa
|
|||||||
}
|
}
|
||||||
let sentMsg;
|
let sentMsg;
|
||||||
|
|
||||||
if (audioBase64) {
|
if (imageBase64) {
|
||||||
|
const buffer = Buffer.from(imageBase64, 'base64');
|
||||||
|
sentMsg = await sock.sendMessage(jid, {
|
||||||
|
image: buffer,
|
||||||
|
caption: message || ''
|
||||||
|
});
|
||||||
|
} else if (audioBase64) {
|
||||||
let finalAudioBase64 = audioBase64;
|
let finalAudioBase64 = audioBase64;
|
||||||
let finalMime = mimetype || 'audio/mp4';
|
let finalMime = mimetype || 'audio/mp4';
|
||||||
|
|
||||||
|
|||||||
@@ -84,18 +84,18 @@ app.get('/api/sessions/active', (req, res) => {
|
|||||||
|
|
||||||
// Send outbound message
|
// Send outbound message
|
||||||
app.post('/api/messages/send', async (req, res) => {
|
app.post('/api/messages/send', async (req, res) => {
|
||||||
const { session_key, phone, message, media_url, audio, mimetype } = req.body;
|
const { session_key, phone, message, media_url, audio, mimetype, image } = req.body;
|
||||||
|
|
||||||
if (!session_key || !phone) {
|
if (!session_key || !phone) {
|
||||||
return res.status(400).json({ error: 'Missing session_key or phone' });
|
return res.status(400).json({ error: 'Missing session_key or phone' });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!message && !audio && !media_url) {
|
if (!message && !audio && !media_url && !image) {
|
||||||
return res.status(400).json({ error: 'Missing message, audio or media_url' });
|
return res.status(400).json({ error: 'Missing message, audio, media_url, or image' });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await sendMessage(session_key, phone, message, media_url, audio, mimetype);
|
const result = await sendMessage(session_key, phone, message, media_url, audio, mimetype, image);
|
||||||
res.json({ status: 'success', data: result });
|
res.json({ status: 'success', data: result });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`Error sending message via ${session_key} to ${phone}:`, err);
|
console.error(`Error sending message via ${session_key} to ${phone}:`, err);
|
||||||
|
|||||||
Reference in New Issue
Block a user