
# 接收并处理twitter平台发送的消息接口
#### 接口功能
接收并处理twitter平台发送的消息。
#### 使用说明
前提条件
调用[twitter回调接口](https://support.huaweicloud.com/api-ccfs-cec/cec_07_0986.html#ZH-CN_TOPIC_0000002513233875)成功生成鉴权response_token。
#### 接口鉴权
获取请求头x-twitter-webhooks-signature中的认证字符串，截去前面的字符串"sha256="。
获取渠道配置的appSecret，将请求中request，与appSecret进行sha256加密，生成对比的鉴权字符串。
对比两个生成的字符串，相同，即鉴权通过。
```
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;
   }
```
#### 接口原型
表1接口原型说明 
|   请求方法    |                                                                                                 POST                                                                                                 ||
|---|---|---|
| **请求URL** | https://ip:port/social/on/twitter/{channelId} | 其中，ip:port为对外暴露的nslb的ip和port， channelId为渠道id |
   
#### 请求参数
表2请求Headers参数说明 
| 参数名称                         | 选取原则 | 参数类型   | 默认值 | 说明                                                                                                                        |
|:---|:---|:---|:---|:---|
| Content-Type                 | 必选   | String | 无   | 固定填 application/x-www-form-urlencoded。                                                                                    |
| x-twitter-webhooks-signature | 必选   | String | 无   | 用于上行消息接口鉴权，调用[twitter回调接口](https://support.huaweicloud.com/api-ccfs-cec/cec_07_0986.html#ZH-CN_TOPIC_0000002513233875)生成。 |
   
表3请求Body中request参数说明 
| 参数名称                  | 选取原则 | 参数类型      | 默认值 | 说明                                                                                                                                                    |
|:---|:---|:---|:---|:---|
| direct_message_events | 必选   | JSONArray | 无   | 事件数据，详见[表4 direct_message_events中的参数说明] |
| users                 | 必选   | String    | 无   | 用户相关数据，详见[表6 users中的参数说明]                  |
   
 表4direct_message_events中的参数说明 
| 参数名称              | 选取原则 | 参数类型       | 默认值 | 说明                                                                                                                                              |
|:---|:---|:---|:---|:---|
| message_create    | 必选   | JSONObject | 无   | 用户发送的消息，详见[表5 message_create中的参数说明] |
| created_timestamp | 必选   | String     | 无   | 发送消息时间戳。                                                                                                                                        |
   
 表5message_create中的参数说明 
| 参数名称       | 选取原则 | 参数类型   | 默认值 | 说明          |
|:---|:---|:---|:---|:---|
| sender_id  | 必选   | String | 无   | 标志消息的发送者ID。 |
| message_id | 必选   | String | 无   | 消息唯一标识。     |
   
 表6users中的参数说明 
| 参数名称                    | 选取原则 | 参数类型   | 默认值 | 说明     |
|:---|:---|:---|:---|:---|
| id                      | 必选   | String | 无   | 用户ID。  |
| name                    | 必选   | String | 无   | 用户名称。  |
| profile_image_url_https | 必选   | String | 无   | 图片url。 |
   
#### 响应参数
当调用本接口响应成功，response 无响应内容。
#### 接口示例
- 请求示例
  ```
  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"
  }
  }
  ```
  
 
