In this article, you will learn how to create a Facebook Bot to post automatically on groups using Facebook Graph API and Python Programming Language. By using this Facebook bot you can post automatically on every Facebook group which you have joined.
Features of Facebook Bot
- Get all of the Facebook group ids which you have joined
- Read post link with a hash tag and description from excel file
- Post every link (added in excel file) on all Facebook groups (post after 12 to 16 minutes)
- Get the random link from excel file and post on Facebook groups randomly (post after 12 to 16 minutes)
How to Create Facebook Bot to Post Automatically on Groups
Below are the steps to create a Facebook bot using python and Facebook graph API.
- Create and generate access tokens by using Facebook Graph API
- Make a JSON file (named "credentials.json") and use the above token to in that file by creating a python dictionary (key-value pair). The key will be "access_token" and the value should be the generated token.
- Create an excel file named "link_description.xlsx", the file contains three columns and the name of the columns must be like below.
- link
- tag
- description
- Each row of your excel file contains the post link, tag, and description. You can add as many rows as you want.
Create and generate access tokens by using Facebook Graph API
Make a JSON File "credentials.json"
{"access_token": "your access token goes here"}
Create an Excel File "link_description.xlsx"
Below is the code snippet of the Facebook bot to post automatically in Facebook groups after 12 to 16 minutes.
Post Random Link of Excel File in Random Facebook Groups
This file contains 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'] | |
print('----------',post['groups']['data']) | |
# post in groups after below seconds | |
post_after = [600, 720, 840, 900] | |
groups = [] | |
for mg in my_groups: | |
if 'privacy' in mg: | |
if mg['privacy'] != 'CLOSED': | |
groups.append(mg['id']) | |
else: | |
groups.append(mg['id']) | |
# open the file | |
df = pd.read_excel("links_description.xlsx", sheet_name=0) | |
for lp in range(df.shape[0]): | |
post = random.randint(0,df.shape[0]-1) | |
message = df.tag[post] + '\n' + df.description[post] + '\n' | |
link = df.link[post] | |
wait_sec = secrets.choice(post_after) | |
random_group = secrets.choice(groups) | |
print('******Auto Posting Start After %s Seconds*******' % (wait_sec)) | |
time.sleep(wait_sec) | |
print('1111111',random_group,df.link[post]) | |
graph.put_object(random_group, 'feed', message=message, link=link) | |
print('******Link Posted*******',post) |
Post Every Link of Excel File in All Facebook Groups
This file contains 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*******') |
2 Comments
GraphAPIError: (#200) If posting to a group, requires app being installed in the group, and \
ReplyDeleteeither publish_to_groups permission with user token, or both pages_read_engagement \
and pages_manage_posts permission with page token; If posting to a page, \
requires both pages_read_engagement and pages_manage_posts as an admin with \
sufficient administrative permission
It doesn't work, that facebook app needs to be installed on that particular group to allow API posting on it
ReplyDelete