Skip to content

Commit 5bb964c

Browse files
committed
Facebook buttons
1 parent 4ebed9d commit 5bb964c

File tree

3 files changed

+80
-20
lines changed

3 files changed

+80
-20
lines changed

ChatGPT/src/main/java/cloud/cleo/squareup/FaceBookOperations.java

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package cloud.cleo.squareup;
22

33
import static cloud.cleo.squareup.ChatGPTLambda.mapper;
4+
import static cloud.cleo.squareup.functions.PrivateShoppingLink.PRIVATE_SHOPPING_URL;
45
import java.net.HttpURLConnection;
56
import java.net.MalformedURLException;
67
import java.net.URL;
7-
import lombok.NonNull;
88
import org.apache.logging.log4j.LogManager;
99
import org.apache.logging.log4j.Logger;
1010

1111
/**
12-
* Perform various Facebook operations. Used when Channel is FB. Rather
13-
* than pull in some other dependency, we will just use basic HTTP for all
14-
* Facebook operations.
12+
* Perform various Facebook operations. Used when Channel is FB. Rather than pull in some other dependency, we will just
13+
* use basic HTTP for all Facebook operations.
1514
*
1615
* @author sjensen
1716
*/
@@ -20,16 +19,14 @@ public class FaceBookOperations {
2019
// Initialize the Log4j logger.
2120
private static final Logger log = LogManager.getLogger(FaceBookOperations.class);
2221

23-
2422
/**
25-
* Transfer control of Messenger Thread Session from Bot control to the
26-
* Inbox. Used when end user needs to deal with a real person to resolve
27-
* issue the Bot can't handle. Some people despise Bots, so we need to allow
28-
* getting the Bot out of the conversation.
29-
*
23+
* Transfer control of Messenger Thread Session from Bot control to the Inbox. Used when end user needs to deal with
24+
* a real person to resolve issue the Bot can't handle. Some people despise Bots, so we need to allow getting the
25+
* Bot out of the conversation.
26+
*
3027
* https://developers.facebook.com/docs/messenger-platform/handover-protocol/conversation-control
3128
*
32-
* @param id
29+
* @param id of the recipient
3330
*/
3431
public static void transferToInbox(String id) {
3532
HttpURLConnection connection = null;
@@ -71,6 +68,61 @@ public static void transferToInbox(String id) {
7168
}
7269
}
7370

71+
/**
72+
* Send our private Shopping URL as a Messenger Button
73+
*
74+
* @param id of the recipient
75+
*/
76+
public static void sendPrivateBookingURL(String id) {
77+
HttpURLConnection connection = null;
78+
try {
79+
connection = (HttpURLConnection) getFaceBookURL(null, "me/messages").openConnection();
80+
connection.setRequestMethod("POST");
81+
connection.setRequestProperty("Content-Type", "application/json; utf-8");
82+
connection.setRequestProperty("Accept", "application/json");
83+
connection.setDoOutput(true);
84+
85+
// Construct the payload
86+
var json = mapper.createObjectNode();
87+
88+
// The page scoped user ID of the person chatting with us
89+
json.putObject("recipient").put("id", id);
90+
91+
json.putObject("message").putObject("attachment")
92+
.put("type", "template").putObject("payload")
93+
.put("template_type", "button")
94+
.put("text", "Book Private Shopping")
95+
.putArray("buttons")
96+
.addObject()
97+
.put("type", "web_url")
98+
.put("url", "https://" + PRIVATE_SHOPPING_URL)
99+
.put("title", "Book Private Shopping")
100+
.put("webview_height_ratio", "full");
101+
102+
log.debug("Post Payload for URL push" + json.toPrettyString());
103+
mapper.writeValue(connection.getOutputStream(), json);
104+
105+
final int responseCode = connection.getResponseCode();
106+
log.debug("Facebook Call Response Code: " + responseCode);
107+
108+
final var result = mapper.readTree(connection.getInputStream());
109+
log.debug("FB Messgene URL send result is " + result.toPrettyString());
110+
111+
if (result.findValue("message_id") != null) {
112+
log.debug("Call Succeeded in sending URL in FB Messenger");
113+
} else {
114+
log.debug("Call FAILED to send URL in FB Messenger");
115+
}
116+
117+
} catch (Exception e) {
118+
log.error("Facebook Messenger send failed", e);
119+
} finally {
120+
if (connection != null) {
121+
connection.disconnect();
122+
}
123+
}
124+
}
125+
74126
/**
75127
* Given a Facebook user Page Scoped ID get the users full name
76128
*
@@ -112,22 +164,23 @@ public static String getFacebookName(String id) {
112164
}
113165

114166
/**
115-
* Get the base URL for Facebook Graph Operations with page access token
116-
* incorporated.
167+
* Get the base URL for Facebook Graph Operations with page access token incorporated.
117168
*
118169
* @param id
119170
* @param operation
120171
* @return
121172
* @throws MalformedURLException
122173
*/
123-
private static URL getFaceBookURL(@NonNull String id, String operation) throws MalformedURLException {
174+
private static URL getFaceBookURL(String id, String operation) throws MalformedURLException {
124175
final var sb = new StringBuilder("https://graph.facebook.com/");
125176

126177
// Version of API we are calling
127178
sb.append("v18.0/");
128179

129180
// ID for the entity we are using (Page ID, or Page scoped User ID)
130-
sb.append(id);
181+
if (id != null) {
182+
sb.append(id);
183+
}
131184

132185
// Optional operation
133186
if (operation != null) {

ChatGPT/src/main/java/cloud/cleo/squareup/functions/PrivateShoppingLink.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
public abstract class PrivateShoppingLink<Request> extends AbstractFunction {
1616

17-
protected final static String PRIVATE_SHOPPING_URL = WEBSITE_URL + "/book";
17+
public final static String PRIVATE_SHOPPING_URL = WEBSITE_URL + "/book";
1818

1919

2020

ChatGPT/src/main/java/cloud/cleo/squareup/functions/PrivateShoppingLinkText.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package cloud.cleo.squareup.functions;
66

77
import static cloud.cleo.squareup.ChatGPTLambda.PRIVATE_SHOPPING_TEXT_FUNCTION_NAME;
8+
import cloud.cleo.squareup.FaceBookOperations;
9+
import static cloud.cleo.squareup.enums.ChannelPlatform.FACEBOOK;
810
import java.util.function.Function;
911

1012
/**
@@ -22,25 +24,30 @@ public final String getName() {
2224
protected String getDescription() {
2325
return "Returns a URL for direct booking of Private Shopping";
2426
}
25-
27+
2628
@Override
2729
protected Function getExecutor() {
2830
return (var r) -> {
31+
32+
if (getChannelPlatform().equals(FACEBOOK)) {
33+
FaceBookOperations.sendPrivateBookingURL(getSessionId());
34+
}
2935
return mapper.createObjectNode().put("url", PRIVATE_SHOPPING_URL);
3036
};
3137
}
3238

3339
/**
3440
* This function is Text only
35-
* @return
41+
*
42+
* @return
3643
*/
3744
@Override
3845
protected boolean isVoice() {
3946
return false;
4047
}
41-
48+
4249
/**
43-
*
50+
*
4451
*
4552
* @return
4653
*/

0 commit comments

Comments
 (0)