Skip to content

Commit

Permalink
Facebook buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
docwho2 committed Aug 5, 2024
1 parent 4ebed9d commit 5bb964c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 20 deletions.
83 changes: 68 additions & 15 deletions ChatGPT/src/main/java/cloud/cleo/squareup/FaceBookOperations.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package cloud.cleo.squareup;

import static cloud.cleo.squareup.ChatGPTLambda.mapper;
import static cloud.cleo.squareup.functions.PrivateShoppingLink.PRIVATE_SHOPPING_URL;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import lombok.NonNull;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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


/**
* Transfer control of Messenger Thread Session from Bot control to the
* Inbox. Used when end user needs to deal with a real person to resolve
* issue the Bot can't handle. Some people despise Bots, so we need to allow
* getting the Bot out of the conversation.
*
* Transfer control of Messenger Thread Session from Bot control to the Inbox. Used when end user needs to deal with
* a real person to resolve issue the Bot can't handle. Some people despise Bots, so we need to allow getting the
* Bot out of the conversation.
*
* https://developers.facebook.com/docs/messenger-platform/handover-protocol/conversation-control
*
* @param id
* @param id of the recipient
*/
public static void transferToInbox(String id) {
HttpURLConnection connection = null;
Expand Down Expand Up @@ -71,6 +68,61 @@ public static void transferToInbox(String id) {
}
}

/**
* Send our private Shopping URL as a Messenger Button
*
* @param id of the recipient
*/
public static void sendPrivateBookingURL(String id) {
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) getFaceBookURL(null, "me/messages").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.putObject("recipient").put("id", id);

json.putObject("message").putObject("attachment")
.put("type", "template").putObject("payload")
.put("template_type", "button")
.put("text", "Book Private Shopping")
.putArray("buttons")
.addObject()
.put("type", "web_url")
.put("url", "https://" + PRIVATE_SHOPPING_URL)
.put("title", "Book Private Shopping")
.put("webview_height_ratio", "full");

log.debug("Post Payload for URL push" + 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 Messgene URL send result is " + result.toPrettyString());

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

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

/**
* Given a Facebook user Page Scoped ID get the users full name
*
Expand Down Expand Up @@ -112,22 +164,23 @@ public static String getFacebookName(String id) {
}

/**
* Get the base URL for Facebook Graph Operations with page access token
* incorporated.
* Get the base URL for Facebook Graph Operations with page access token incorporated.
*
* @param id
* @param operation
* @return
* @throws MalformedURLException
*/
private static URL getFaceBookURL(@NonNull String id, String operation) throws MalformedURLException {
private static URL getFaceBookURL(String id, String operation) throws MalformedURLException {
final var sb = new StringBuilder("https://graph.facebook.com/");

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

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

// Optional operation
if (operation != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
public abstract class PrivateShoppingLink<Request> extends AbstractFunction {

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



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package cloud.cleo.squareup.functions;

import static cloud.cleo.squareup.ChatGPTLambda.PRIVATE_SHOPPING_TEXT_FUNCTION_NAME;
import cloud.cleo.squareup.FaceBookOperations;
import static cloud.cleo.squareup.enums.ChannelPlatform.FACEBOOK;
import java.util.function.Function;

/**
Expand All @@ -22,25 +24,30 @@ public final String getName() {
protected String getDescription() {
return "Returns a URL for direct booking of Private Shopping";
}

@Override
protected Function getExecutor() {
return (var r) -> {

if (getChannelPlatform().equals(FACEBOOK)) {
FaceBookOperations.sendPrivateBookingURL(getSessionId());
}
return mapper.createObjectNode().put("url", PRIVATE_SHOPPING_URL);
};
}

/**
* This function is Text only
* @return
*
* @return
*/
@Override
protected boolean isVoice() {
return false;
}

/**
*
*
*
* @return
*/
Expand Down

0 comments on commit 5bb964c

Please sign in to comment.