Skip to content

Instantly share code, notes, and snippets.

@Anders87x
Created June 13, 2024 02:30
Show Gist options
  • Select an option

  • Save Anders87x/09cd115a7638f8d766746f5cab6a25b8 to your computer and use it in GitHub Desktop.

Select an option

Save Anders87x/09cd115a7638f8d766746f5cab6a25b8 to your computer and use it in GitHub Desktop.
<?php
const TOKEN_ANDERCODE = "ANDERCODEPHPAPIMETA2";
const WEBHOOK_URL = "https://anderson-bastidas.com/webhook.php";
function recibirMensajes($req, $res) {
try {
$entry = $req['entry'][0];
$changes = $entry['changes'][0];
$value = $changes['value'];
$mensaje = $value['messages'][0];
$comentario = $mensaje['text']['body'];
$numero = $mensaje['from'];
$res->header('Content-Type: application/json');
$res->status(200)->send(json_encode(['message' => 'EVENT_RECEIVED']));
} catch (Exception $e) {
$res->header('Content-Type: application/json');
$res->status(200)->send(json_encode(['message' => 'EVENT_RECEIVED']));
}
}
if ($_SERVER['REQUEST_METHOD']==='POST'){
$input = file_get_contents('php://input');
$data = json_decode($input,true);
recibirMensajes($data,http_response_code());
}else if($_SERVER['REQUEST_METHOD']==='GET'){
if(isset($_GET['hub_mode']) && isset($_GET['hub_verify_token']) && isset($_GET['hub_challenge']) && $_GET['hub_mode'] === 'subscribe' && $_GET['hub_verify_token'] === TOKEN_ANDERCODE){
echo $_GET['hub_challenge'];
}else{
http_response_code(403);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment