接收并处理line平台发送的消息接口
接口功能
接收并处理line平台发送的消息。
接口鉴权
将请求体,渠道配置里的appSecret,通过sha256算法生成认证字符串,与Line请求头x-line-signature里面字符串进行比较。
/**
* 校验line签名信息
*
* @param signature line发送携带的签名信息
* @param requestBody 请求体参数
* @param channelSecret 加密密钥
* @return 校验结果, true:校验通过 false:校验不通过
*/
private boolean validateSignature(String signature, String requestBody, String channelSecret) {
try {
SecretKeySpec key = new SecretKeySpec(channelSecret.getBytes(StandardCharsets.UTF_8), privateKeyAlgorithm);
Mac mac = Mac.getInstance(privateKeyAlgorithm);
mac.init(key);
byte[] source = requestBody.getBytes(StandardCharsets.UTF_8);
String encodeResult = Base64.encodeBase64String(mac.doFinal(source));
return signature.equals(encodeResult);
} catch (Exception e) {
log.error("line signature validate exception.", e);
return false;
}
} 接口原型
| 请求方法 | POST | |
|---|---|---|
| 请求URL | https://ip:port/social/on/line/{channelId} | 其中,ip:port为对外暴露的nslb的ip和port, channelId为渠道id |
请求参数
| 参数名称 | 选取原则 | 参数类型 | 默认值 | 说明 |
|---|---|---|---|---|
| Content-Type | 必选 | String | 无 | 固定填 application/json; charset=UTF-8。 |
| X-Forwarded-For | 必选 | String | 无 | 发送消息IP |
| x-line-signature | 必选 | String | 无 | 请求头签名校验 |
| 参数名称 | 选取原则 | 参数类型 | 默认值 | 说明 |
|---|---|---|---|---|
| events | 必选 | JSONArray | 无 | events中包含事件数据表4。 |
| 参数名称 | 选取原则 | 参数类型 | 默认值 | 说明 |
|---|---|---|---|---|
| type | 必选 | String | 无 | 消息类型。 |
| mode | 必选 | String | active | 消息模式, |
| source | 必选 | JSONObject | 无 | 消息来源。表5 |
| message | 必选 | JSONObject | 无 | 具体消息。表6 |
| 参数名称 | 选取原则 | 参数类型 | 默认值 | 说明 |
|---|---|---|---|---|
| id | 必选 | String | 无 | 用户消息唯一编码。 |
| type | 必选 | String | 无 | 消息类型:text(文本),audio(音频),image(图片),video(视频),location(位置) |
| latitude | 可选 | BigDecimal | 无 | 经度。 |
| longitude | 可选 | BigDecimal | 无 | 纬度。 |
| text | 可选 | String | 无 | 文本消息。 |
| contentProvider | 可选 | JSONObject | 无 | 多媒体内容提供方。表7 |
响应参数
无响应参数
接口示例
- 请求示例
POST /******************** Accept: */* Host: 10.154.198.164 Content-Type: application/json;charset=UTF-8 Content-Length: 185 { "events": [ { "type": "", "mode": "active", "source": {"type": "user", "userId": "8615897042502" }, "message": { "id":"asdwe145FFUfj", "type": "TEXT", "text": "hello" } } ] }