Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
New Referral Webhooks on CTM Lead Generation Ads (#113)
Browse files Browse the repository at this point in the history
Summary:
Implement the new Referal Webhooks available to Lead Generation with Click to Messenger Ads

As announced on Nov 9, 2022 new referral webhooks are sent on Click To Messenger, Lead Generation Ads
https://developers.facebook.com/docs/messenger-platform/discovery/lead-generation-ads-in-messenger

This change supports the bot response after a lead is complete. It implements the response that happend after this lead ad ends:
https://www.facebook.com/ads/experience/confirmation/?experience_id=2170497373124589

The demo ad is also linked in the documentation mentioned above as the Sample Lead Generation Ad.

Pull Request resolved: #113

Reviewed By: gitLee47

Differential Revision: D41205127

Pulled By: jorgeluiso

fbshipit-source-id: 4c1f58c61a7c9a69bd9eced8b20d758fbe67a950
  • Loading branch information
jorgeluiso authored and facebook-github-bot committed Nov 11, 2022
1 parent e31f384 commit ecb454f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"path": "^0.12.7"
},
"engines": {
"node": ">=12.0 <=18.9.x"

This comment has been minimized.

Copy link
@katoqiioo

katoqiioo Feb 11, 2023

="nodo": ">=16.0"

This comment has been minimized.

Copy link
@katoqiioo

katoqiioo Feb 11, 2023

="nodo": ">=16.0"

"node": ">=12.0"
},
"repository": {
"url": "https://github.com/fbsamples/original-coast-clothing"
Expand Down
14 changes: 7 additions & 7 deletions services/lead.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ module.exports = class Lead {
this.webhookEvent = webhookEvent;
}

handleHandover(metadata) {
switch (metadata) {
case "messenger_lead_gen_complete":
return this.responseForLeadRef();
case "messenger_lead_gen_incomplete":
return Response.genNuxMessage(this.user);
handleReferral(type) {
switch (type) {
case "LEAD_COMPLETE":
return this.responseForLeadComplete();
case "LEAD_INCOMPLETE":
return null;
}

return;
}

responseForLeadRef() {
responseForLeadComplete() {
var responses = [
Response.genTextWithPersona(
i18n.__("wholesale_leadgen.intro", {
Expand Down
24 changes: 17 additions & 7 deletions services/receive.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,20 @@ module.exports = class Receive {
// Handles referral events
handleReferral() {
// Get the payload of the postback
let payload = this.webhookEvent.referral.ref.toUpperCase();
if (payload.trim().length === 0) {
console.log("Ignore referral with empty payload");
return null;
let type = this.webhookEvent.referral.type;
if (type === "LEAD_COMPLETE" || type === "LEAD_INCOMPLETE") {
let lead = new Lead(this.user, this.webhookEvent);
return lead.handleReferral(type);
}
return this.handlePayload(payload);
if (type === "OPEN_THREAD") {
let payload = this.webhookEvent.referral.ref.toUpperCase();
if (payload.trim().length === 0) {
console.log("Ignore referral with empty payload");
return null;
}
return this.handlePayload(payload);
}
console.log("Ignore referral of invalid type");
}

// Handles optins events
Expand Down Expand Up @@ -213,8 +221,10 @@ module.exports = class Receive {
}
const lead_gen_app_id = 413038776280800; // App id for Messenger Lead Ads
if (previous_owner_app_id === lead_gen_app_id) {
let lead = new Lead(this.user, this.webhookEvent);
return lead.handleHandover(metadata);
console.log(
"Received a handover event from Lead Generation Ad will handle Referral Webhook Instead"
);
return;
}
// We have thread control but no context on what to do, default to New User Experience
return Response.genNuxMessage(this.user);
Expand Down

0 comments on commit ecb454f

Please sign in to comment.