-
-
Save anton-dudarev/a47d3f3a8ae509dacc888cc64193516e to your computer and use it in GitHub Desktop.
Dialogflow responses for FB Messenger integration.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Dialogflow = { | |
fulfillmentMessages: [], | |
text: function (txt) { | |
// console.log(typeof txt); | |
if (typeof txt === 'string') { | |
txt = `${txt}` | |
} else { | |
txt = `${txt.title}\n\n${txt.upvotes} upvotes | ${txt.comments} comments\n\n` | |
} | |
this.fulfillmentMessages.push({ | |
"payload": { | |
"facebook": { | |
"text": txt | |
} | |
} | |
}); | |
return this; | |
}, | |
image: function (url) { | |
let data = { | |
"payload": { | |
"facebook": { | |
"attachment": { | |
"type": 'image', | |
"payload": { | |
"url": url | |
} | |
} | |
} | |
} | |
} | |
if (url) { | |
this.fulfillmentMessages.push(data); | |
} else { | |
console.error('No image url found.') | |
} | |
return this; | |
}, | |
video: function (url) { | |
let data = { | |
"payload": { | |
"facebook": { | |
"attachment": { | |
"type": 'video', | |
"payload": { | |
"url": url | |
} | |
} | |
} | |
} | |
} | |
if (url) { | |
this.fulfillmentMessages.push(data); | |
} else { | |
console.error('No video url found.') | |
} | |
return this; | |
}, | |
audio: function (url) { | |
let data = { | |
"payload": { | |
"facebook": { | |
"attachment": { | |
"type": 'audio', | |
"payload": { | |
"url": url | |
} | |
} | |
} | |
} | |
} | |
if (url) { | |
this.fulfillmentMessages.push(data); | |
} else { | |
console.error('No audio url found.') | |
} | |
return this; | |
}, | |
card: function (card) { | |
let data = { | |
"card": { | |
"title": card.title, | |
"subtitle": card.subtitle, | |
"imageUri": card.url, | |
"buttons": card.buttons | |
}, | |
"platform": "FACEBOOK" | |
} | |
if (card.title) { | |
this.fulfillmentMessages.push(data); | |
} else { | |
console.error('No card details found.'); | |
} | |
return this; | |
}, | |
qr: function (text, replies) { | |
const qr_replies = (replies) => { | |
let data = []; | |
for (const key in replies) { | |
data.push({ | |
"content_type": "text", | |
"title": replies[key], | |
"payload": replies[key] | |
}) | |
} | |
return data; | |
} | |
let data = { | |
"payload": { | |
"facebook": { | |
"text": text, | |
"quick_replies": qr_replies(replies) | |
} | |
} | |
} | |
if (text === 'location') { | |
data = { | |
"payload": { | |
"facebook": { | |
"text": 'Please share your location.', | |
"quick_replies": [ | |
{ | |
"content_type": "location" | |
} | |
] | |
} | |
} | |
}; | |
} | |
if (text) { | |
this.fulfillmentMessages.push(data); | |
} else { | |
console.error('No quick replies found.'); | |
} | |
return this; | |
}, | |
anyMsg: function (data) { | |
if (data.domain === 'v.redd.it') { | |
data.url = data.url + '/DASH_600_K' | |
} | |
let ext_video = data.url.match(/mp4/gi); | |
let ext_image = data.url.match(/jpg|png|gif/gi); | |
if (ext_image) { | |
Dialogflow | |
.image(data.url) | |
.text(data) | |
} else if (ext_video) { | |
Dialogflow | |
.video(data.url) | |
.text(data.title) | |
} else { | |
Dialogflow.text(data.title) | |
} | |
return this; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment