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

Receiving and Processing Messages Sent on Twitter

Function

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

Usage Description

Prerequisites

response_token for authentication has been successfully generated by invoking the interface in Calling Back Data on Twitter.

Interface Authentication

Obtain the authentication character string from the request header x-twitter-webhooks-signature and delete sha256= from the character string.

Obtain the value of appSecret configured for the channel, encrypt the request and appSecret in the request using SHA256, and generate an authentication character string for comparison.

Compare the two generated character strings. If they are the same, the authentication is successful.
String signatureHeader = request.getHeader("x-twitter-webhooks-signature");
String payloadContent = IOUtils.toString(request.getReader());
String signature = StringUtils.substringAfter(signatureHeader, "sha256=");
try {
      String payloadHash = BaseUtil.hMacSha256(payloadContent, consumerKey);
      if (!StringUtils.equals(signature, payloadHash)) {
           log.error("The signature is not correct, channelId = {}", channelId);
           return false;
      }
      } catch (NoSuchAlgorithmException | InvalidKeyException e) {
         log.error("get payloadHash failed", e);
         return false;
   }
    

Interface Prototype

Table 1 Interface prototype description

Method

POST

URL

https://{IP address}:{Port number}/social/on/twitter/{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

Default Value

Description

Content-Type

Yes

String

None

The value is fixed at application/x-www-form-urlencoded.

x-twitter-webhooks-signature

Yes

String

None

This parameter is used to authenticate the MO message interface and is generated by invoking the interface in Calling Back Data on Twitter.

Table 3 Request body parameters

Parameter

Mandatory or Not

Type

Default Value

Description

direct_message_events

Yes

JSON array

None

Event data. For details, see Table 4.

users

Yes

String

None

User data. For details, see Table 6.

Table 4 direct_message_events parameters

Parameter

Mandatory or Not

Type

Default Value

Description

message_create

Yes

JSON object

None

Message sent by a user. For details, see Table 5.

created_timestamp

Yes

String

None

Timestamp when a message is sent.

Table 5 message_create parameters

Parameter

Mandatory or Not

Type

Default Value

Description

sender_id

Yes

String

None

ID of the message sender.

message_id

Yes

String

None

Unique message ID.

Table 6 users parameters

Parameter

Mandatory or Not

Type

Default Value

Description

id

Yes

String

None

User ID.

name

Yes

String

None

Username.

profile_image_url_https

Yes

String

None

Image URL.

Response Parameters

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

Example

  • Request
    POST 
    x-twitter-webhooks-signature: xxxxxxxxxxxxxxx
    /*************************
    Accept: */*
    Host: 10.154.198.164
    Content-Type: application/x-www-form-urlencoded
    {
    	"direct_message_events" : [
    		"message_create" : {
    			"sender_id" : "01122552341412",
    			"message_id" : "aaaswfwefew1255"
    		},
    		"created_timestamp" : "1111235235666"
    		],
    		"user" : {
    			"id" : "01122552341412",
    			"name" : "sgyyub",
    			"profile_image_url_https" : "https://www.twitter/xxxxxx"
    		}		
    }