Created
July 11, 2024 14:29
-
-
Save russmatney/c46a7c6205b54b42ddffcbcce8f22c8b to your computer and use it in GitHub Desktop.
Daily Moon Phase Discord Post via Babashka and Github Actions
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
{:paths ["bb"] | |
:tasks | |
{:requires ([moon-phases]) | |
post-moon-phase | |
(moon-phases/fetch-and-post-moon-phase | |
{:rapid-api/key (System/getenv "RAPIDAPI_KEY") | |
:discord/webhook-url (or (first *command-line-args*) | |
(System/getenv "DISCORD_WEBHOOK_URL"))})}} |
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
# .github/workflows/daily_moon_phase.yml | |
name: Post the daily moon phase to discord | |
on: | |
workflow_dispatch: {} | |
schedule: | |
# 15 UTC = 11 EST | |
- cron: '0 15 * * *' | |
jobs: | |
post_moon_phase: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Setup Babashka | |
uses: turtlequeue/[email protected] | |
with: | |
babashka-version: 1.3.191 | |
- name: Run a task | |
run: bb post-moon-phase | |
env: | |
RAPIDAPI_KEY: ${{ secrets.RAPIDAPI_KEY }} | |
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} |
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
;; bb/moon_phases.clj | |
(ns moon-phases | |
(:require | |
[babashka.http-client :as http] | |
[cheshire.core :as json])) | |
(def rapid-api-url "https://moon-phase.p.rapidapi.com/advanced") | |
(defn get-moon-data [opts] | |
(let [rapid-api-key (:rapid-api/key opts)] | |
(if-not rapid-api-key | |
(println "No api key passed, aborting") | |
(do | |
(println "Fetching moon data!") | |
(-> | |
(http/get rapid-api-url | |
{:headers {:x-rapidapi-key rapid-api-key} | |
:query-params {:lat 40 :lon -74}}) | |
:body | |
(json/parse-string true)))))) | |
(defn moon-data->discord-content [data] | |
(let [{:keys [emoji phase_name]} (:moon data)] | |
(str "Good morning Team Moonstorm!\n\nThe current moon phase is " phase_name ". " emoji))) | |
(defn post-to-channel [content opts] | |
(let [channel-url (:discord/webhook-url opts)] | |
(when-not content | |
(println "No content to post, aborting")) | |
(when-not channel-url | |
(println "No discord channel url, aborting")) | |
(when (and content channel-url) | |
(println "Posting to discord:" content) | |
(-> | |
(http/post channel-url | |
{:headers {:content-type "application/json"} | |
:body (json/encode {:content content})}) | |
((fn [result] | |
(if (#{204} (:status result)) | |
(println "Success!" result) | |
(println "Error!" result)))))))) | |
;; public | |
(defn fetch-and-post-moon-phase [opts] | |
(if-not (and (:discord/webhook-url opts) | |
(:rapid-api/key opts)) | |
(println "bad request, need discord/webhook-url and rapid-api/key") | |
(-> (get-moon-data opts) | |
moon-data->discord-content | |
(post-to-channel opts)))) | |
(comment | |
(def test-opts | |
{:rapid-api/key "" ;; rapidapi key | |
:discord/webhook-url "" ;; discord channel webhook | |
}) | |
(fetch-and-post-moon-phase test-opts) | |
(post-to-channel "hello!!" test-opts)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment