Deploy: 2026-05-25 00:29:42
This commit is contained in:
44
backend/app/Services/MetaService.php
Normal file
44
backend/app/Services/MetaService.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
/**
|
||||
* Meta Service
|
||||
* Wraps communication with Facebook Graph API for Messenger and Instagram Business.
|
||||
*/
|
||||
class MetaService
|
||||
{
|
||||
/**
|
||||
* Send a text message response to Meta Graph API
|
||||
*/
|
||||
public static function sendMessage(string $pageAccessToken, string $recipientPsid, string $messageText): bool
|
||||
{
|
||||
$url = "https://graph.facebook.com/v20.0/me/messages?access_token=" . urlencode($pageAccessToken);
|
||||
|
||||
$payload = json_encode([
|
||||
'recipient' => ['id' => $recipientPsid],
|
||||
'messaging_type' => 'RESPONSE',
|
||||
'message' => ['text' => $messageText]
|
||||
]);
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json'
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
error_log("[MetaService Error] Failed to send message. HTTP Code: " . $httpCode . ", Response: " . $response);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user