-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from Cloudkibo/anisha_work
getting started
- Loading branch information
Showing
2 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
163 changes: 163 additions & 0 deletions
163
server/webhook_subscriptions/messenger/getStarted/logiclayer.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
function prepareSendAPIPayload (subscriberId, body, fname, lname, isResponse) { | ||
let messageType = isResponse ? 'RESPONSE' : 'UPDATE' | ||
let payload = {} | ||
let text = body.text | ||
if (body.componentType === 'text' && !body.buttons) { | ||
if (body.text.includes('{{user_full_name}}') || body.text.includes('[Username]')) { | ||
text = text.replace( | ||
'{{user_full_name}}', fname + ' ' + lname) | ||
} | ||
if (body.text.includes('{{user_first_name}}')) { | ||
text = text.replace( | ||
'{{user_first_name}}', fname) | ||
} | ||
if (body.text.includes('{{user_last_name}}')) { | ||
text = text.replace( | ||
'{{user_last_name}}', lname) | ||
} | ||
payload = { | ||
'messaging_type': messageType, | ||
'recipient': JSON.stringify({ | ||
'id': subscriberId | ||
}), | ||
'message': JSON.stringify({ | ||
'text': text, | ||
'metadata': 'This is a meta data' | ||
}) | ||
} | ||
return payload | ||
} else if (body.componentType === 'text' && body.buttons) { | ||
if (body.text.includes('{{user_full_name}}') || body.text.includes('[Username]')) { | ||
text = text.replace( | ||
'{{user_full_name}}', fname + ' ' + lname) | ||
} | ||
if (body.text.includes('{{user_first_name}}')) { | ||
text = text.replace( | ||
'{{user_first_name}}', fname) | ||
} | ||
if (body.text.includes('{{user_last_name}}')) { | ||
text = text.replace( | ||
'{{user_last_name}}', lname) | ||
} | ||
payload = { | ||
'messaging_type': messageType, | ||
'recipient': JSON.stringify({ | ||
'id': subscriberId | ||
}), | ||
'message': JSON.stringify({ | ||
'attachment': { | ||
'type': 'template', | ||
'payload': { | ||
'template_type': 'button', | ||
'text': text, | ||
'buttons': body.buttons | ||
} | ||
} | ||
}) | ||
} | ||
} else if (['image', 'audio', 'file', 'video'].indexOf( | ||
body.componentType) > -1) { | ||
let dir = path.resolve(__dirname, '../../../../broadcastFiles/userfiles') | ||
let fileReaderStream | ||
if (body.componentType === 'file') { | ||
fileReaderStream = fs.createReadStream(dir + '/' + body.fileurl.name) | ||
} else { | ||
fileReaderStream = fs.createReadStream(dir + '/' + body.fileurl.id) | ||
} | ||
|
||
payload = { | ||
'messaging_type': messageType, | ||
'recipient': JSON.stringify({ | ||
'id': subscriberId | ||
}), | ||
'message': JSON.stringify({ | ||
'attachment': { | ||
'type': body.componentType, | ||
'payload': {} | ||
} | ||
}), | ||
'filedata': fileReaderStream | ||
} | ||
return payload | ||
// todo test this one. we are not removing as we need to keep it for live chat | ||
// if (!isForLiveChat) deleteFile(body.fileurl) | ||
} else if (['gif', 'sticker', 'thumbsUp'].indexOf( | ||
body.componentType) > -1) { | ||
payload = { | ||
'messaging_type': messageType, | ||
'recipient': JSON.stringify({ | ||
'id': subscriberId | ||
}), | ||
'message': JSON.stringify({ | ||
'attachment': { | ||
'type': 'image', | ||
'payload': { | ||
'url': body.fileurl | ||
} | ||
} | ||
}) | ||
} | ||
} else if (body.componentType === 'card') { | ||
payload = { | ||
'messaging_type': messageType, | ||
'recipient': JSON.stringify({ | ||
'id': subscriberId | ||
}), | ||
'message': JSON.stringify({ | ||
'attachment': { | ||
'type': 'template', | ||
'payload': { | ||
'template_type': 'generic', | ||
'elements': [ | ||
{ | ||
'title': body.title, | ||
'image_url': body.image_url, | ||
'subtitle': body.description, | ||
'buttons': body.buttons | ||
} | ||
] | ||
} | ||
} | ||
}) | ||
} | ||
} else if (body.componentType === 'gallery') { | ||
payload = { | ||
'messaging_type': messageType, | ||
'recipient': JSON.stringify({ | ||
'id': subscriberId | ||
}), | ||
'message': JSON.stringify({ | ||
'attachment': { | ||
'type': 'template', | ||
'payload': { | ||
'template_type': 'generic', | ||
'elements': body.cards | ||
} | ||
} | ||
}) | ||
} | ||
} else if (body.componentType === 'list') { | ||
payload = { | ||
'messaging_type': messageType, | ||
'recipient': JSON.stringify({ | ||
'id': subscriberId | ||
}), | ||
'message': JSON.stringify({ | ||
'attachment': { | ||
'type': 'template', | ||
'payload': { | ||
'template_type': 'list', | ||
'top_element_style': body.topElementStyle, | ||
'elements': body.listItems, | ||
'buttons': body.buttons | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
return payload | ||
} | ||
exports.prepareSendAPIPayload = prepareSendAPIPayload |