feat(send): pass record_id and tenant_code, accept async response
This commit is contained in:
@@ -118,12 +118,14 @@ public class WechatPersonalChannelAdapter implements ChannelAdapter {
|
||||
}
|
||||
|
||||
String contentType = defaultString(request.getContentType()).toLowerCase();
|
||||
String recordId = getString(request.getExtra(), "record_id");
|
||||
String tenantCode = defaultString(getString(request.getExtra(), "tenant_code"));
|
||||
try {
|
||||
Map<String, Object> response;
|
||||
if ("image".equals(contentType)) {
|
||||
response = sendImage(accountId, conversationId, request.getContent());
|
||||
response = sendImage(accountId, conversationId, recordId, tenantCode, request.getContent());
|
||||
} else {
|
||||
response = sendText(accountId, conversationId, request.getContent());
|
||||
response = sendText(accountId, conversationId, recordId, tenantCode, request.getContent());
|
||||
}
|
||||
|
||||
if (response == null) {
|
||||
@@ -133,7 +135,8 @@ public class WechatPersonalChannelAdapter implements ChannelAdapter {
|
||||
}
|
||||
|
||||
Boolean success = getBoolean(response, "success");
|
||||
if (!Boolean.TRUE.equals(success)) {
|
||||
Boolean accepted = getBoolean(response, "accepted");
|
||||
if (!Boolean.TRUE.equals(success) && !Boolean.TRUE.equals(accepted)) {
|
||||
result.setSuccess(false);
|
||||
result.setMessage("个人微信发送失败:" + getString(response, "error"));
|
||||
return result;
|
||||
@@ -157,7 +160,7 @@ public class WechatPersonalChannelAdapter implements ChannelAdapter {
|
||||
return payload;
|
||||
}
|
||||
|
||||
private Map<String, Object> sendText(String accountId, String conversationId, Map<String, Object> content) {
|
||||
private Map<String, Object> sendText(String accountId, String conversationId, String recordId, String tenantCode, Map<String, Object> content) {
|
||||
String text = getString(content, "text");
|
||||
if (isBlank(text)) {
|
||||
throw new IllegalArgumentException("文本内容为空");
|
||||
@@ -166,10 +169,12 @@ public class WechatPersonalChannelAdapter implements ChannelAdapter {
|
||||
body.put("account_id", accountId);
|
||||
body.put("conversation_id", conversationId);
|
||||
body.put("text", text);
|
||||
body.put("record_id", recordId);
|
||||
body.put("tenant_code", tenantCode);
|
||||
return post("/v1/wechat-personal/send/text", body);
|
||||
}
|
||||
|
||||
private Map<String, Object> sendImage(String accountId, String conversationId, Map<String, Object> content) {
|
||||
private Map<String, Object> sendImage(String accountId, String conversationId, String recordId, String tenantCode, Map<String, Object> content) {
|
||||
String imageUrl = getString(content, "image_url");
|
||||
if (isBlank(imageUrl)) {
|
||||
throw new IllegalArgumentException("图片地址为空");
|
||||
@@ -178,6 +183,8 @@ public class WechatPersonalChannelAdapter implements ChannelAdapter {
|
||||
body.put("account_id", accountId);
|
||||
body.put("conversation_id", conversationId);
|
||||
body.put("image_url", imageUrl);
|
||||
body.put("record_id", recordId);
|
||||
body.put("tenant_code", tenantCode);
|
||||
return post("/v1/wechat-personal/send/image", body);
|
||||
}
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ class WechatPersonalChannelAdapterTest {
|
||||
.setResponseCode(200)
|
||||
.setHeader("Content-Type", "application/json")
|
||||
.setBody(toJson(Map.of(
|
||||
"success", true,
|
||||
"accepted", true,
|
||||
"message_id", "msg-123"))));
|
||||
|
||||
SendMessageRequest request = new SendMessageRequest();
|
||||
@@ -110,6 +110,8 @@ class WechatPersonalChannelAdapterTest {
|
||||
request.setContent(content);
|
||||
Map<String, Object> extra = new HashMap<>();
|
||||
extra.put("account_id", "wx-123");
|
||||
extra.put("record_id", "1001");
|
||||
extra.put("tenant_code", "rescue");
|
||||
request.setExtra(extra);
|
||||
|
||||
SendMessageResult result = adapter.sendMessage(request);
|
||||
@@ -121,7 +123,7 @@ class WechatPersonalChannelAdapterTest {
|
||||
assertThat(recorded.getMethod()).isEqualTo("POST");
|
||||
assertThat(recorded.getPath()).isEqualTo("/v1/wechat-personal/send/text");
|
||||
String body = recorded.getBody().readUtf8();
|
||||
assertThat(body).contains("wx-123", "chat-123", "hello");
|
||||
assertThat(body).contains("wx-123", "chat-123", "hello", "1001", "rescue");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -130,7 +132,7 @@ class WechatPersonalChannelAdapterTest {
|
||||
.setResponseCode(200)
|
||||
.setHeader("Content-Type", "application/json")
|
||||
.setBody(toJson(Map.of(
|
||||
"success", true,
|
||||
"accepted", true,
|
||||
"message_id", "img-123"))));
|
||||
|
||||
SendMessageRequest request = new SendMessageRequest();
|
||||
@@ -141,6 +143,8 @@ class WechatPersonalChannelAdapterTest {
|
||||
request.setContent(content);
|
||||
Map<String, Object> extra = new HashMap<>();
|
||||
extra.put("account_id", "wx-123");
|
||||
extra.put("record_id", "1002");
|
||||
extra.put("tenant_code", "rescue");
|
||||
request.setExtra(extra);
|
||||
|
||||
SendMessageResult result = adapter.sendMessage(request);
|
||||
@@ -150,6 +154,8 @@ class WechatPersonalChannelAdapterTest {
|
||||
|
||||
RecordedRequest recorded = mockWebServer.takeRequest();
|
||||
assertThat(recorded.getPath()).isEqualTo("/v1/wechat-personal/send/image");
|
||||
String body = recorded.getBody().readUtf8();
|
||||
assertThat(body).contains("wx-123", "1002", "rescue", "https://example.com/img.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -899,6 +899,8 @@ public class SendService {
|
||||
if (account != null && StringUtils.hasText(account.getAccountId())) {
|
||||
extra.put("account_id", account.getAccountId());
|
||||
}
|
||||
extra.put("record_id", record.getId());
|
||||
extra.put("tenant_code", record.getTenantCode());
|
||||
messageRequest.setExtra(extra);
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user