Updated on 2025-11-13 GMT+08:00

Receiving and Processing MO Messages Sent on Facebook

Function

This interface is invoked to receive and process MO messages sent on Facebook.

Usage Description

  • Prerequisites

    The authentication is successful, and the connection to the CC-Messaging is successful.

  • For details, visit official document at https://developers.facebook.com/.

Interface Authentication

Obtain the authentication character string in the request header X-Hub-Signature-XX and compare the parameter in the request with the encrypted secretKey in the channel configuration. If they are the same, the authentication is successful.
        String algorithm = channelConfig.getAlgorithm();
        String signature = "";
        if (AlgorithmEnum.SHA256.getValue().equals(algorithm)) {
            String signatureHeader = request.getHeader("X-Hub-Signature-256");
            // Verify signatureHeaderSha256.
            if (StringUtils.isTrimEmpty(signatureHeader)) {
                log.error("sha256 Signature from facebook is empty");
                return false;
            }
            signature = StringUtils.substringAfter(signatureHeader, "sha256=");
        } else {
            String signatureHeader = request.getHeader();
            // Verify signatureHeader.
            if (StringUtils.isTrimEmpty(signatureHeader)) {
                log.error("sha1 Signature from facebook is empty");
                return false;
            }
            signature = StringUtils.substringAfter(signatureHeader, "sha1=");
        }
        String hash;
        try {
            hash = getSignatureByAlgorithm(payloadContent, secretKey, channelConfig.getAlgorithm());
        } catch (Exception e) {
            log.error("Fail to get the signature, and the exception is {}", e.getMessage());
            return false;
        }
        return StringUtils.equals(hash, signature);

The SHA1 encryption algorithm has security risks. Exercise caution when using this algorithm.

Interface Prototype

Table 1 Interface prototype description

Method

POST

URL

https://{IP address}:{Port number}/social/on/facebook/{channelId}

{IP address}:{Port number} indicates the IP address and port number of the NSLB exposed externally.

{channelId} indicates the channel ID.

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory or Not

Type

Description

Content-Type

Yes

String

The value is fixed at application/json; charset=UTF-8.

X-Hub-Signature-XX

Yes

String

Facebook authentication information.

Table 3 Request body parameters

Parameter

Mandatory or Not

Type

Description

object

Yes

String

Request object structure.

entry

Yes

JSON array

entry contains event data. For details, see Table 4.

Table 4 entry parameters

Parameter

Mandatory or Not

Type

Description

id

Yes

String

Home page ID.

time

Yes

Long

Request time.

messaging

Yes

JSON array

Messaging webhook event. For details, see Table 5.

Table 5 messaging parameters

Parameter

Mandatory or Not

Type

Description

message

Yes

JSON object

Event content. For details, see Table 6.

sender

Yes

JSON object

Sender ID, in the format of {"id":"5465176836886605"}.

timestamp

Yes

Long

Time when a message is sent.

recipient

Yes

JSON object

The value is in the format of {"id":"107416531546370"}.

Table 6 message parameters

Parameter

Mandatory or Not

Type

Default Value

Description

mid

Yes

String

None

Message ID.

text

Yes

String

None

Message content.

Response Parameters

If this interface is invoked successfully, no response is returned.

If this interface fails to be invoked, a status code and error description are returned.

Example

  • Request
    POST /social/on/facebook/202108135240338790
    
    host : 10.10.10.2:28090
    connection : Keep-Alive
    x-forwarded-for : 10.10.10.3, 10.10.10.4
    x-real-ip : 10.10.10.5
    content-length : 314
    remote-host : 10.10.10.6
    accept : */*
    accept-encoding : deflate, gzip
    user-agent : facebookexternalua
    content-type : application/json
    x-hub-signature-256 : sha256=599c****************3bc
    facebook-api-version : v8.0
    
    {
    	"object" : "page",
    	"entry" : [{
    			"id" : "107416531546370",
    			"time" : 1629191082254,
    			"messaging" : [{
    					"sender" : {
    						"id" : "5465176836886605"
    					},
    					"recipient" : {
    						"id" : "107416531546370"
    					},
    					"timestamp" : 1629191082001,
    					"message" : {
    						"mid" : "m_5ugOgGm2v5x36EyvvInnNOwvV5bGYdVdhgz8VK0mdjnLC3qm8PfaQZdWwAsLOg8hiDIqmpkcYQtllLH_tDzl7A",
    						"text" : "jianlian"
    					}
    				}
    			]
    		}
    	]
    }