Ross Mackie
Ross Mackie

Reputation: 11

Facebook chat webhook is no longer sending a JSON with the senders text - just their ID

I developed a very simple chat-bot for Facebook, where I saved the JSON file that Facebook sends into a new file, fb.txt. In this JSON one of the fields in the messaging array was the text that was sent from Facebook, but that no longer seems the case.

<?php

if (isset($_REQUEST['hub_challenge'])) {
    $challenge = $_REQUEST['hub_challenge'];
    $token = $_REQUEST['hub_verify_token'];
}

if($token == "macross93") {
    echo $challenge;
}

$input = json_decode(file_get_contents('php://input'), true);

$userID = $input['entry'][0]['messaging'][0]['sender']['id'];

$message = $input['entry'][0]['messaging'][0]['message']['text'];

echo $userID." and ".$message;
file_put_contents("fb.txt", file_get_contents($userID));

$access_token="EAAElJTd6foABAGhCjYcL3ikrPMFkOZBihqb5jzVZCuCnJd8oL4jFZAZCV26EYiOXGLzkz6lKZAMDXBKDsFwWFVYBVfSguV2ZCZCgGNCOLCxWWHQYC8of3DJks4AmEAVFo2DB29TajZBGQDUYtQqOWaig3M2m5ioeOElY7CPXZB3eEgAZDZD";

$url="https://graph.facebook.com/v2.8/me/messages?access_token=EAAElJTd6foABAGhCjYcL3ikrPMFkOZBihqb5jzVZCuCnJd8oL4jFZAZCV26EYiOXGLzkz6lKZAMDXBKDsFwWFVYBVfSguV2ZCZCgGNCOLCxWWHQYC8of3DJks4AmEAVFo2DB29TajZBGQDUYtQqOWaig3M2m5ioeOElY7CPXZB3eEgAZDZD";

$jsonData = "{
    'recipient': {
    'id': $userID
    },
    'message': {
        'text': 'finally, the bait is over'
    }
}";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

if(!empty($input['entry'][0]['messaging'][0]['message'])) {
    curl_exec($ch);
}

And the JSON I receive now is:

{
    "object": "page",
    "entry": [{
        "id": "632423650282074",
        "time": 1489785206037,
        "messaging": [{
            "sender": {
                "id": "892399990863615"
            },
            "recipient": {
                "id": "632423650282074"
            },
            "timestamp": 1489785206027,
            "read": {
                "watermark": 1489785205361,
                "seq": 0
            }
        }]
    }]
}

Any ideas how to read the message like I used to be able to?

Upvotes: 1

Views: 91

Answers (1)

Oranagwa Osmond Oscar
Oranagwa Osmond Oscar

Reputation: 326

That JSON response corresponds to the read event callback payload from Facebook when a user reads your message. See the documentation here.

What you are interested in is the message callback. Check your webhook setup to make sure you are subscribed to the messages event callback.

webhook event subscription

Upvotes: 1

Related Questions