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

Add Facebook attachment upload api #899

Merged
merged 8 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/readme-facebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions examples/facebook_bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([{
Expand Down
43 changes: 42 additions & 1 deletion lib/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions npm-debug.log
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/npm/npm/issues>
12 verbose exit [ 1, true ]