|
7 | 7 | import in.strollup.fb.contract.Messaging; |
8 | 8 | import in.strollup.fb.contract.Payload; |
9 | 9 | import in.strollup.fb.contract.Recipient; |
| 10 | +import in.strollup.fb.profile.FbProfile; |
10 | 11 | import in.strollup.fb.servlet.WebHookServlet; |
11 | 12 |
|
| 13 | +import java.io.BufferedReader; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.InputStreamReader; |
| 16 | +import java.net.URL; |
12 | 17 | import java.util.ArrayList; |
13 | 18 | import java.util.List; |
14 | 19 |
|
| 20 | +import org.apache.commons.lang3.StringUtils; |
| 21 | + |
15 | 22 | import com.google.gson.Gson; |
16 | 23 |
|
17 | 24 | /** |
|
28 | 35 | public class FbChatHelper { |
29 | 36 | private String vr = "VR headsets"; |
30 | 37 | private List<TitleSubTitle> vrImageUrls; |
| 38 | + private static String profileLink = "https://graph.facebook.com/v2.6/SENDER_ID?access_token=" |
| 39 | + + WebHookServlet.PAGE_TOKEN; |
31 | 40 |
|
32 | 41 | public FbChatHelper() { |
33 | 42 | vrImageUrls = new ArrayList<>(); |
@@ -70,8 +79,8 @@ public void setSubTitle(String subTitle) { |
70 | 79 | } |
71 | 80 |
|
72 | 81 | /** |
73 | | - * methos which analyze the postbacks ie. the button clicks sent by senderId |
74 | | - * and replies according to it. |
| 82 | + * methods which analyze the postbacks ie. the button clicks sent by |
| 83 | + * senderId and replies according to it. |
75 | 84 | * |
76 | 85 | * @param senderId |
77 | 86 | * @param text |
@@ -102,8 +111,11 @@ public List<String> getPostBackReplies(String senderId, String text) { |
102 | 111 | */ |
103 | 112 | public List<String> getReplies(String senderId, String text) { |
104 | 113 | List<String> replies = new ArrayList<String>(); |
| 114 | + String link = StringUtils.replace(profileLink, "SENDER_ID", senderId); |
| 115 | + FbProfile profile = getObjectFromUrl(link, FbProfile.class); |
105 | 116 |
|
106 | | - String msg = "Hello, I've received msg: " + text; |
| 117 | + String msg = "Hello " + profile.getFirstName() |
| 118 | + + ", I've received msg: " + text; |
107 | 119 | Message fbMsg = getMsg(msg); |
108 | 120 | String fbReply = getJsonReply(senderId, fbMsg); |
109 | 121 | replies.add(fbReply); |
@@ -232,4 +244,36 @@ private List<Button> getButtons(String title, String url) { |
232 | 244 | return buttons; |
233 | 245 | } |
234 | 246 |
|
| 247 | + /** |
| 248 | + * Returns object of type clazz from an json api link |
| 249 | + * |
| 250 | + * @param link |
| 251 | + * @param clazz |
| 252 | + * @return |
| 253 | + * @throws Exception |
| 254 | + */ |
| 255 | + private <T> T getObjectFromUrl(String link, Class<T> clazz) { |
| 256 | + T t = null; |
| 257 | + URL url; |
| 258 | + String jsonString = ""; |
| 259 | + try { |
| 260 | + url = new URL(link); |
| 261 | + BufferedReader in = new BufferedReader(new InputStreamReader( |
| 262 | + url.openStream())); |
| 263 | + |
| 264 | + String inputLine; |
| 265 | + while ((inputLine = in.readLine()) != null) { |
| 266 | + jsonString = jsonString + inputLine; |
| 267 | + } |
| 268 | + in.close(); |
| 269 | + } catch (IOException e) { |
| 270 | + // TODO Auto-generated catch block |
| 271 | + e.printStackTrace(); |
| 272 | + } |
| 273 | + if (!StringUtils.isEmpty(jsonString)) { |
| 274 | + Gson gson = new Gson(); |
| 275 | + t = gson.fromJson(jsonString, clazz); |
| 276 | + } |
| 277 | + return t; |
| 278 | + } |
235 | 279 | } |
0 commit comments