bottender-facebook
Version:
Facebook connector for Bottender.
71 lines (54 loc) • 2.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _axiosError = _interopRequireDefault(require("axios-error"));
var _messagingApiMessenger = require("messaging-api-messenger");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function handleError(err) {
const {
error
} = err.response.data;
const msg = `Messenger API - ${error.code} ${error.type} ${error.message}`;
throw new _axiosError.default(msg, err);
}
class FacebookClient extends _messagingApiMessenger.MessengerClient {
constructor(...args) {
super(...args);
_defineProperty(this, "sendPrivateReply", (objectId, text, options = {}) => this.sendText({
comment_id: objectId
}, text, options));
_defineProperty(this, "sendComment", (objectId, comment, {
access_token: customAccessToken
} = {}) => {
let body;
if (typeof comment === 'string') {
body = {
message: comment
};
} else {
body = comment;
}
return this._axios.post(`/${objectId}/comments?access_token=${customAccessToken || this._accessToken}`, body).then(res => res.data, handleError);
});
_defineProperty(this, "sendLike", (objectId, {
access_token: customAccessToken
} = {}) => this._axios.post(`/${objectId}/likes?access_token=${customAccessToken || this._accessToken}`).then(res => res.data, handleError));
_defineProperty(this, "getComment", (commentId, {
fields,
access_token: customAccessToken
} = {}) => {
const conjunctFields = Array.isArray(fields) ? fields.join(',') : fields;
const fieldsQuery = conjunctFields ? `fields=${conjunctFields}&` : '';
return this._axios.get(`/${commentId}?${fieldsQuery}access_token=${customAccessToken || this._accessToken}`).then(res => res.data, handleError);
});
_defineProperty(this, "getLikes", (objectId, {
summary,
access_token: customAccessToken
} = {}) => this._axios.get(`/${objectId}/likes?${summary ? 'summary=true&' : ''}access_token=${customAccessToken || this._accessToken}`).then(res => res.data, handleError));
}
}
exports.default = FacebookClient;
_defineProperty(FacebookClient, "connect", (accessTokenOrConfig, version = '4.0') => new FacebookClient(accessTokenOrConfig, version));