Skip to content

Commit 44685d8

Browse files
committed
added Fb public profile for personal message
1 parent 11610e5 commit 44685d8

3 files changed

Lines changed: 187 additions & 4 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package in.strollup.fb.profile;
2+
3+
import javax.annotation.Generated;
4+
5+
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
7+
import com.google.gson.annotations.Expose;
8+
import com.google.gson.annotations.SerializedName;
9+
10+
@Generated("org.jsonschema2pojo")
11+
public class FbProfile {
12+
13+
@SerializedName("first_name")
14+
@Expose
15+
private String firstName;
16+
@SerializedName("last_name")
17+
@Expose
18+
private String lastName;
19+
@SerializedName("profile_pic")
20+
@Expose
21+
private String profilePic;
22+
@SerializedName("locale")
23+
@Expose
24+
private String locale;
25+
@SerializedName("timezone")
26+
@Expose
27+
private String timezone;
28+
@SerializedName("gender")
29+
@Expose
30+
private String gender;
31+
32+
/**
33+
*
34+
* @return The firstName
35+
*/
36+
public String getFirstName() {
37+
return firstName;
38+
}
39+
40+
/**
41+
*
42+
* @param firstName
43+
* The first_name
44+
*/
45+
public void setFirstName(String firstName) {
46+
this.firstName = firstName;
47+
}
48+
49+
/**
50+
*
51+
* @return The lastName
52+
*/
53+
public String getLastName() {
54+
return lastName;
55+
}
56+
57+
/**
58+
*
59+
* @param lastName
60+
* The last_name
61+
*/
62+
public void setLastName(String lastName) {
63+
this.lastName = lastName;
64+
}
65+
66+
/**
67+
*
68+
* @return The profilePic
69+
*/
70+
public String getProfilePic() {
71+
return profilePic;
72+
}
73+
74+
/**
75+
*
76+
* @param profilePic
77+
* The profile_pic
78+
*/
79+
public void setProfilePic(String profilePic) {
80+
this.profilePic = profilePic;
81+
}
82+
83+
/**
84+
*
85+
* @return The locale
86+
*/
87+
public String getLocale() {
88+
return locale;
89+
}
90+
91+
/**
92+
*
93+
* @param locale
94+
* The locale
95+
*/
96+
public void setLocale(String locale) {
97+
this.locale = locale;
98+
}
99+
100+
/**
101+
*
102+
* @return The timezone
103+
*/
104+
public String getTimezone() {
105+
return timezone;
106+
}
107+
108+
/**
109+
*
110+
* @param timezone
111+
* The timezone
112+
*/
113+
public void setTimezone(String timezone) {
114+
this.timezone = timezone;
115+
}
116+
117+
/**
118+
*
119+
* @return The gender
120+
*/
121+
public String getGender() {
122+
return gender;
123+
}
124+
125+
/**
126+
*
127+
* @param gender
128+
* The gender
129+
*/
130+
public void setGender(String gender) {
131+
this.gender = gender;
132+
}
133+
134+
@Override
135+
public String toString() {
136+
return ToStringBuilder.reflectionToString(this);
137+
}
138+
139+
}

src/main/java/in/strollup/fb/servlet/WebHookServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class WebHookServlet extends HttpServlet {
3737
private static final long serialVersionUID = -2326475169699351010L;
3838

3939
/************* FB Chat Bot variables *************************/
40-
private static final String PAGE_TOKEN = "YOUR_FB_PAGE_TOKEN";
40+
public static final String PAGE_TOKEN = "YOUR_FB_PAGE_TOKEN";
4141
private static final String VERIFY_TOKEN = "whatever_string_you_or_your_friends_wish";
4242
private static final String FB_MSG_URL = "https://graph.facebook.com/v2.6/me/messages?access_token="
4343
+ PAGE_TOKEN;

src/main/java/in/strollup/fb/utils/FbChatHelper.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
import in.strollup.fb.contract.Messaging;
88
import in.strollup.fb.contract.Payload;
99
import in.strollup.fb.contract.Recipient;
10+
import in.strollup.fb.profile.FbProfile;
1011
import in.strollup.fb.servlet.WebHookServlet;
1112

13+
import java.io.BufferedReader;
14+
import java.io.IOException;
15+
import java.io.InputStreamReader;
16+
import java.net.URL;
1217
import java.util.ArrayList;
1318
import java.util.List;
1419

20+
import org.apache.commons.lang3.StringUtils;
21+
1522
import com.google.gson.Gson;
1623

1724
/**
@@ -28,6 +35,8 @@
2835
public class FbChatHelper {
2936
private String vr = "VR headsets";
3037
private List<TitleSubTitle> vrImageUrls;
38+
private static String profileLink = "https://graph.facebook.com/v2.6/SENDER_ID?access_token="
39+
+ WebHookServlet.PAGE_TOKEN;
3140

3241
public FbChatHelper() {
3342
vrImageUrls = new ArrayList<>();
@@ -70,8 +79,8 @@ public void setSubTitle(String subTitle) {
7079
}
7180

7281
/**
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.
7584
*
7685
* @param senderId
7786
* @param text
@@ -102,8 +111,11 @@ public List<String> getPostBackReplies(String senderId, String text) {
102111
*/
103112
public List<String> getReplies(String senderId, String text) {
104113
List<String> replies = new ArrayList<String>();
114+
String link = StringUtils.replace(profileLink, "SENDER_ID", senderId);
115+
FbProfile profile = getObjectFromUrl(link, FbProfile.class);
105116

106-
String msg = "Hello, I've received msg: " + text;
117+
String msg = "Hello " + profile.getFirstName()
118+
+ ", I've received msg: " + text;
107119
Message fbMsg = getMsg(msg);
108120
String fbReply = getJsonReply(senderId, fbMsg);
109121
replies.add(fbReply);
@@ -232,4 +244,36 @@ private List<Button> getButtons(String title, String url) {
232244
return buttons;
233245
}
234246

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+
}
235279
}

0 commit comments

Comments
 (0)