Git Product home page Git Product logo

luisalejandro / fb-random-post-from-feed Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 6.0 104 KB

[ARCHIVED] GitHub Action for posting a random entry from an atom feed to a facebook page

License: GNU General Public License v3.0

Dockerfile 7.09% Makefile 20.06% Python 63.77% Shell 1.71% Smarty 7.37%
facebook facebook-api facebook-page publishing social-network rss-feed atom-feed github-actions

fb-random-post-from-feed's Introduction

Development of this app has been halted in favor of Agoras

Current version: 0.2.0

๐ŸŽ’ Prep Work

  1. Get a facebook permanent access token (explained below) using a facebook account that owns the page where you want to post messages.
  2. Find the ID of the page that you want to post messages in (explained below).
  3. Find the atom feed URL that contains the posts that you wish to share.

๐Ÿ–ฅ Workflow Usage

Configure your workflow to use LuisAlejandro/[email protected], 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 random post of feed hourly
on:
  schedule:
    - cron: '0 * * * *'
jobs:
  fbpost:
    runs-on: ubuntu-20.04
    steps:
      - uses: LuisAlejandro/[email protected]
        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.

โ— Important notes

  • The action is designed to pick random posts published within the last year. You can modify this interval by setting a MAX_POST_AGE env variable with an integer value in days. For example, to pick posts from the last month set MAX_POST_AGE: 30.

๐Ÿ‘ฅ How to get a Facebook permanent access token

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.

0. Create Facebook App

If you already have an app, skip to step 1.

  1. Go to My Apps.
  2. Click "+ Add a New App".
  3. 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.

1. Get User Short-Lived Access Token

  1. Go to the Graph API Explorer.
  2. Select the application you want to get the access token for (in the "Facebook App" drop-down menu, not the "My Apps" menu).
  3. In the "Add a Permission" drop-down, search and check "pages_manage_posts" and "pages_read_engagement".
  4. Click "Generate Access Token".
  5. 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.

2. Generate Long-Lived Access Token

Following these instructions from the Facebook docs, make a GET request to

https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=**{app_id}**&client_secret=**{app_secret}**&fb_exchange_token=**{short_lived_token}**

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".

3. Get User ID

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.

4. Get Permanent Page Access Token

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".

๐Ÿ‘ฅ How to get a Facebook page ID

To find your Page ID:

  1. From News Feed, click Pages in the left side menu.
  2. Click your Page name to go to your Page.
  3. Click About in the left column. If you don't see About in the left column, click See More.
  4. Scroll down to find your Page ID below More Info.

๐Ÿ•ต๐Ÿพ Hacking suggestions

  • 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
      

Made with โค๏ธ and ๐Ÿ”

Banner

Web luisalejandro.org ยท GitHub @LuisAlejandro ยท Twitter @LuisAlejandro

fb-random-post-from-feed's People

Contributors

dependabot[bot] avatar luisalejandro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.