@alpha-manager/fb
v1.0.6
Published
Simply require this module, configure it, and pass it to alpha's .to method!
Downloads
20
Readme
Alpha Manager [FB]
This is a Facebook API wrapper that integrates seamlessly with @alpha-manager/core.
Installation
$ npm i --s @alpha-manager/fb
Getting Started
const AlphaFB = require ('@alpha-manager/fb');
const fb = new AlphaFB ().config ({
id: 0, // the page id, must be a number
token: "" // the access token, must be a string
});
Using it with @alpha-manager/core
This section is targeted towards those who wish to post and comment on a schedule.
These examples will not include the aforementioned setup. Also, they use @alpha-manager/core to create the tasks, so you will first need to install it
$ npm i --s @alpha-manager/core
and import it into your script
const alpha = require ('@alpha-manager/core');
Posting plain text
const myTask = new alpha.Task ()
.to (fb) //the alphaFB instance we configured earlier
.do (actionObject => {
actionObject.type = "post";
actionObject.message = "hello! this is a text post!";
actionObject.done ();
})
.every (5).minute ()
.start ();
The above piece of code will post "hello! this is a text post!"
on the Facebook page we configured earlier every 5 minutes.
Posting media
Images
const myTask = new alpha.Task ()
.to (fb) //the alphaFB instance we configured earlier
.do (actionObject => {
actionObject.type = "post";
actionObject.message = "hello! this is the caption of an image post!";
actionObject.media = "path/to/image.png";
actionObject.done ();
})
.every (5).minute ()
.start ();
or
const myTask = new alpha.Task ()
.to (fb) //the alphaFB instance we configured earlier
.do (actionObject => {
actionObject.type = "post";
actionObject.message = "hello! this is the caption of an image post!";
actionObject.media = {
type: 'image',
src: 'path/to/image.png'
};
actionObject.done ();
})
.every (5).minute ()
.start ();
Video
const myTask = new alpha.Task ()
.to (fb) //the alphaFB instance we configured earlier
.do (actionObject => {
actionObject.type = "post";
actionObject.message = "hello! this is the description of a video post!";
actionObject.media = {
type: 'video',
src: 'path/to/video.mp4'
};
actionObject.done ();
})
.every (5).minute ()
.start ();
Commenting on a post as soon as it posted
const myTask = new alpha.Task ()
.to (fb) //the alphaFB instance we configured earlier
.do (actionObject => {
actionObject.type = "post";
actionObject.message = "hello! this is a text post!";
actionObject.comment = {
message: 'this is a comment',
media: 'path/to/image.png' // optional, attaches image to the comment
}
actionObject.done ();
})
.every (5).minute ()
.start ();
Commenting on a schedule
Just like we are able to post on a schedule using Tasks, we can also comment on a schedule.
const myCommentTask = new alpha.Task ()
.to (fb)
.do (actionObject => {
actionObject.type = "comment";
actionObject.on = "id_of_object"; // the object on which to comment
actionObject.message = "the comment message";
actionObject.media = "path/to/image.png" // optional
})
.every (5). minute ()
.start ();
Async Tasks
We can also do async operations inside tasks when generating the post object
const myTask = new alpha.Task ()
.to (fb) //the alphaFB instance we configured earlier
.do (async actionObject => {
actionObject.type = "post";
actionObject.message = await doAsyncStuff ();
actionObject.done ();
})
.every (5).minute ()
.start ();
or
const myTask = new alpha.Task ()
.to (fb) //the alphaFB instance we configured earlier
.do (actionObject => {
actionObject.type = "post";
doAsyncStuff ().then (msg => {
actionObject.message = msg;
actionObject.done ();
});
})
.every (5).minute ()
.start ();
Here doAsyncStuff ()
is a hypothetical async function that gets resolved at some unknown time in the future.
Other features
Posting
Text
fb.post ({
type: 'text',
message: 'this is a text post!'
})
.then (response => console.log (response))
.catch (error => console.error (error));
When posting an image or a video, the 'media' property of the object can either be a path or a ReadStream
Images
fb.post ({
type: 'image',
message: 'this is a caption for the image', // optional
media: 'path/to/image.png'
})
.then (response => console.log (response))
.catch (error => console.error (error));
Video
fb.post ({
type: 'video',
title: 'video title', // optional
description: 'video description', // optional
media: 'path/to/video.mp4'
})
.then (response => console.log (response))
.catch (error => console.error (error));
Commenting
A comment can also contain a media property that is used to comment pictures. The 'media' property can be either a path or a ReadStream.
The object_id is the ID of the object upon which the comment will be made.
fb.comment ({
on: 'object_id',
message: 'this is a comment!',
media: 'path/to/image.png' // optional
})
.then (response => console.log (response))
.catch (error => console.error (error));
Getters
@alpha-manager/fb
can also be used to get posts, comments, and reactions.
All getters accept an options
object.
Currently the only parameter the options
object supports is fields
which specifies which fields should be retrieved from the Facebook API.
The fields
parameter can either be a string (with the fields separated by ","), or an array of strings. For example:
fb.get.posts.all ({fields: ['id', 'message', 'type']})
.then (res => {
//every post in `res` will have their id, message and type [link, status, photo, video, offer]
})
.catch (err => console.log (err));
or
fb.get.posts.all ({fields: 'id, message, created_time'})
.then (res => {
//every post in `res` will have their id, message and creation time
})
.catch (err => console.log (err));
Posts
all
all(options: object)
fb.get.posts.all ()
.then (allPosts => console.log (allPosts))
.catch (error => console.error (error));
latest
latest()
(without any parameter) returns the latest post made on the specified page.
fb.get.posts.latest ()
.then (latestPost => console.log (latestPost))
.catch (error => console.error (error));
``latest(n: number, options: object)` returns the latest n posts made on the specified page.
fb.get.posts.latest (5)
.then (latestFivePosts => console.log (latestFivePosts))
.catch (error => console.error (error));
range
range(start_date: Date, end_date: Date, options: object)
returns all posts published between the two dates
let now = new Date ();
let aWeekAgo = new Date (Date.now () - (7 * 24 * 60 * 60 * 1000));
fb.get.posts.range (aWeekAgo, now)
.then (posts => {
//all posts published in the last 7 days
console.log(posts);
})
.catch (err => console.log(err));
Comments
comments(object_id: string, options: object)
The object_id is the ID of the object that we are retrieving the comments of.
fb.get.comments(object_id)
.then (comments => console.log(comments))
.catch (err => console.log(err));
Reactions
reactions(object_id: string, options: object)
The object_id is the ID of the object that we are retrieving the reactions of.
fb.get.reactions(object_id)
.then(reactions => console.log(reactions))
.catch(err => console.log(err));