發送自訂訊息
更新時間:2023-02-16 23:29:52
介面描述
1、发送聊天室自定义消息
2、接口支持https协议
介面URL
http://api.polyv.net/live/v4/chat/send-custom-message/encode
請求方式
POST
介面限制
1、介面同時支援HTTP、HTTPS,建議使用HTTPS以確保介面安全,介面呼叫有頻率限制,詳細請查看
2、需要發送的文字內容(content)或圖片(imgUrl)不能同時為空,可以同時提交
3、需要發送的文字內容(content)需要進行URL安全的base64編碼
請求參數描述
| 參數名 | 必選 | 類型 | 說明 |
|---|---|---|---|
| appId | true | String | 帳號appId【詳見取得金鑰】 |
| timestamp | true | Long | 當前13位毫秒級時間戳,3分鐘內有效 |
| sign | true | String | 簽名,為32位大寫的MD5值,產生簽名的appSecret金鑰作為通訊資料安全的關鍵資訊,嚴禁保存在用戶端直接使用,所有API都必須透過客戶自己伺服器中轉呼叫POLYV伺服器取得回應資料【詳見簽名產生規則】 |
| channelId | true | String | 頻道號 |
| content | false | String | 發送文字內容,需要進行URL安全的base64編碼,編碼後最大長度1500個字元 |
| imgUrl | false | String | 發送的圖片url地址 |
| joinHistoryList | false | Integer | 是否加入聊天歷史資料,預設為1 1:加入到聊天歷史資料 0:不加入 |
| watchType | false | Integer | 此訊息面向何種角色發送(預設為1) 1:面向頻道所有角色 2:僅面向頻道內觀眾角色 3:僅面向特殊角色(講師、嘉賓、助教、管理員) |
範例
http://api.polyv.net/live/v4/chat/send-custom-message/encode?joinHistoryList=0&appId=frlr1zazn3&sign=94CAD8CB13062C904F193ED3958CE7DB&channelId=2272655×tamp=1676561050174
表單參數:
content=5ZCM5a2m5Lus77yM5pma5LiK5aW977yB6L-Z5piv5oiR5Lus55qE56ys5LiA5aCC6K--77yB
回應參數描述
| 參數名 | 類型 | 說明 |
|---|---|---|
| code | Integer | 狀態碼,與 http 狀態碼相同,用於確定基本的回應狀態 |
| status | String | 回應結果,由業務決定,成功返回success,失敗返回error |
| success | Boolean | 是否成功回應 |
| requestId | String | 請求ID,每次請求產生的唯一的 UUID,僅可用於排查、除錯,不應該和業務掛鉤 |
| error | Object | 狀態碼非200時的錯誤資訊【詳見Error欄位描述】 |
| data | Object | 成功回應時返回null |
Error參數描述
| 參數名 | 類型 | 說明 |
|---|---|---|
| code | Integer | 錯誤代碼,用於確定具體的錯誤原因 |
| desc | String | 錯誤描述,與 error.code 對應 |
Java請求範例
快速接入基礎代碼請下載相關依賴原始碼, 點擊下載原始碼 ,下載後加入到自己的原始碼工程中即可。測試用例中的HttpUtil.java 和 LiveSignUtil.java 都包含在下載檔案中。
強烈建議您使用直播Java SDK完成API的功能對接,直播Java SDK 對API呼叫邏輯、異常處理、資料簽名、HTTP請求執行緒池進行了統一封裝和最佳化。
private static final Logger log = LoggerFactory.getLogger(getClass());
/**
* 发送聊天室自定义消息
* @throws IOException
* @throws NoSuchAlgorithmException
*/
@Test
public void sendCustomMessageEncodeTest() throws IOException, NoSuchAlgorithmException {
//公共参数,填写自己的实际参数
String appId = super.appId;
String appSecret = super.appSecret;
String timestamp = String.valueOf(System.currentTimeMillis());
//业务参数
String url = "http://api.polyv.net/live/v4/chat/send-custom-message/encode";
String channelId = "2272655";
String content = "同学们,晚上好!这是我们的第一堂课!";
String joinHistoryList = "0";
content = new String(
org.apache.commons.codec.binary.Base64.encodeBase64URLSafe(content.getBytes(StandardCharsets.UTF_8)));
//http 调用逻辑
Map<String, String> requestMap = new HashMap<>();
requestMap.put("appId", appId);
requestMap.put("timestamp", timestamp);
requestMap.put("channelId", channelId);
requestMap.put("joinHistoryList", joinHistoryList);
requestMap.put("sign", LiveSignUtil.getSign(requestMap, appSecret));
url = HttpUtil.appendUrl(url, requestMap);
Map<String, String> paramMap = new HashMap<>();
paramMap.put("content", content);
String response = HttpUtil.postFormBody(url, paramMap);
log.info("测试发送聊天室自定义消息成功:{}", response);
//do somethings
}
回應範例
系統全域錯誤說明詳見全域錯誤說明
成功範例
{
"code": 200,
"status": "success",
"requestId": "8cdf6088d1dc42abbe8727f402d502ac.73.16765610518450895",
"data": null,
"success": true
}
異常範例
{
"code": 400,
"status": "error",
"requestId": "d310b70bc329403f87f77f9203d50f89.128.16360831552223589",
"error": {
"code": 20001,
"desc": "application not found."
},
"success": false
}
