Skip to content

Instantly share code, notes, and snippets.

@orhun
Last active March 21, 2023 12:29
Show Gist options
  • Save orhun/16febf27e7fdc8e5e22b350009d826c8 to your computer and use it in GitHub Desktop.
Save orhun/16febf27e7fdc8e5e22b350009d826c8 to your computer and use it in GitHub Desktop.
Telegram bot for GitHub follower notifications
#!/usr/bin/env bash
set -e
file="followers.txt"
followers=()
page=1
per_page=100
page_result=()
# github_token=
# telegram_bot_token=
# telegram_chat_id=
notify() {
curl -s -X POST \
-H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" \
--data "chat_id=${telegram_chat_id}" \
--data-urlencode "text=${1}" \
--data "parse_mode=markdown" \
"https://api.telegram.org/bot${telegram_bot_token}/sendMessage"
}
while true; do
readarray -t page_result <<<$(curl -s \
-X GET \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${github_token}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/user/followers?page=${page}&per_page=${per_page}" | jq -r '.[].login')
if [ ${#page_result[@]} -eq 1 ]; then
break
fi
followers+=("${page_result[@]}")
page=$((page + 1))
done
printf "%s\n" "${followers[@]}" >"${file}"
diff_result=$(diff -u "old_${file}" "${file}" | tail -n +4 | awk '$1 ~ /^+|^-/' | sed -E "s/^(\+|-)/\1 https:\/\/github.com\//g")
if [ -n "${diff_result}" ]; then
notify "${diff_result}"
notify "$(wc -l <"old_${file}") -> $(wc -l <"${file}")"
fi
printf "%s\n" "${followers[@]}" >"old_${file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment