Created
November 22, 2021 10:42
-
-
Save sehrishnaz/20106403ef7f9aa1ff741e19892b5020 to your computer and use it in GitHub Desktop.
Automate Facebook Post to Groups using Graph API and Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from facebook import GraphAPI | |
import pandas as pd | |
import time | |
import secrets | |
import random | |
def read_creds(filename): | |
''' | |
Store API credentials in a safe place. | |
If you use Git, make sure to add the file to .gitignore | |
''' | |
with open(filename) as f: | |
credentials = json.load(f) | |
return credentials | |
credentials = read_creds('credentials.json') | |
graph = GraphAPI(access_token=credentials['access_token']) | |
message = ''' | |
Add your message here. | |
''' | |
# get groups whihc you have joined | |
post = graph.get_object(id='me',fields='groups') | |
my_groups = post['groups']['data'] | |
groups = [] | |
for mg in my_groups: | |
if 'privacy' in mg: | |
if mg['privacy'] != 'CLOSED': | |
groups.append(mg['id']) | |
else: | |
groups.append(mg['id']) | |
# post in groups after below seconds | |
post_after = [600, 720, 840, 900] | |
# open the file | |
df = pd.read_excel("links_description.xlsx", sheet_name=0) | |
for idx, group in enumerate(groups): | |
if idx < df.shape[0]: | |
message = df.tag[idx] + '\n' + df.description[idx] + '\n' | |
link = df.link[idx] | |
wait_sec = secrets.choice(post_after) | |
print('******Auto Posting Start After %s Seconds*******' % (wait_sec)) | |
time.sleep(wait_sec) | |
graph.put_object(group, 'feed', message=message, link=link) | |
print('******Link Posted*******') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Automate Facebook Post to Groups using Graph API and Python