Skip to content

Commit c38cbe0

Browse files
author
Alvin Reyes
committed
Added Json To Action Frame Processor to allow the engine to convert a JSON response to an Action Frame
1 parent 260fc5f commit c38cbe0

16 files changed

Lines changed: 441 additions & 152 deletions

File tree

src/main/java/co/aurasphere/botmill/kik/builder/TextMessageBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public TextMessageBuilder setBody(String body) {
9393
/**
9494
* Sets the delay.
9595
*
96-
* @param delay the delay
96+
* @param delay the delay before this message is process. This is in milliseconds.
9797
* @return the text message builder
9898
*/
9999
public TextMessageBuilder setDelay(Integer delay) {

src/main/java/co/aurasphere/botmill/kik/incoming/event/annotation/AnyEvent.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,5 @@
4040
@Inherited
4141
@Retention(RetentionPolicy.RUNTIME)
4242
public @interface AnyEvent {
43-
4443

4544
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
*
3+
* MIT License
4+
*
5+
* Copyright (c) 2017 BotMill.io
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*
25+
*/
26+
package co.aurasphere.botmill.kik.incoming.event.annotation;
27+
28+
import java.lang.annotation.Documented;
29+
import java.lang.annotation.ElementType;
30+
import java.lang.annotation.Inherited;
31+
import java.lang.annotation.Retention;
32+
import java.lang.annotation.RetentionPolicy;
33+
import java.lang.annotation.Target;
34+
35+
/**
36+
* The Interface Event.
37+
*/
38+
@Documented
39+
@Target(ElementType.CONSTRUCTOR)
40+
@Inherited
41+
@Retention(RetentionPolicy.RUNTIME)
42+
public @interface Event {
43+
44+
}

src/main/java/co/aurasphere/botmill/kik/incoming/event/annotation/TextMessageEvent.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
import java.lang.annotation.RetentionPolicy;
3333
import java.lang.annotation.Target;
3434

35-
import co.aurasphere.botmill.kik.incoming.model.IncomingMessage;
36-
import co.aurasphere.botmill.kik.model.Event;
37-
3835
/**
3936
* The Interface TextMessageEvent.
4037
*/

src/main/java/co/aurasphere/botmill/kik/incoming/handler/IncomingToOutgoingMessageHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ private void outgoingHandler(List<Frame> actionFrames, Message message, boolean
150150
outgoingMessage = new co.aurasphere.botmill.kik.outgoing.model.ReadReceiptMessage();
151151
outgoingMessage = (co.aurasphere.botmill.kik.outgoing.model.ReadReceiptMessage) reply
152152
.processReply(message);
153-
;
154153
} else if (reply instanceof AnyReply) {
155154
// don't treat istyping, start chatting or any
156155
// non-text incoming message as any reply.

src/main/java/co/aurasphere/botmill/kik/incoming/handler/JsonToActionFrameHandler.java

Lines changed: 94 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import java.util.List;
3030

3131
import co.aurasphere.botmill.kik.builder.ActionFrameBuilder;
32+
import co.aurasphere.botmill.kik.builder.LinkMessageBuilder;
33+
import co.aurasphere.botmill.kik.builder.PictureMessageBuilder;
34+
import co.aurasphere.botmill.kik.builder.VideoMessageBuilder;
3235
import co.aurasphere.botmill.kik.factory.EventFactory;
3336
import co.aurasphere.botmill.kik.factory.ReplyFactory;
3437
import co.aurasphere.botmill.kik.incoming.handler.model.JsonAction;
@@ -38,47 +41,127 @@
3841
import co.aurasphere.botmill.kik.model.ActionFrame;
3942
import co.aurasphere.botmill.kik.model.Event;
4043
import co.aurasphere.botmill.kik.model.Frame;
44+
import co.aurasphere.botmill.kik.model.Message;
45+
import co.aurasphere.botmill.kik.model.Reply;
4146
import co.aurasphere.botmill.kik.network.NetworkUtils;
47+
import co.aurasphere.botmill.kik.outgoing.model.LinkMessage;
48+
import co.aurasphere.botmill.kik.outgoing.model.PictureMessage;
49+
import co.aurasphere.botmill.kik.outgoing.model.VideoMessage;
50+
import co.aurasphere.botmill.kik.outgoing.reply.LinkMessageReply;
51+
import co.aurasphere.botmill.kik.outgoing.reply.PictureMessageReply;
4252
import co.aurasphere.botmill.kik.outgoing.reply.TextMessageReply;
53+
import co.aurasphere.botmill.kik.outgoing.reply.VideoMessageReply;
4354

4455
/**
4556
* The Class JsonToActionFrameHandler.
4657
*
4758
* @author Alvin P. Reyes
4859
*/
4960
public class JsonToActionFrameHandler {
50-
61+
5162
/** The Constant CONST_TEXT. */
5263
private static final String CONST_TEXT = "text";
53-
64+
5465
/** The Constant CONST_PATTERN. */
5566
private static final String CONST_PATTERN = "pattern";
5667

5768
/**
5869
* Json to text message reply.
5970
*
6071
* @param jsonUrl
61-
* the json url
72+
* the json url.
73+
*
74+
* JSON Format of the source should follow the following convention.
75+
*
76+
* {
77+
* "jsonkikbotmill": [
78+
* {
79+
* "event":"pattern",
80+
* "input":"(?i:who (wrote|created|create) (this|you|)[\\?])",
81+
* "replies":[
82+
* {"type":"text","text":{"body":"Written by the brilliant minds of Alvin Reyes and Donato Rimenti. Bad-ass Java Developers."}},
83+
* {"type":"text","text":{"body":"Written by the brilliant minds of Alvin Reyes and Donato Rimenti. Bad-ass Java Developers."}},
84+
* {"type":"picture", "picture":{"text":"picture","title":"title picture","url":""}},
85+
* {"type":"link","link": {"text":"link","title":"link title","url":""}},
86+
* {"type":"video", "video": {"text":"video","title":"title video","url":""}},
87+
* ]
88+
* }
89+
* ]
90+
* }
6291
* @return the list
6392
*/
6493
public static List<Frame> jsonToFrameReply(String jsonUrl) {
65-
66-
94+
6795
List<Frame> list = new ArrayList<Frame>();
6896
String json = NetworkUtils.get(jsonUrl);
6997
JsonToActionFrame a = JsonUtils.fromJson(json, JsonToActionFrame.class);
70-
98+
7199
for (JsonAction jaction : a.getJsonTextAction()) {
72-
100+
73101
if (jaction.getEvent().equals(CONST_PATTERN)) {
74-
75-
102+
list.add(ActionFrameBuilder.getInstance().setEvent(EventFactory.textMessagePattern(jaction.getInput()))
103+
.addReplies(processReplies(jaction)).build());
104+
}else if (jaction.getEvent().equals(CONST_TEXT)) {
105+
list.add(ActionFrameBuilder.getInstance().setEvent(EventFactory.textMessage(jaction.getInput()))
106+
.addReplies(processReplies(jaction)).build());
76107
}
77108
}
78109

79110
return list;
80111
}
81112

82-
83-
113+
/**
114+
* Process replies.
115+
*
116+
* @param jaction the jaction
117+
* @return the list< reply<? extends message>>
118+
*/
119+
private static List<Reply<? extends Message>> processReplies(JsonAction jaction) {
120+
121+
List<Reply<? extends Message>> replies = new ArrayList<Reply<? extends Message>>();
122+
123+
for(JsonReply jsonReply:jaction.getReplies()) {
124+
if(jsonReply.getType().equals("text")) {
125+
replies.add(ReplyFactory.buildTextMessageReply(jsonReply.getText().getBody()));
126+
}
127+
128+
if(jsonReply.getType().equals("picture")) {
129+
replies.add(new PictureMessageReply() {
130+
131+
@Override
132+
public PictureMessage processReply(Message message) {
133+
return PictureMessageBuilder.getInstance()
134+
.setPicUrl(jsonReply.getPicture().getPicUrl())
135+
.build();
136+
}
137+
});
138+
}
139+
if(jsonReply.getType().equals("video")) {
140+
replies.add(new VideoMessageReply() {
141+
142+
@Override
143+
public VideoMessage processReply(Message message) {
144+
return VideoMessageBuilder.getInstance()
145+
.setVideoUrl(jsonReply.getVideo().getVideoUrl())
146+
.build();
147+
}
148+
});
149+
}
150+
if(jsonReply.getType().equals("link")) {
151+
replies.add(new LinkMessageReply() {
152+
153+
@Override
154+
public LinkMessage processReply(Message message) {
155+
return LinkMessageBuilder.getInstance().setText(jsonReply.getLink().getText())
156+
.setTitle(jsonReply.getLink().getTitle())
157+
.setUrl(jsonReply.getLink().getUrl())
158+
.build();
159+
}
160+
});
161+
}
162+
}
163+
164+
return replies;
165+
}
166+
84167
}

src/main/java/co/aurasphere/botmill/kik/incoming/handler/model/JsonAction.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,37 +46,78 @@ public class JsonAction {
4646
/** The output. */
4747
private String input;
4848

49+
/** The replies. */
4950
@SerializedName("replies")
5051
private List<JsonReply> replies;
5152

53+
/**
54+
* Gets the keyboards.
55+
*
56+
* @return the keyboards
57+
*/
5258
public List<JsonKeyboard> getKeyboards() {
5359
return keyboards;
5460
}
5561

62+
/**
63+
* Sets the keyboards.
64+
*
65+
* @param keyboards the new keyboards
66+
*/
5667
public void setKeyboards(List<JsonKeyboard> keyboards) {
5768
this.keyboards = keyboards;
5869
}
5970

71+
/**
72+
* Gets the event.
73+
*
74+
* @return the event
75+
*/
6076
public String getEvent() {
6177
return event;
6278
}
6379

80+
/**
81+
* Sets the event.
82+
*
83+
* @param event the new event
84+
*/
6485
public void setEvent(String event) {
6586
this.event = event;
6687
}
6788

89+
/**
90+
* Gets the input.
91+
*
92+
* @return the input
93+
*/
6894
public String getInput() {
6995
return input;
7096
}
7197

98+
/**
99+
* Sets the input.
100+
*
101+
* @param input the new input
102+
*/
72103
public void setInput(String input) {
73104
this.input = input;
74105
}
75106

107+
/**
108+
* Gets the replies.
109+
*
110+
* @return the replies
111+
*/
76112
public List<JsonReply> getReplies() {
77113
return replies;
78114
}
79115

116+
/**
117+
* Sets the replies.
118+
*
119+
* @param replies the new replies
120+
*/
80121
public void setReplies(List<JsonReply> replies) {
81122
this.replies = replies;
82123
}

0 commit comments

Comments
 (0)