Skip to content

Commit f48be62

Browse files
authored
Merge pull request #123 from olimpias/fallback-support
Add support for fallback messages
2 parents fc61492 + 694ce8b commit f48be62

File tree

6 files changed

+250
-1
lines changed

6 files changed

+250
-1
lines changed

api/src/main/java/com/messagebird/MessageBirdClient.java

+15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import com.messagebird.objects.conversations.ConversationMessage;
3636
import com.messagebird.objects.conversations.ConversationMessageList;
3737
import com.messagebird.objects.conversations.ConversationMessageRequest;
38+
import com.messagebird.objects.conversations.ConversationSendRequest;
39+
import com.messagebird.objects.conversations.ConversationSendResponse;
3840
import com.messagebird.objects.conversations.ConversationStartRequest;
3941
import com.messagebird.objects.conversations.ConversationStatus;
4042
import com.messagebird.objects.conversations.ConversationWebhook;
@@ -110,6 +112,7 @@ public class MessageBirdClient {
110112
private static final String VERIFYPATH = "/verify";
111113
private static final String VOICEMESSAGESPATH = "/voicemessages";
112114
private static final String CONVERSATION_PATH = "/conversations";
115+
private static final String CONVERSATION_SEND_PATH = "/send";
113116
private static final String CONVERSATION_MESSAGE_PATH = "/messages";
114117
private static final String CONVERSATION_WEBHOOK_PATH = "/webhooks";
115118
static final String VOICECALLSPATH = "/calls";
@@ -951,6 +954,18 @@ public Conversation startConversation(ConversationStartRequest request)
951954
return messageBirdService.sendPayLoad(url, request, Conversation.class);
952955
}
953956

957+
/**
958+
* sendMessage allows you to send message to users over any communication platform supported by Programmable Conversations
959+
*
960+
* @param request Data for this request.
961+
* @return The created Message in ConversationSendResponse object.
962+
*/
963+
public ConversationSendResponse sendMessage(ConversationSendRequest request)
964+
throws UnauthorizedException, GeneralException {
965+
String url = String.format("%s%s", this.conversationsBaseUrl, CONVERSATION_SEND_PATH);
966+
return messageBirdService.sendPayLoad(url, request, ConversationSendResponse.class);
967+
}
968+
954969
/**
955970
* Gets a ConversationMessage listing with specified pagination options.
956971
*

api/src/main/java/com/messagebird/MessageBirdServiceImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public <T, P> T getJsonData(final String request, final P payload, final String
217217
final String body = apiResponse.getBody();
218218
final int status = apiResponse.getStatus();
219219

220-
if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_CREATED) {
220+
if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_CREATED || status == HttpURLConnection.HTTP_ACCEPTED) {
221221
try {
222222
final ObjectMapper mapper = new ObjectMapper();
223223
// If we as new properties, we don't want the system to fail, we rather want to ignore them
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.messagebird.objects.conversations;
2+
3+
public class ConversationFallbackOption {
4+
private String from;
5+
private String after;
6+
7+
public ConversationFallbackOption() {
8+
}
9+
10+
public ConversationFallbackOption(String from, String after) {
11+
this.from = from;
12+
this.after = after;
13+
}
14+
15+
public String getFrom() {
16+
return from;
17+
}
18+
19+
public void setFrom(String from) {
20+
this.from = from;
21+
}
22+
23+
public String getAfter() {
24+
return after;
25+
}
26+
27+
public void setAfter(String after) {
28+
this.after = after;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "ConversationFallbackOption{" +
34+
"from='" + from + '\'' +
35+
", after='" + after + '\'' +
36+
'}';
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.messagebird.objects.conversations;
2+
3+
public class ConversationSendRequest {
4+
private String to;
5+
private ConversationContentType type;
6+
private ConversationContent content;
7+
private String from;
8+
private String reportUrl;
9+
private ConversationFallbackOption fallback;
10+
11+
public ConversationSendRequest(String to, ConversationContentType type, ConversationContent content, String from, String reportUrl, ConversationFallbackOption fallback) {
12+
this.to = to;
13+
this.type = type;
14+
this.content = content;
15+
this.from = from;
16+
this.reportUrl = reportUrl;
17+
this.fallback = fallback;
18+
}
19+
20+
public ConversationSendRequest() {
21+
}
22+
23+
24+
public String getTo() {
25+
return to;
26+
}
27+
28+
public void setTo(String to) {
29+
this.to = to;
30+
}
31+
32+
public ConversationContentType getType() {
33+
return type;
34+
}
35+
36+
public void setType(ConversationContentType type) {
37+
this.type = type;
38+
}
39+
40+
public ConversationContent getContent() {
41+
return content;
42+
}
43+
44+
public void setContent(ConversationContent content) {
45+
this.content = content;
46+
}
47+
48+
public String getFrom() {
49+
return from;
50+
}
51+
52+
public void setFrom(String from) {
53+
this.from = from;
54+
}
55+
56+
public String getReportUrl() {
57+
return reportUrl;
58+
}
59+
60+
public void setReportUrl(String reportUrl) {
61+
this.reportUrl = reportUrl;
62+
}
63+
64+
public ConversationFallbackOption getFallback() {
65+
return fallback;
66+
}
67+
68+
public void setFallback(ConversationFallbackOption fallback) {
69+
this.fallback = fallback;
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return "ConversationSendRequest{" +
75+
"to='" + to + '\'' +
76+
", type=" + type +
77+
", content=" + content +
78+
", from='" + from + '\'' +
79+
", reportUrl='" + reportUrl + '\'' +
80+
", fallback=" + fallback +
81+
'}';
82+
}
83+
}
84+
85+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.messagebird.objects.conversations;
2+
3+
public class ConversationSendResponse {
4+
private String id; //messageID
5+
private String status;
6+
private FallbackOptionResponse fallback;
7+
8+
public static class FallbackOptionResponse{
9+
private String id;
10+
11+
12+
public String getId() {
13+
return id;
14+
}
15+
16+
public void setId(String id) {
17+
this.id = id;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "FallbackOptionResponse{" +
23+
"id='" + id + '\'' +
24+
'}';
25+
}
26+
}
27+
28+
public String getId() {
29+
return id;
30+
}
31+
32+
public void setId(String id) {
33+
this.id = id;
34+
}
35+
36+
public String getStatus() {
37+
return status;
38+
}
39+
40+
public void setStatus(String status) {
41+
this.status = status;
42+
}
43+
44+
public FallbackOptionResponse getFallback() {
45+
return fallback;
46+
}
47+
48+
public void setFallback(FallbackOptionResponse fallback) {
49+
this.fallback = fallback;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "ConversationSendResponse{" +
55+
"id='" + id + '\'' +
56+
", status='" + status + '\'' +
57+
", fallback=" + fallback +
58+
'}';
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import com.messagebird.MessageBirdClient;
2+
import com.messagebird.MessageBirdService;
3+
import com.messagebird.MessageBirdServiceImpl;
4+
import com.messagebird.exceptions.GeneralException;
5+
import com.messagebird.exceptions.UnauthorizedException;
6+
import com.messagebird.objects.conversations.ConversationContent;
7+
import com.messagebird.objects.conversations.ConversationContentType;
8+
import com.messagebird.objects.conversations.ConversationFallbackOption;
9+
import com.messagebird.objects.conversations.ConversationSendRequest;
10+
import com.messagebird.objects.conversations.ConversationSendResponse;
11+
12+
public class ExampleConversationSendMessage {
13+
14+
public static void main(String[] args) {
15+
if (args.length < 3) {
16+
System.out.println("Please at least specify your access key, the channel id and destination address.\n" +
17+
"Usage : java -jar <this jar file> test_accesskey(Required) channel_id(Required) to(Required) fallback_channel_id(optional)");
18+
return;
19+
}
20+
21+
//First create your service object
22+
final MessageBirdService wsr = new MessageBirdServiceImpl(args[0]);
23+
//Add the service to the client
24+
final MessageBirdClient messageBirdClient = new MessageBirdClient(wsr);
25+
26+
27+
28+
ConversationFallbackOption fallbackOption = null;
29+
if (args.length == 4) {
30+
fallbackOption = new ConversationFallbackOption(args[3], "5m");
31+
}
32+
ConversationContent conversationContent = new ConversationContent();
33+
conversationContent.setText("Hello world from java sdk");
34+
35+
ConversationSendRequest request = new ConversationSendRequest(
36+
args[1],
37+
ConversationContentType.TEXT,
38+
conversationContent,
39+
args[2],
40+
"",
41+
fallbackOption);
42+
43+
try {
44+
ConversationSendResponse sendResponse = messageBirdClient.sendMessage(request);
45+
System.out.println(sendResponse.toString());
46+
47+
} catch (GeneralException | UnauthorizedException exception) {
48+
exception.printStackTrace();
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)