mixin.java.sdk.api.Message Maven / Gradle / Ivy
The newest version!
package mixin.java.sdk.api;
import com.google.gson.*;
import mixin.java.sdk.algorithm.JWToken;
import mixin.java.sdk.entity.Msg;
import mixin.java.sdk.util.Action;
import mixin.java.sdk.util.JsonUtil;
import mixin.java.sdk.util.MessageCategory;
import mixin.java.sdk.util.MixinHttpUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import org.apache.commons.lang.StringUtils;
import java.io.IOException;
import java.util.Base64;
import java.util.UUID;
public class Message {
public static boolean send(WebSocket webSocket, Action action, String params) {
JsonObject jsObj = new JsonObject();
Gson gson = new Gson();
JsonElement jsXp = (JsonElement)gson.fromJson(params, JsonElement.class);
jsObj.addProperty("id", UUID.randomUUID().toString());
jsObj.addProperty("action", action.toString());
jsObj.add("params", jsXp);
return webSocket.send(JsonUtil.jsonStrToByteString(jsObj.toString()));
}
public static void sendText(WebSocket webSocket,Msg msg){
JsonObject params = new JsonObject();
params.addProperty("conversation_id",msg.getConversation_id());
params.addProperty("recipient_id",msg.getRecipient_id());
params.addProperty("message_id",msg.getMessage_id());
params.addProperty("category",msg.getCategory());
if(StringUtils.isNotBlank(msg.getRepresentative_id())){
params.addProperty("representative_id",msg.getRepresentative_id());
}
if(StringUtils.isNotBlank(msg.getQuote_message_id())){
params.addProperty("quote_message_id",msg.getQuote_message_id());
}
params.addProperty("data",toBase64(msg.getData()));
send(webSocket, Action.CREATE_MESSAGE, params.toString());
}
public static boolean sendListPendingMessages(WebSocket webSocket) {
return send(webSocket, Action.LIST_PENDING_MESSAGES, (String)null);
}
public static boolean sendMessageAck(WebSocket webSocket, String messageId) {
JsonObject params = new JsonObject();
params.addProperty("message_id", messageId);
params.addProperty("status", "READ");
return send(webSocket, Action.ACKNOWLEDGE_MESSAGE_RECEIPT, params.toString());
}
static String toBase64(String orig) {
return new String(Base64.getEncoder().encode(orig.getBytes()));
}
public static JsonObject sendMessage(long groupId,Msg... msgs) {
try {
JsonArray jsonArray = new JsonArray();
for(Msg msg:msgs){
JsonObject params = new JsonObject();
params.addProperty("conversation_id",msg.getConversation_id());
params.addProperty("recipient_id",msg.getRecipient_id());
params.addProperty("message_id",msg.getMessage_id());
params.addProperty("category",msg.getCategory());
params.addProperty("representative_id",msg.getRepresentative_id());
params.addProperty("quote_message_id",msg.getQuote_message_id());
if(StringUtils.isNotBlank(msg.getData())){
params.addProperty("data",toBase64(msg.getData()));
}
jsonArray.add(params);
}
String result = MixinHttpUtil.post(groupId,Constant.sendMessages, jsonArray.toString());
JsonParser parser = new JsonParser();
JsonObject jsonTree = (JsonObject) parser.parse(result);
return jsonTree;
}catch (IOException e){
e.printStackTrace();
return null;
}
}
private static JsonObject buildMessage(JsonElement params,Action action){
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("id",UUID.randomUUID().toString());
jsonObject.addProperty("action",action.toString());
jsonObject.add("params",params);
return jsonObject;
}
public static void main(String[] args) {
/**Msg msg = new Msg("58c200f9-4b29-357d-b605-8e664bccda59", "2d8ef69d-4132-46d7-bfd8-36fe8db4ddb3",UUID.randomUUID().toString(),MessageCategory.PLAIN_TEXT.toString(),"机器人代发消息测试");
msg.setRepresentative_id("631b3606-26e2-4440-b6e9-365af1d20b83");
msg.setQuote_message_id("57b914d9-85fd-43b8-a74a-016e0cce236a");
//userId:2d8ef69d-4132-46d7-bfd8-36fe8db4ddb3 631b3606-26e2-4440-b6e9-365af1d20b83
//conversationId:58c200f9-4b29-357d-b605-8e664bccda59 242d5912-eb2a-32e5-b0a9-d762fabe3960
JsonObject jsonObject = sendMessage(msg);
System.out.println(jsonObject);*/
//System.out.println(new String(Base64.getUrlDecoder().decode("eyJtZXNzYWdlX2lkIjoiZDRhYTIwYjYtYTk4Ny00MjQ0LThkOWMtZmI1ZjNmM2Y3MTEyIn0=")));
Msg msg = new Msg();
String recall_msg_id = Conversation.UniqueConversationId("2d8ef69d-4132-46d7-bfd8-36fe8db4ddb3","6928d13d-b138-4903-9356-9097722e7186");
msg.setMessage_id(recall_msg_id);
msg.setConversation_id("58c200f9-4b29-357d-b605-8e664bccda59");
msg.setCategory("MESSAGE_RECALL");
msg.setRecipient_id("2d8ef69d-4132-46d7-bfd8-36fe8db4ddb3");
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("message_id",recall_msg_id);
msg.setData(jsonObject.toString());
//JsonObject result = sendMessage(1,msg);
//System.out.println(result);
}
}