diff --git a/docs/readme-facebook.md b/docs/readme-facebook.md index 3c335ad48..825ced647 100644 --- a/docs/readme-facebook.md +++ b/docs/readme-facebook.md @@ -20,6 +20,7 @@ Table of Contents * [Simulate typing](#simulate-typing) * [Silent and No Notifications](#silent-and-no-notifications) * [Messenger code API](#messenger-code-api) +* [Attachment upload API](#attachment-upload-api) * [Running Botkit with an Express server](#use-botkit-for-facebook-messenger-with-an-express-web-server) ## Getting Started @@ -506,6 +507,38 @@ controller.api.messenger_profile.get_target_audience(function (err, data) { ``` +## Attachment upload API + +Attachment upload API allows you to upload an attachment that you may later send out to many users, without having to repeatedly upload the same data each time it is sent : + + +```js +var attachment = { + "type":"image", + "payload":{ + "url":"https://pbs.twimg.com/profile_images/803642201653858305/IAW1DBPw_400x400.png", + "is_reusable": true + } + }; + + controller.api.attachment_upload.upload(attachment, function (err, attachmentId) { + if(err) { + // Error + } else { + var image = { + "attachment":{ + "type":"image", + "payload": { + "attachment_id": attachmentId + } + } + }; + bot.reply(message, image); + } + }); + +``` + ## Use BotKit for Facebook Messenger with an Express web server Instead of the web server generated with setupWebserver(), it is possible to use a different web server to receive webhooks, as well as serving web pages. diff --git a/examples/facebook_bot.js b/examples/facebook_bot.js index 29e0ed41f..c5ee94b1e 100755 --- a/examples/facebook_bot.js +++ b/examples/facebook_bot.js @@ -132,6 +132,32 @@ controller.setupWebserver(process.env.port || 3000, function(err, webserver) { }); +controller.hears(['attachment_upload'], 'message_received', function(bot, message) { + var attachment = { + "type":"image", + "payload":{ + "url":"https://pbs.twimg.com/profile_images/803642201653858305/IAW1DBPw_400x400.png", + "is_reusable": true + } + }; + + controller.api.attachment_upload.upload(attachment, function (err, attachmentId) { + if(err) { + // Error + } else { + var image = { + "attachment":{ + "type":"image", + "payload": { + "attachment_id": attachmentId + } + } + }; + bot.reply(message, image); + } + }); +}); + controller.api.messenger_profile.greeting('Hello! I\'m a Botkit bot!'); controller.api.messenger_profile.get_started('sample_get_started_payload'); controller.api.messenger_profile.menu([{ diff --git a/lib/Facebook.js b/lib/Facebook.js index 78cf8f208..03c8b2217 100644 --- a/lib/Facebook.js +++ b/lib/Facebook.js @@ -610,9 +610,50 @@ function Facebookbot(configuration) { } }; + var attachment_upload_api = { + upload: function(attachment, cb) { + var message = { + message : { + attachment: attachment + } + }; + + request.post('https://' + api_host + '/v2.6/me/message_attachments?access_token=' + configuration.access_token, + { form: message }, + function(err, res, body) { + if (err) { + facebook_botkit.log('Could not upload attachment'); + cb(err); + } else { + + var results = null; + try { + results = JSON.parse(body); + } catch (err) { + facebook_botkit.log('ERROR in attachment upload API call: Could not parse JSON', err, body); + cb(err); + } + + if (results) { + if (results.error) { + facebook_botkit.log('ERROR in attachment upload API call: ', results.error.message); + cb(results.error); + } else { + var attachment_id = results.attachment_id; + facebook_botkit.log('Successfully got attachment id ', attachment_id); + cb(null, attachment_id); + } + } + } + }); + } + + }; + facebook_botkit.api = { 'messenger_profile': messenger_profile_api, - 'thread_settings': messenger_profile_api + 'thread_settings': messenger_profile_api, + 'attachment_upload': attachment_upload_api }; // Verifies the SHA1 signature of the raw request payload before bodyParser parses it diff --git a/npm-debug.log b/npm-debug.log new file mode 100644 index 000000000..deca7db99 --- /dev/null +++ b/npm-debug.log @@ -0,0 +1,24 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'start' ] +2 info using npm@3.10.8 +3 info using node@v6.9.1 +4 verbose stack Error: missing script: start +4 verbose stack at run (/usr/local/lib/node_modules/npm/lib/run-script.js:151:19) +4 verbose stack at /usr/local/lib/node_modules/npm/lib/run-script.js:61:5 +4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:356:5 +4 verbose stack at checkBinReferences_ (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:320:45) +4 verbose stack at final (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:354:3) +4 verbose stack at then (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:124:5) +4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:311:12 +4 verbose stack at /usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16 +4 verbose stack at tryToString (fs.js:455:3) +4 verbose stack at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:442:12) +5 verbose cwd /Users/SOAT-OLA/works/projects/botkit +6 error Darwin 16.5.0 +7 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "start" +8 error node v6.9.1 +9 error npm v3.10.8 +10 error missing script: start +11 error If you need help, you may report this error at: +11 error +12 verbose exit [ 1, true ]