This program automates posting images, documents, and texts across Facebook, Instagram, and WhatsApp.
- Clone the repository.
- Install dependencies:
pip install -r requirements.txt
- Update
config.py
with your credentials. - Run
main.py
to start posting.
This class provides methods to interact with the Facebook Graph API. You can use it to post text, images, and videos to Facebook pages or personal accounts, and to edit existing posts.
requests
library: Make sure to install it usingpip install requests
.- A valid Facebook access token with the required permissions.
__init__(self, access_token: str)
Parameters:
access_token
(str): Your Facebook Graph API access token.
Description:
Initializes the Facebook
class and sets up the API client. Fetches the user's details and a list of pages that the user has access to.
_requester(self, url: str, method: str = 'get', params: dict = {}, files: dict = {}, new_access_token: str = '')
Parameters:
url
(str): The URL for the API request.method
(str): The HTTP method to use (get
,post
,delete
). Default isget
.params
(dict): Parameters to include in the request.files
(dict): Files to upload (e.g., images or videos).new_access_token
(str): A new access token to use for this request. If not provided, the class's default token is used.
Returns:
dict
: The response from the API or an error message.
Description:
Handles API requests with error handling.
post_text(self, page_id: str, message: str)
Parameters:
page_id
(str): The page ID or 'me' for personal account.message
(str): The message content to post.
Returns:
dict
: The API response, including the result of the post operation.
Description:
Posts text to a specified page or personal account. If page_id
is 'me', it posts to the authenticated user's account.
edit_post(self, post_id: str, message: str)
Parameters:
post_id
(str): The ID of the post to edit.message
(str): The new message content.
Returns:
dict
: The API response, including the result of the edit operation.
Description:
Edits an existing post on a page or personal account.
get_pages_list(self) -> list
Returns:
list
: A list of pages that the user has access to.
Description:
Fetches the list of pages that the authenticated user has access to.
# Instantiate the Facebook class with your access token
fba = Facebook('YOUR_ACCESS_TOKEN')
# Post text to a page or personal account
response = fba.post_text('PAGE_NAME', 'Hello, world!\nThis is an automated post generated by the python script.')
print(response)
# Edit a post
post_id = 'YOUR_POST_ID'
new_message = 'This is the updated content of the post.'
edit_response = fba.edit_post(post_id, new_message)
print(edit_response)
- Ensure that your access token has the
pages_manage_posts
permission to edit posts on pages. - Verify that the
post_id
is correct and that the post belongs to the authenticated user or the page specified.
- 403 Forbidden Error: This may occur if the access token does not have the required permissions or if the post ID is incorrect.
- 401 Unauthorized Error: Check if your access token is valid and has not expired.
Feel free to submit pull requests or open issues.