NIMSDK-AOS  9.16.0
RobotAttachment.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.robot.model;
2 
4 import com.netease.nimlib.util.JSONHelper;
5 
6 import org.json.JSONException;
7 import org.json.JSONObject;
8 
13 public class RobotAttachment implements MsgAttachment {
14 
15  // common
16  private static final String TAG_ROBOT_SEND = "msgOut";
17  private static final String TAG_ROBOT_ACCID = "robotAccid";
18 
19  // 下行
20  private static final String TAG_CLIENT_MSG_ID = "clientMsgId";
21  private static final String TAG_ROBOT_MSG = "robotMsg";
22 
23  // 上行
24  private static final String TAG_PARAM = "param";
25  private static final String TAG_REQUEST_TYPE = "type";
26  private static final String TAG_REQUEST_CONTENT = "content";
27  private static final String TAG_REQUEST_TARGET = "target";
28  private static final String TAG_REQUEST_PARAMS = "params";
29 
30  private boolean isRobotSend = true; // 是否为下行消息,true为下行,false为上行。默认true
31  private String fromRobotAccount;
32 
33  private String responseForMessageId;
34  private String response;
35 
36  private String requestType;
37  private String requestContent;
38  private String requestTarget;
39  private String requestParams;
40 
41  public RobotAttachment() {
42 
43  }
44 
45  public RobotAttachment(String attStr) {
46  fromJson(attStr);
47  }
48 
49  public void initSend(String robotAccount, String type, String content, String target, String params) {
50  this.fromRobotAccount = robotAccount;
51  this.requestType = type;
52  this.requestContent = content;
53  this.requestTarget = target;
54  this.requestParams = params;
55  this.isRobotSend = false; // 上行
56  }
57 
58  private void fromJson(String attach) {
59  JSONObject json = JSONHelper.parse(attach);
60  isRobotSend = JSONHelper.getBoolean(json, TAG_ROBOT_SEND);
61  fromRobotAccount = JSONHelper.getString(json, TAG_ROBOT_ACCID);
62  responseForMessageId = JSONHelper.getString(json, TAG_CLIENT_MSG_ID);
63  JSONObject responseObject = JSONHelper.getJSONObject(json, TAG_ROBOT_MSG);
64  if (responseObject != null) {
65  response = responseObject.toString();
66  }
67  JSONObject paramJson = JSONHelper.getJSONObject(json, TAG_PARAM);
68  if (paramJson != null) {
69  requestType = JSONHelper.getString(paramJson, TAG_REQUEST_TYPE);
70  requestContent = JSONHelper.getString(paramJson, TAG_REQUEST_CONTENT);
71  requestTarget = JSONHelper.getString(paramJson, TAG_REQUEST_TARGET);
72  requestParams = JSONHelper.getString(paramJson, TAG_REQUEST_PARAMS);
73  }
74  }
75 
76  @Override
77  public String toJson(boolean send) {
78  JSONObject jsonObject = new JSONObject();
79  try {
80  if (send) {
81  // 发
82  JSONObject paramObject = new JSONObject();
83  paramObject.put(TAG_REQUEST_TYPE, requestType);
84  paramObject.put(TAG_REQUEST_CONTENT, requestContent);
85  paramObject.put(TAG_REQUEST_TARGET, requestTarget);
86  paramObject.put(TAG_REQUEST_PARAMS, requestParams);
87  jsonObject.put(TAG_PARAM, paramObject);
88  } else {
89  // 收
90  jsonObject.put(TAG_ROBOT_MSG, response);
91  jsonObject.put(TAG_CLIENT_MSG_ID, responseForMessageId);
92  }
93 
94  jsonObject.put(TAG_ROBOT_ACCID, fromRobotAccount);
95  jsonObject.put(TAG_ROBOT_SEND, isRobotSend);
96  } catch (JSONException e) {
97  e.printStackTrace();
98  }
99 
100  return jsonObject.toString();
101  }
102 
103  public boolean isRobotSend() {
104  return isRobotSend;
105  }
106 
107  public void setRobotSend(boolean robotSend) {
108  this.isRobotSend = robotSend;
109  }
110 
111  public String getFromRobotAccount() {
112  return fromRobotAccount;
113  }
114 
115  public void setFromRobotAccount(String fromRobotAccount) {
116  this.fromRobotAccount = fromRobotAccount;
117  }
118 
119  public String getResponseForMessageId() {
120  return responseForMessageId;
121  }
122 
123  public void setResponseForMessageId(String responseForMessageId) {
124  this.responseForMessageId = responseForMessageId;
125  }
126 
127  public String getResponse() {
128  return response;
129  }
130 
131  public String getRequestType() {
132  return requestType;
133  }
134 
135  public void setRequestType(String requestType) {
136  this.requestType = requestType;
137  }
138 
139  public String getRequestContent() {
140  return requestContent;
141  }
142 
143  public void setRequestContent(String requestContent) {
144  this.requestContent = requestContent;
145  }
146 
147  public String getRequestTarget() {
148  return requestTarget;
149  }
150 
151  public void setRequestTarget(String requestTarget) {
152  this.requestTarget = requestTarget;
153  }
154 
155  public String getRequestParams() {
156  return requestParams;
157  }
158 
159  public void setRequestParams(String requestParams) {
160  this.requestParams = requestParams;
161  }
162 }
void setResponseForMessageId(String responseForMessageId)
void initSend(String robotAccount, String type, String content, String target, String params)
String toJson(boolean send)
将消息附件序列化为字符串,存储到消息数据库或发送到服务器。