diff --git a/.gitignore b/.gitignore index 579406b..044cdd6 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ **/completions/* **/.netrwhist **/.zshrc-local +facebook-bot/config/* diff --git a/facebook-bot/README.md b/facebook-bot/README.md new file mode 100644 index 0000000..74f22a3 --- /dev/null +++ b/facebook-bot/README.md @@ -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. diff --git a/scripts/scripts/mhlink b/scripts/scripts/mhlink index 657313f..fbd9adc 100755 --- a/scripts/scripts/mhlink +++ b/scripts/scripts/mhlink @@ -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" diff --git a/scripts/scripts/post-to-facebook b/scripts/scripts/post-to-facebook new file mode 100755 index 0000000..f103379 --- /dev/null +++ b/scripts/scripts/post-to-facebook @@ -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 {}` +# +# + +#################### 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 <" + +Usage: post-to-facebook [-a] [-m ] [-o] [-p] + + -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 +