Skip to content

Commit 8457d4a

Browse files
committed
Facebook persistent menu
1 parent 22fc9f2 commit 8457d4a

File tree

4 files changed

+114
-8
lines changed

4 files changed

+114
-8
lines changed

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

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static cloud.cleo.squareup.ChatGPTLambda.mapper;
44
import static cloud.cleo.squareup.functions.PrivateShoppingLink.PRIVATE_SHOPPING_URL;
5-
import java.math.BigDecimal;
65
import java.net.HttpURLConnection;
76
import java.net.MalformedURLException;
87
import java.net.URL;
@@ -69,9 +68,66 @@ public static void transferToInbox(String id) {
6968
}
7069
}
7170

71+
72+
/**
73+
* Adds a static menu button, so when bot calls for the URL, will persist as menu item.
74+
* https://developers.facebook.com/docs/messenger-platform/send-messages/persistent-menu/
75+
* @param id
76+
* @return
77+
*/
78+
public static void addPrivateShoppingMenu(String id) {
79+
HttpURLConnection connection = null;
80+
try {
81+
connection = (HttpURLConnection) getFaceBookURL(null, "me/custom_user_settings").openConnection();
82+
connection.setRequestMethod("POST");
83+
connection.setRequestProperty("Content-Type", "application/json; utf-8");
84+
connection.setRequestProperty("Accept", "application/json");
85+
connection.setDoOutput(true);
86+
87+
// Construct the payload
88+
var json = mapper.createObjectNode();
89+
90+
// The page scoped user ID of the person chatting with us
91+
json.put("psid", id);
92+
93+
json.putArray("persistent_menu")
94+
.addObject()
95+
.put("locale", "default")
96+
.put("composer_input_disabled", false)
97+
.putArray("call_to_actions")
98+
.addObject()
99+
.put("type", "web_url")
100+
.put("url", "https://" + PRIVATE_SHOPPING_URL)
101+
.put("title", "Book Appointment Now!")
102+
.put("webview_height_ratio", "full");
103+
104+
log.debug("Post Payload for Private Shopping Menu" + json.toPrettyString());
105+
mapper.writeValue(connection.getOutputStream(), json);
106+
107+
final int responseCode = connection.getResponseCode();
108+
log.debug("Facebook Call Response Code: " + responseCode);
109+
110+
final var result = mapper.readTree(connection.getInputStream());
111+
log.debug("FB Private Shopping Menu send result is " + result.toPrettyString());
112+
113+
if (result.findValue("message_id") != null) {
114+
log.debug("Call Succeeded in sending Private Shopping Menu");
115+
} else {
116+
log.debug("Call FAILED to send Private Shopping Menu");
117+
}
118+
119+
} catch (Exception e) {
120+
log.error("Facebook Messenger Private Shopping Menu send failed", e);
121+
} finally {
122+
if (connection != null) {
123+
connection.disconnect();
124+
}
125+
}
126+
}
127+
72128
/**
73129
* Send our private Shopping URL as a Messenger Button
74-
*
130+
*
75131
* @param id of the recipient
76132
* @return true if successfully sent
77133
*/
@@ -97,7 +153,7 @@ public static boolean sendPrivateBookingURL(String id) {
97153
.putArray("buttons")
98154
.addObject()
99155
.put("type", "web_url")
100-
.put("messenger_extensions", "true")
156+
//.put("messenger_extensions", true)
101157
.put("url", "https://" + PRIVATE_SHOPPING_URL)
102158
.put("title", "Book Now!")
103159
.put("webview_height_ratio", "full");
@@ -180,11 +236,11 @@ private static URL getFaceBookURL(String id, String operation) throws MalformedU
180236
final var sb = new StringBuilder("https://graph.facebook.com/");
181237

182238
// Version of API we are calling
183-
sb.append("v18.0/");
239+
sb.append("v18.0");
184240

185241
// ID for the entity we are using (Page ID, or Page scoped User ID)
186242
if (id != null) {
187-
sb.append(id);
243+
sb.append('/').append(id);
188244
}
189245

190246
// Optional operation

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ protected Function getExecutor() {
3030
return (var r) -> {
3131

3232
if (getChannelPlatform().equals(FACEBOOK)) {
33-
if ( FaceBookOperations.sendPrivateBookingURL(getSessionId()) ) {
34-
return mapper.createObjectNode().put("message", "URL was just sent out of band successfully as a Messenger Button, so just make reference to the URL button above as part of your response");
35-
}
33+
// Persist the shopping link as a menu choice
34+
FaceBookOperations.addPrivateShoppingMenu(getSessionId());
3635
}
3736
return mapper.createObjectNode().put("url", PRIVATE_SHOPPING_URL);
3837
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"commands": [
3+
{
4+
"locale": "default",
5+
"commands": [
6+
{
7+
"name": "Hours",
8+
"description": "When our store is open/closed"
9+
},
10+
{
11+
"name": "Location",
12+
"description": "Our business address"
13+
},
14+
{
15+
"name": "Person",
16+
"description": "Transfer conversation to a real person (store staff)"
17+
},
18+
{
19+
"name": "Personl Shopping",
20+
"description": "Infomation about booking a personal shopping experience"
21+
}
22+
]
23+
}
24+
]
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
curl -X POST -H "Content-Type: application/json" -d '{
3+
"commands": [
4+
{
5+
"locale": "default",
6+
"commands": [
7+
{
8+
"name": "Hours",
9+
"description": "When our store is open/closed"
10+
},
11+
{
12+
"name": "Location",
13+
"description": "Our business address"
14+
},
15+
{
16+
"name": "Person",
17+
"description": "Transfer conversation to a real person (store staff)"
18+
},
19+
{
20+
"name": "Personal-Shopping",
21+
"description": "Infomation about booking a personal shopping experience"
22+
}
23+
]
24+
}
25+
]
26+
}' "https://graph.facebook.com/v20.0/me/messenger_profile?access_token="

0 commit comments

Comments
 (0)