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

Commit a68642e

Browse files
Resolve conflict with botkit v0.0.6
1 parent afd2136 commit a68642e

File tree

2 files changed

+98
-104
lines changed

2 files changed

+98
-104
lines changed

docs/readme-facebook.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Table of Contents
2121
* [Silent and No Notifications](#silent-and-no-notifications)
2222
* [Messenger code API](#messenger-code-api)
2323
* [Attachment upload API](#attachment-upload-api)
24+
* [Handover Protocol](#handover-protocol)
2425
* [Running Botkit with an Express server](#use-botkit-for-facebook-messenger-with-an-express-web-server)
2526

2627
## Getting Started
@@ -86,9 +87,9 @@ Normal messages will be sent to your bot using the `message_received` event. In
8687
| facebook_optin | a user has clicked the [Send-to-Messenger plugin](https://developers.facebook.com/docs/messenger-platform/implementation#send_to_messenger_plugin)
8788
| facebook_referral | a user has clicked on a [m.me URL with a referral param](https://developers.facebook.com/docs/messenger-platform/referral-params)
8889
| facebook_app_roles | This callback will occur when a page admin changes the role of your application.
89-
| facebook_standby | This callback will occur when a message has been sent to your page, but your application is not the current thread owner.
90-
| facebook_pass_thread_control | This callback will occur when thread ownership for a user has been passed to your application.
91-
| facebook_take_thread_control | This callback will occur when thread ownership for a user has been taken away from your application.
90+
| standby | This callback will occur when a message has been sent to your page, but your application is not the current thread owner.
91+
| facebook_receive_thread_control | This callback will occur when thread ownership for a user has been passed to your application.
92+
| facebook_lose_thread_control | This callback will occur when thread ownership for a user has been taken away from your application.
9293

9394
All incoming events will contain the fields `user` and `channel`, both of which represent the Facebook user's ID, and a `timestamp` field.
9495

@@ -597,7 +598,7 @@ Allows the Primary Receiver app to take control of a specific thread from a Seco
597598

598599
- To thread control :
599600
```javascript
600-
controller.api.handover.take_thread_control('PSID', 'String to pass to the secondary receiver', function (result) {
601+
controller.api.handover.take_thread_control('<RECIPIENT_PSID>', 'String to pass to pass to the secondary receiver', function (result) {
601602
// result = {"success":true}
602603
});
603604
```
@@ -608,7 +609,7 @@ Allows you to pass thread control from your app to another app.
608609

609610
- To pass thread control :
610611
```javascript
611-
controller.api.handover.pass_thread_control('PSID', '123456789', 'String to pass to the secondary receiver app', function (result) {
612+
controller.api.handover.pass_thread_control('<RECIPIENT_PSID>', '<TARGET_PSID>', 'String to pass to the secondary receiver app', function (result) {
612613
// result = {"success":true}
613614
});
614615
```

lib/Facebook.js

+92-99
Original file line numberDiff line numberDiff line change
@@ -362,20 +362,13 @@ function Facebookbot(configuration) {
362362
for (var m = 0; m < payload.entry[e].messaging.length; m++) {
363363
facebook_botkit.ingest(bot, payload.entry[e].messaging[m], res);
364364
}
365-
}
365+
}
366366
if (payload.entry[e].standby) {
367367
for (var s = 0; s < payload.entry[e].standby.length; s++) {
368-
const message_event = payload.entry[e].standby[s]
369-
370-
const standby_message = {
371-
sender: message_event.sender,
372-
recipient: message_event.recipient,
373-
timestamp: message_event.timestamp,
374-
standby: message_event
375-
}
368+
var standby_message = payload.entry[e].standby[s];
369+
standby_message.standby = true;
376370
facebook_botkit.ingest(bot, standby_message, res);
377371
}
378-
379372
}
380373
}
381374
}
@@ -471,6 +464,8 @@ function Facebookbot(configuration) {
471464
next();
472465

473466
});
467+
468+
/* Facebook Handover Protocol categorize middleware */
474469
facebook_botkit.middleware.categorize.use(function threadControl(bot, message, next) {
475470

476471
if (message.app_roles) {
@@ -485,10 +480,10 @@ function Facebookbot(configuration) {
485480
if (message.take_thread_control) {
486481
message.type = 'facebook_lose_thread_control'
487482
}
488-
console.log('Thread Control Step', {message})
489483

490-
next()
491-
})
484+
next();
485+
486+
});
492487

493488
facebook_botkit.on('webserver_up', function(webserver) {
494489

@@ -795,87 +790,6 @@ function Facebookbot(configuration) {
795790
}
796791
};
797792

798-
var handover = {
799-
get_secondary_receivers_list: function(cb) {
800-
request.get({
801-
url: 'https://' + api_host + '/v2.6/me/secondary_receivers',
802-
qs: {
803-
fields: 'id,name',
804-
access_token: configuration.access_token
805-
},
806-
json: true
807-
},
808-
function(err, res, body) {
809-
if (err) {
810-
facebook_botkit.log('Could not get secondary receivers list');
811-
cb && cb(err)
812-
} else {
813-
if (body) {
814-
if (body.error) {
815-
facebook_botkit.log('ERROR in secondary receivers list: ', results.error.message);
816-
cb && cb(body.error)
817-
} else {
818-
facebook_botkit.debug('Successfully call page message tags', body);
819-
cb && cb(null, body);
820-
}
821-
}
822-
}
823-
});
824-
},
825-
take_thread_control: function(recipient, metadata, cb) {
826-
var request_body = {
827-
recipient: {
828-
id: recipient
829-
},
830-
metadata: metadata
831-
};
832-
request.post({
833-
url: 'https://' + api_host + '/v2.6/me/take_thread_control',
834-
qs: {access_token: configuration.access_token},
835-
body: request_body,
836-
json: true
837-
},
838-
function(err, res, body) {
839-
if (err) {
840-
facebook_botkit.log('Could not take thread control');
841-
} else {
842-
if (body) {
843-
if (body.error) {
844-
facebook_botkit.log('ERROR in take thread control API call: ', body.error.message);
845-
cb && cb(body.error)
846-
} else {
847-
facebook_botkit.debug('Successfully taken thread control', body);
848-
cb && cb(null, body);
849-
}
850-
}
851-
}
852-
});
853-
},
854-
pass_thread_control: function(recipient, target, metadata, cb) {
855-
var request_body = {
856-
recipient: {
857-
id: recipient
858-
},
859-
target_app_id: target,
860-
metadata: metadata
861-
};
862-
request.post({
863-
url: 'https://' + api_host + '/v2.6/me/pass_thread_control',
864-
qs: {access_token: configuration.access_token},
865-
body: request_body,
866-
json: true,
867-
},
868-
function(err, res, body) {
869-
if (err) {
870-
facebook_botkit.log('Could not pass thread control');
871-
} else {
872-
if (body) {
873-
if (body.error) {
874-
facebook_botkit.log('ERROR in pass thread control API call: ', body.error.message);
875-
cb && cb(body)
876-
} else {
877-
facebook_botkit.debug('Successfully pass thread control', body);
878-
cb && cb(null, body);
879793
var user_profile = function(uid, fields, cb) {
880794
if (!fields) {
881795
fields = 'first_name,last_name,timezone,gender,locale';
@@ -929,8 +843,90 @@ function Facebookbot(configuration) {
929843
};
930844

931845

932-
933-
846+
var handover = {
847+
get_secondary_receivers_list: function (fields, cb) {
848+
request.get({
849+
url: 'https://' + api_host + '/v2.6/me/secondary_receivers',
850+
qs: {
851+
fields: typeof(fields) == 'string' ? fields : fields.join(','),
852+
access_token: configuration.access_token
853+
},
854+
json: true
855+
}, function (err, res, body) {
856+
if (err) {
857+
facebook_botkit.log('Could not get secondary receivers list');
858+
cb && cb(err);
859+
} else {
860+
if (body.error) {
861+
facebook_botkit.log('ERROR in secondary receivers list: ', body.error.message);
862+
cb && cb(body.error);
863+
} else {
864+
facebook_botkit.debug('Successfully getting secondary receivers list', body);
865+
cb && cb(null, body);
866+
}
867+
}
868+
});
869+
},
870+
take_thread_control: function (recipient, metadata, cb) {
871+
var request_body = {
872+
recipient: {
873+
id: recipient
874+
},
875+
metadata: metadata
876+
};
877+
request.post({
878+
url: 'https://' + api_host + '/v2.6/me/take_thread_control',
879+
qs: {
880+
access_token: configuration.access_token
881+
},
882+
body: request_body,
883+
json: true
884+
}, function (err, res, body) {
885+
if (err) {
886+
facebook_botkit.log('Could not take thread control');
887+
cb && cb(err);
888+
} else {
889+
if (body.error) {
890+
facebook_botkit.log('ERROR in take thread control API call: ', body.error.message);
891+
cb && cb(body.error);
892+
} else {
893+
facebook_botkit.debug('Successfully taken thread control', body);
894+
cb && cb(null, body);
895+
}
896+
}
897+
});
898+
},
899+
pass_thread_control: function (recipient, target, metadata, cb) {
900+
var request_body = {
901+
recipient: {
902+
id: recipient
903+
},
904+
target_app_id: target,
905+
metadata: metadata
906+
};
907+
request.post({
908+
url: 'https://' + api_host + '/v2.6/me/pass_thread_control',
909+
qs: {
910+
access_token: configuration.access_token
911+
},
912+
body: request_body,
913+
json: true
914+
}, function (err, res, body) {
915+
if (err) {
916+
facebook_botkit.log('Could not pass thread control');
917+
cb && cb(err);
918+
} else {
919+
if (body.error) {
920+
facebook_botkit.log('ERROR in pass thread control API call: ', body.error.message);
921+
cb && cb(body.error);
922+
} else {
923+
facebook_botkit.debug('Successfully past thread control', body);
924+
cb && cb(null, body);
925+
}
926+
}
927+
});
928+
}
929+
};
934930

935931

936932
facebook_botkit.api = {
@@ -944,9 +940,6 @@ function Facebookbot(configuration) {
944940
};
945941

946942

947-
948-
949-
950943
// Verifies the SHA1 signature of the raw request payload before bodyParser parses it
951944
// Will abort parsing if signature is invalid, and pass a generic error to response
952945
function verifyRequest(req, res, buf, encoding) {

0 commit comments

Comments
 (0)