Skip to content

Commit

Permalink
Facebook persistent menu
Browse files Browse the repository at this point in the history
  • Loading branch information
docwho2 committed Aug 5, 2024
1 parent 22fc9f2 commit 8457d4a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 8 deletions.
66 changes: 61 additions & 5 deletions ChatGPT/src/main/java/cloud/cleo/squareup/FaceBookOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static cloud.cleo.squareup.ChatGPTLambda.mapper;
import static cloud.cleo.squareup.functions.PrivateShoppingLink.PRIVATE_SHOPPING_URL;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -69,9 +68,66 @@ public static void transferToInbox(String id) {
}
}


/**
* Adds a static menu button, so when bot calls for the URL, will persist as menu item.
* https://developers.facebook.com/docs/messenger-platform/send-messages/persistent-menu/
* @param id
* @return
*/
public static void addPrivateShoppingMenu(String id) {
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) getFaceBookURL(null, "me/custom_user_settings").openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);

// Construct the payload
var json = mapper.createObjectNode();

// The page scoped user ID of the person chatting with us
json.put("psid", id);

json.putArray("persistent_menu")
.addObject()
.put("locale", "default")
.put("composer_input_disabled", false)
.putArray("call_to_actions")
.addObject()
.put("type", "web_url")
.put("url", "https://" + PRIVATE_SHOPPING_URL)
.put("title", "Book Appointment Now!")
.put("webview_height_ratio", "full");

log.debug("Post Payload for Private Shopping Menu" + json.toPrettyString());
mapper.writeValue(connection.getOutputStream(), json);

final int responseCode = connection.getResponseCode();
log.debug("Facebook Call Response Code: " + responseCode);

final var result = mapper.readTree(connection.getInputStream());
log.debug("FB Private Shopping Menu send result is " + result.toPrettyString());

if (result.findValue("message_id") != null) {
log.debug("Call Succeeded in sending Private Shopping Menu");
} else {
log.debug("Call FAILED to send Private Shopping Menu");
}

} catch (Exception e) {
log.error("Facebook Messenger Private Shopping Menu send failed", e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
}

/**
* Send our private Shopping URL as a Messenger Button
*
*
* @param id of the recipient
* @return true if successfully sent
*/
Expand All @@ -97,7 +153,7 @@ public static boolean sendPrivateBookingURL(String id) {
.putArray("buttons")
.addObject()
.put("type", "web_url")
.put("messenger_extensions", "true")
//.put("messenger_extensions", true)
.put("url", "https://" + PRIVATE_SHOPPING_URL)
.put("title", "Book Now!")
.put("webview_height_ratio", "full");
Expand Down Expand Up @@ -180,11 +236,11 @@ private static URL getFaceBookURL(String id, String operation) throws MalformedU
final var sb = new StringBuilder("https://graph.facebook.com/");

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

// ID for the entity we are using (Page ID, or Page scoped User ID)
if (id != null) {
sb.append(id);
sb.append('/').append(id);
}

// Optional operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ protected Function getExecutor() {
return (var r) -> {

if (getChannelPlatform().equals(FACEBOOK)) {
if ( FaceBookOperations.sendPrivateBookingURL(getSessionId()) ) {
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");
}
// Persist the shopping link as a menu choice
FaceBookOperations.addPrivateShoppingMenu(getSessionId());
}
return mapper.createObjectNode().put("url", PRIVATE_SHOPPING_URL);
};
Expand Down
25 changes: 25 additions & 0 deletions ChatGPT/src/main/resources/fb_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"commands": [
{
"locale": "default",
"commands": [
{
"name": "Hours",
"description": "When our store is open/closed"
},
{
"name": "Location",
"description": "Our business address"
},
{
"name": "Person",
"description": "Transfer conversation to a real person (store staff)"
},
{
"name": "Personl Shopping",
"description": "Infomation about booking a personal shopping experience"
}
]
}
]
}
26 changes: 26 additions & 0 deletions ChatGPT/src/main/resources/fb_commands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

curl -X POST -H "Content-Type: application/json" -d '{
"commands": [
{
"locale": "default",
"commands": [
{
"name": "Hours",
"description": "When our store is open/closed"
},
{
"name": "Location",
"description": "Our business address"
},
{
"name": "Person",
"description": "Transfer conversation to a real person (store staff)"
},
{
"name": "Personal-Shopping",
"description": "Infomation about booking a personal shopping experience"
}
]
}
]
}' "https://graph.facebook.com/v20.0/me/messenger_profile?access_token="

0 comments on commit 8457d4a

Please sign in to comment.