Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Support fb send upload #870

Closed
wants to merge 3 commits into from
Closed

Conversation

siygle
Copy link

@siygle siygle commented Jun 3, 2017

Solved #233

Use filedata to assign upload file/image describe on Content Types page in FB messenger doc. It will do a basic file validate (fs.existsSync) then stream the file using form-data.

e.g., upload png file

bot.reply(message, {
  attachment: {
    type: 'image',
    payload: {}
  },
  filedata: UPLOAD_FILE_PATH
})

requestObj = {
method: 'POST',
formData: prepareFormdata,
uri: 'https://' + api_host + '/v2.6/me/messages'
Copy link
Contributor

@jonchurch jonchurch Jun 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't used the upload api myself, but the docs say the url for uploads is:
v2.6/me/message_attachments

Ahh, misunderstood the purpose of this PR! Carry on 😉

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonchurch you made me think to bring the functionality that you misunderstood to Botkit 😈

If you don't already start to work on 😬

Copy link
Collaborator

@ouadie-lahdioui ouadie-lahdioui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello champion,

Thanks for this awesome work 👍

uri: 'https://' + api_host + '/v2.6/me/messages'
},
function(err, res, body) {
var requestObj = {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about initializing requestObject with POST method and uri to avoid duplication ?

var requestObj = {
      method: 'POST',
      uri: 'https://' + api_host + '/v2.6/me/messages'
};

prepareFormdata[key] = facebook_message[key];
}
}
requestObj = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you initialised the requestObj with the http verbe POST and the uri, you just need to set here the formData :

requestObj.formData = prepareFormdata;

requestObj = {
method: 'POST',
formData: prepareFormdata,
uri: 'https://' + api_host + '/v2.6/me/messages'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonchurch you made me think to bring the functionality that you misunderstood to Botkit 😈

If you don't already start to work on 😬

if ('filedata' in facebook_message) {
var prepareFormdata = {};
for (key in facebook_message) {
if (key !== 'filedata' && typeof facebook_message[key] === 'object') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code is really awesome, but what do you think about starting with checking for filedata to avoid a slightly duplication ?

if (key === 'filedata') {
   if (!fs.existsSync(facebook_message[key])) return cb(new Error('filedata not exist'));
       prepareFormdata[key] = fs.createReadStream(facebook_message[key]);
   } else if (typeof facebook_message[key] === 'object') {
       prepareFormdata[key] = JSON.stringify(facebook_message[key]);
   } else {
       prepareFormdata[key] = facebook_message[key];
   }

@stale
Copy link

stale bot commented Oct 22, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 22, 2018
@stale stale bot closed this Oct 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants