diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-05-21 18:56:19 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-05-21 18:56:19 +0200 |
| commit | 688779fbc61d48046199d1f45549720e289ce417 (patch) | |
| tree | 4ea8c13d96428176f97c0c27f665391c09a93098 /cmd/fx | |
| parent | fb0fd67d6e7e55548c7a0a1ec67f608efffe3a94 (diff) | |
| download | xmppcb-688779fbc61d48046199d1f45549720e289ce417.tar.gz xmppcb-688779fbc61d48046199d1f45549720e289ce417.zip | |
Added support for user generated shell scripts:
- create a director with shell scripts
- call them within the chat with @<script-name>
- four sample scripts
Diffstat (limited to 'cmd/fx')
| -rwxr-xr-x | cmd/fx | 44 |
1 files changed, 44 insertions, 0 deletions
@@ -0,0 +1,44 @@ +#!/bin/sh +# @fx - exchange rates between USD, EUR, PLN and THB (no arguments) +# +# One keyless request to open.er-api.com (base USD); the six cross-rates +# are derived from it. JSON is parsed with awk so no 'jq' is needed. + +json=$(curl -fsS --max-time 8 "https://open.er-api.com/v6/latest/USD" \ + | tr -d '\n\r') || { echo "fx: rate lookup failed"; exit 1; } + +case "$json" in + *'"result":"success"'*) ;; + *) echo "fx: rate service returned an error"; exit 1 ;; +esac + +printf '%s' "$json" | awk ' +function rate(cur, s) { + if (match($0, "\"" cur "\":[0-9.]+")) { + s = substr($0, RSTART, RLENGTH) + sub(/^.*:/, "", s) + return s + 0 + } + return -1 +} +{ + e = rate("EUR"); p = rate("PLN"); t = rate("THB") + if (e <= 0 || p <= 0 || t <= 0) { + print "fx: incomplete rate data" + exit 1 + } + if (match($0, /"time_last_update_utc":"[^"]*"/)) { + d = substr($0, RSTART, RLENGTH) + sub(/^.*:"/, "", d) + sub(/"$/, "", d) + split(d, w, " ") + print "FX rates " w[1] " " w[2] " " w[3] " " w[4] + } + printf "1 USD = %.4g EUR\n", e + printf "1 USD = %.4g PLN\n", p + printf "1 USD = %.4g THB\n", t + printf "1 EUR = %.4g PLN\n", p / e + printf "1 EUR = %.4g THB\n", t / e + printf "1 PLN = %.4g THB\n", t / p +} +' |
