Development of this app has been halted in favor of Agoras
- Get a facebook permanent access token (explained below) using a facebook account that owns the page where you want to post messages.
- Find the ID of the page that you want to post messages in (explained below).
- Find the atom feed URL that contains the posts that you wish to share.
Configure your workflow to use LuisAlejandro/fb-last-post-from-feed@0.2.0
,
and provide the atom feed URL you want to use as the FEED_URL
env variable.
Provide the access token for your Facebook app as the
FACEBOOK_ACCESS_TOKEN
env variable, set your facebook page ID as
FACEBOOK_PAGE_ID
(as secrets). Remember, to add secrets go to your repository
Settings
> Secrets
> Actions
> New repository secret
for each secret.
For example, create a file .github/workflows/schedule.yml
on
a github repository with the following content:
name: Publish last post of feed hourly
on:
schedule:
- cron: '0 * * * *'
jobs:
fbpost:
runs-on: ubuntu-20.04
steps:
- uses: LuisAlejandro/fb-last-post-from-feed@0.2.0
env:
FACEBOOK_ACCESS_TOKEN: ${{ secrets.FACEBOOK_ACCESS_TOKEN }}
FACEBOOK_PAGE_ID: ${{ secrets.FACEBOOK_PAGE_ID }}
FEED_URL: ${{ secrets.FEED_URL }}
Publish your changes, activate your actions if disabled and enjoy.
- The action is designed to publish a maximum of 1 post per batch, regardless of the actual
number of new posts since the last run. You can alter this behavior by setting a
MAX_COUNT
env variable with your new value. - For this action to work properly, it should be run with an hourly cron (
0 * * * *
). The script is designed to look back and publish all posts (set byMAX_COUNT
) since the last hour. If you want to change the frecuency of execution, modify the cron expression and then set aPOST_LOOKBACK
env variable with the cron interval in seconds. For example, for a*/5 * * * *
cron (every 5 min), set envPOST_LOOKBACK: 300
.
Following the instructions laid out in Facebook's extending page tokens documentation I was able to get a page access token that does not expire.
I suggest using the Graph API Explorer for all of these steps except where otherwise stated.
If you already have an app, skip to step 1.
- Go to My Apps.
- Click "+ Add a New App".
- Setup a website app.
You don't need to change its permissions or anything. You just need an app that wont go away before you're done with your access token.
- Go to the Graph API Explorer.
- Select the application you want to get the access token for (in the "Facebook App" drop-down menu, not the "My Apps" menu).
- In the "Add a Permission" drop-down, search and check "pages_manage_posts" and "pages_read_engagement".
- Click "Generate Access Token".
- Grant access from a Facebook account that has access to manage the target page. Note that if this user loses access the final, never-expiring access token will likely stop working.
The token that appears in the "Access Token" field is your short-lived access token.
Following these instructions from the Facebook docs, make a GET request to
entering in your app's ID and secret and the short-lived token generated in the previous step.
You cannot use the Graph API Explorer. For some reason it gets stuck on this request. I think it's because the response isn't JSON, but a query string. Since it's a GET request, you can just go to the URL in your browser.
The response should look like this:
{"access_token":"ABC123","token_type":"bearer","expires_in":5183791}
"ABC123" will be your long-lived access token. You can put it into the Access Token Debugger to verify. Under "Expires" it should have something like "2 months".
Using the long-lived access token, make a GET request to
https://graph.facebook.com/me?access_token=**{long_lived_access_token}**
The id
field is your account ID. You'll need it for the next step.
Make a GET request to
https://graph.facebook.com/**{account_id}**/accounts?access_token=**{long_lived_access_token}**
The JSON response should have a data
field under which is an array of items the user has access to. Find the item for the page you want the permanent access token from. The access_token
field should have your permanent access token. Copy it and test it in the Access Token Debugger. Under "Expires" it should say "Never".
To find your Page ID:
- From News Feed, click Pages in the left side menu.
- Click your Page name to go to your Page.
- Click About in the left column. If you don't see About in the left column, click See More.
- Scroll down to find your Page ID below More Info.
-
You can test the script locally with Docker Compose:
-
Install Docker Community Edition according with your operating system
-
Install Docker Compose according with your operating system.
-
Install a git client.
-
Fork this repo.
-
Clone your fork of the repository into your local computer.
-
Open a terminal and navigate to the newly created folder.
-
Change to the
develop
branch.git checkout develop
-
Create a
.env
file with the content of the environment secrets as variables, like this (with real values):FACEBOOK_ACCESS_TOKEN=xxxx FACEBOOK_PAGE_ID=xxxx FEED_URL=xxxx
-
Execute the following command to create the docker image (first time only):
make image
-
You can execute the publish script with this command:
make publish
-
Or, alternatively, open a console where you can manually execute the script and debug any errors:
make console python3 entrypoint.py
-
You can stop the docker container with:
make stop
-
Or, destroy it completely:
make destroy
-
Web luisalejandro.org ยท GitHub @LuisAlejandro ยท Twitter @LuisAlejandro