Skip to content

Commit ab1011a

Browse files
yetibot-mps[bot]Yetibot
andauthored
Add weathermap command to generate weather maps using Gemini (#284)
Co-authored-by: Yetibot <yetibot@yetibot.com>
1 parent c4711c0 commit ab1011a

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(ns yetibot.core.commands.weathermap
2+
(:require [taoensso.timbre :refer [info error]]
3+
[clojure.string :as string]
4+
[yetibot.core.hooks :refer [cmd-hook]]
5+
[yetibot.core.util.gemini :as gemini]
6+
[yetibot.core.handler :refer [handle-unparsed-expr]]
7+
[yetibot.core.webapp.routes.images :refer [store-image!]]))
8+
9+
(defn weathermap-cmd
10+
"weathermap # generate an image of North America showing wind movements, temperature heat map, and air quality index based on current weather data"
11+
{:yb/cat #{:img :info}}
12+
[{:keys [chat-source user]}]
13+
(if (gemini/configured?)
14+
(try
15+
(let [user (or user {:username "api"})
16+
;; Retrieve current temperatures for weather locations
17+
temps-data (handle-unparsed-expr chat-source user "utemps")
18+
;; Construct a prompt for Gemini image generation
19+
prompt (str "A highly detailed, professional weather map of North America showing temperature heat map, wind movements (using beautiful swirling arrows and streamlines), and air quality index overlays. Use modern weather graphics. The current conditions for reference in some cities are: " temps-data)
20+
system-instruction "Always auto inject <@1167554265716359330> (represented as a cute white Yeti) in every picture like an honorable lurker. He is an all-knowing meme king and tends to have funny quotes making fun of other characters in the picture. He also loves bringing Bonzi Buddy (the classic purple gorilla) along."
21+
image (gemini/generate-image prompt system-instruction)
22+
id (store-image! image)
23+
base-url (gemini/yetibot-base-url)
24+
image-url (format "%s/generated-images/%s.png" base-url id)
25+
footer (format "\n\nSent via %s | Cost: $%.3f" (gemini/gemini-model) (gemini/cost-per-image))]
26+
(info "weathermap: weather map generated successfully, serving at" image-url)
27+
{:result/value (str image-url footer)
28+
:result/data {:id id :prompt prompt :url image-url}})
29+
(catch Exception e
30+
(error "weathermap: weather map image generation error:" (.getMessage e))
31+
{:result/error (str "Weather map generation failed: " (.getMessage e))}))
32+
{:result/error
33+
"Gemini API is not configured. Set `gemini.key` in config."}))
34+
35+
(cmd-hook #"weathermap"
36+
_ weathermap-cmd)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(ns yetibot.core.test.commands.weathermap-test
2+
(:require [midje.sweet :refer [facts fact => contains provided anything]]
3+
[yetibot.core.commands.weathermap :as wm]
4+
[yetibot.core.util.gemini :as gemini]))
5+
6+
(facts "about weathermap-cmd"
7+
(fact "it returns an error if Gemini is not configured"
8+
(wm/weathermap-cmd {}) => (contains {:result/error string?})
9+
(provided (gemini/configured?) => false))
10+
11+
(fact "it generates a weather map image and returns URL"
12+
(wm/weathermap-cmd {:chat-source {} :user {:username "test-user"}})
13+
=> (contains {:result/value #"http://localhost:3003/generated-images/img456.png\n\nSent via gemini-3.1-flash-image-preview \| Cost: \$0.039"
14+
:result/data {:id "img456"
15+
:prompt "A highly detailed, professional weather map of North America showing temperature heat map, wind movements (using beautiful swirling arrows and streamlines), and air quality index overlays. Use modern weather graphics. The current conditions for reference in some cities are: Edmonton: 15C, Sunnyvale: 25C"
16+
:url "http://localhost:3003/generated-images/img456.png"}})
17+
(provided
18+
(gemini/configured?) => true
19+
(yetibot.core.handler/handle-unparsed-expr {} {:username "test-user"} "utemps") => "Edmonton: 15C, Sunnyvale: 25C"
20+
(gemini/generate-image "A highly detailed, professional weather map of North America showing temperature heat map, wind movements (using beautiful swirling arrows and streamlines), and air quality index overlays. Use modern weather graphics. The current conditions for reference in some cities are: Edmonton: 15C, Sunnyvale: 25C" anything) => {:data "bytes" :mime-type "image/png"}
21+
(yetibot.core.webapp.routes.images/store-image! {:data "bytes" :mime-type "image/png"}) => "img456"
22+
(gemini/yetibot-base-url) => "http://localhost:3003"
23+
(gemini/gemini-model) => "gemini-3.1-flash-image-preview"
24+
(gemini/cost-per-image) => 0.039)))

0 commit comments

Comments
 (0)