Added post-to-facebook script

This commit is contained in:
Michael Housh
2023-10-06 22:46:53 -04:00
parent 2b4ecf3939
commit be24adf2aa
4 changed files with 107 additions and 1 deletions

1
.gitignore vendored
View File

@@ -9,3 +9,4 @@
**/completions/*
**/.netrwhist
**/.zshrc-local
facebook-bot/config/*

11
facebook-bot/README.md Normal file
View File

@@ -0,0 +1,11 @@
# Facebook Bot Instructions
This is used to programmaticly make posts to Facebook.
## References
- https://developers.facebook.com/docs/graph-api/reference/v18.0/group/feed
- https://developers.facebook.com/tools/explorer?method=GET&path=896542914538874&version=v18.0
An access token with `publish_to_groups` permissions is required to post to
groups.

View File

@@ -59,7 +59,7 @@ choice="${choice##*/}"
&& exit 1
# Set the prefix / website according to the options.
[ -n $localOpt ] \
[ -n "$localOpt" ] \
&& prefix="http://localhost:1313" \
|| prefix="https://mhoush.com"

View File

@@ -0,0 +1,94 @@
#!/usr/bin/env zsh
# Posts to facebook group and personal page.
#
# Typical usage would be to generate a link to the latest
# blog post and send to facebook:
#
# `$ mhlink --last | xargs -I {} post-to-facebook -m <optional-message> {}`
#
#
#################### Options ####################
declare -a message
zparseopts -D -F -K -- \
{a,-all}=locationOpt \
{m,-message}+:=message \
{o,-offline}=offline \
{p,-personal}=locationOpt \
{h,-help}=help
baseUrl="https://graph.facebook.com/v18.0"
link=$1
message="${message[-1]}"
token=$(cat < "$HOME/.config/facebook-bot/access-token.txt")
#################### Usage ####################
function usage() {
cat <<EOF
post-to-facebook: Create posts on facebook using. This accepts
a link as an argument and a message option. Either the message
or the link (or both) needs to be supplied.
Typical usage:
post-to-facebook --message "Some message" "https://mhouhs.com/<post>"
Usage: post-to-facebook [-a] [-m <message>] [-o] [-p] <link>
-a | --all: Send the post to the group and personal pages.
-m | --message: The optional message.
-o | --offline: Do not send the request(s), but print them.
-p | --personal: Send to personal page only.
-h | --help: Show the usage.
The -a or -p options are optional, if neither is supplied then it will
only be posted to the group.
EOF
}
#################### Helpers ####################
function post() {
local url=$1
if [ -n "$offline" ]; then
echo "Offline mode, request not being sent."
http --offline --ignore-stdin POST "$url" access_token=="$token" \
link="$link" \
message="$message"
else
http --ignore-stdin POST "$url" access_token=="$token" \
link="$link" \
message="$message"
fi
}
function post_to_group() {
group=$(cat < "$HOME/.config/facebook-bot/group.txt")
post "$baseUrl/$group/feed"
}
function post_to_personal() {
post "$baseUrl/me/feed"
}
#################### Main ####################
[ -n "$help" ] && usage && exit 0
[ -z "$link" ] && [ -z "$message" ] \
&& echo "Link or message is required." \
&& exit 1
[ "$locationOpt" = "-p" ] || [ "$locationOpt" = "--personal" ] \
&& post_to_personal \
&& exit 0
[ "$locationOpt" = "-a" ] || [ "$locationOpt" = "--all" ] \
&& post_to_personal
post_to_group