just a simple crypto tcl script - it fetches live prices from CoinGecko
Cheers!
Code: Select all
#\00300 - Black
#\00301 - Dark Blue
#\00302 - Dark Green
#\00303 - Red
#\00304 - Dark Red
#\00305 - Dark Purple
#\00306 - Orange
#\00307 - Yellow
#\00308 - Light Green
#\00309 - Teal
#\00310 - Light Blue
#\00311 - Light Purple
#\00312 - Light Yellow
#\00313 - White
#\00314 - Light Gray
#\00315 - Dark Gray
#\00316 - Pink
#\00317 - Light Pink
package require json
bind pub - "!coin" get_price
proc get_price {nick host hand chan args} {
# Ensure args is passed correctly
if {[llength $args] != 1} {
puthelp "PRIVMSG $chan :Usage: !coin <crypto>"
return
}
# Extract the coin name from the arguments
set coin [string tolower [lindex $args 0]]
# Fetch coin data from CoinGecko API (Simple Price API for better reliability)
set url "https://api.coingecko.com/api/v3/simple/price?ids=$coin&vs_currencies=usd&include_24hr_change=true"
set data [exec curl -s $url]
# Debugging: Print API response to ensure it's valid
putlog "API Response: $data"
# Parse JSON response
set json_data [json::json2dict $data]
# Check if coin exists in the response
if {[dict exists $json_data $coin]} {
set coin_data [dict get $json_data $coin]
# Extract values safely
set price [dict get $coin_data usd]
set change [expr {[dict exists $coin_data usd_24h_change] ? [dict get $coin_data usd_24h_change] : 0}]
# Get the coin's name and symbol from the coin details API
set coin_details_url "https://api.coingecko.com/api/v3/coins/$coin"
set coin_details_data [exec curl -s $coin_details_url]
set coin_details_json [json::json2dict $coin_details_data]
if {[dict exists $coin_details_json "name"] && [dict exists $coin_details_json "symbol"]} {
set coin_name [dict get $coin_details_json "name"]
set coin_symbol [dict get $coin_details_json "symbol"]
} else {
set coin_name "Unknown"
set coin_symbol "Unknown"
}
# Format values
set formatted_price [format "%.2f" $price]
set formatted_change [format "%.2f" $change]
# Set the color and arrow based on the price change
# Set the color and arrow based on the price change
set arrow ""
set color ""
if {$change > 0} {
set arrow "▲" ;# Bigger upward arrow
set color "\00303" ;# Green
} elseif {$change < 0} {
set arrow "▼" ;# Bigger downward arrow
set color "\00304" ;# Red
} else {
set arrow "➤" ;# Neutral arrow
set color "\00307" ;# Gray
}
# Send formatted message to the channel
puthelp "PRIVMSG $chan :\00304☢ ᴇxᴘʟᴏɪᴛᴇʀ ☢\003 \00302$coin_name \00306(\00304$coin_symbol\00306) -- \00302Price:\00305 \$${formatted_price} \00302USD -- $color$arrow $formatted_change%\003 -- \00302Last update:\00304 [clock format [clock seconds] -format {%a, %d-%b-%y %T %Z}] "
} else {
puthelp "PRIVMSG $chan :-☢- use a correct coin name - !coin bitcoin -. \00308- ᴇxᴘʟᴏɪᴛᴇʀ - "
}
}