1
1
package cloud .cleo .squareup ;
2
2
3
3
import static cloud .cleo .squareup .ChatGPTLambda .mapper ;
4
+ import static cloud .cleo .squareup .functions .PrivateShoppingLink .PRIVATE_SHOPPING_URL ;
4
5
import java .net .HttpURLConnection ;
5
6
import java .net .MalformedURLException ;
6
7
import java .net .URL ;
7
- import lombok .NonNull ;
8
8
import org .apache .logging .log4j .LogManager ;
9
9
import org .apache .logging .log4j .Logger ;
10
10
11
11
/**
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.
15
14
*
16
15
* @author sjensen
17
16
*/
@@ -20,16 +19,14 @@ public class FaceBookOperations {
20
19
// Initialize the Log4j logger.
21
20
private static final Logger log = LogManager .getLogger (FaceBookOperations .class );
22
21
23
-
24
22
/**
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
+ *
30
27
* https://developers.facebook.com/docs/messenger-platform/handover-protocol/conversation-control
31
28
*
32
- * @param id
29
+ * @param id of the recipient
33
30
*/
34
31
public static void transferToInbox (String id ) {
35
32
HttpURLConnection connection = null ;
@@ -71,6 +68,61 @@ public static void transferToInbox(String id) {
71
68
}
72
69
}
73
70
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
+
74
126
/**
75
127
* Given a Facebook user Page Scoped ID get the users full name
76
128
*
@@ -112,22 +164,23 @@ public static String getFacebookName(String id) {
112
164
}
113
165
114
166
/**
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.
117
168
*
118
169
* @param id
119
170
* @param operation
120
171
* @return
121
172
* @throws MalformedURLException
122
173
*/
123
- private static URL getFaceBookURL (@ NonNull String id , String operation ) throws MalformedURLException {
174
+ private static URL getFaceBookURL (String id , String operation ) throws MalformedURLException {
124
175
final var sb = new StringBuilder ("https://graph.facebook.com/" );
125
176
126
177
// Version of API we are calling
127
178
sb .append ("v18.0/" );
128
179
129
180
// 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
+ }
131
184
132
185
// Optional operation
133
186
if (operation != null ) {
0 commit comments