Code: Select all
In handle_message proc with args: {hola bot que tal el tiempo}
User's message: hola bot que tal el tiempo
In sendMessage proc with message: holabot que tal el tiempo
Tcl error [handle_message]: unexpected 'p' in TOP mode
Code: Select all
# Load the required packages
package require json
putlog "json Cargado Correctamente"
# Define the ChatGPT API key
set apiKey "here your API Key"
putlog "API key set"
# Define the ChatGPT API URL
set apiUrl "https://api.openai.com/v1/engines/davinci/completions"
putlog "API URL set: $apiUrl"
# Send a message to ChatGPT using curl
proc sendMessage {message} {
global apiKey apiUrl
putlog "In sendMessage proc with message: $message"
# Construct the request body as a JSON string
set body [json::json2dict [dict create prompt $message temperature 0.7 max_tokens 1000]]
set body [string map {"\n" "\\n" "\t" "\\t" "\r" "\\r"} $body]
putlog "Request body JSON encoded: $body"
# Use curl to send the request
set command [list curl -s -X POST $apiUrl -H "Authorization: Bearer $apiKey" -H "Content-Type: application/json" -d $body]
putlog "Executing curl command: $command"
set response [eval exec $command]
putlog "HTTP response received: $response"
# Parse the response JSON
set json [json::json2dict $response]
putlog "Response JSON parsed: $json"
# Get the response text
if {[dict exists $json choices 0 text]} {
set text [dict get $json choices 0 text]
putlog "Response text extracted: $text"
} else {
set text "Error: [dict get $json error message]"
putlog "Error in response: $text"
}
# Return the response text
return $text
}
# Handle user messages using `bind pub` command
bind pub -|- * handle_message
putlog "Bind command set for handling messages"
# Message handling procedure
proc handle_message {nick uhost handle chan args} {
global apiKey
putlog "In handle_message proc with args: $args"
# Get the user's message
set message [join $args " "]
putlog "User's message: $message"
# Send the message to ChatGPT
set response [sendMessage $message]
putlog "Response from gtpchat: $response"
# Send the response back to the user
puthelp "privmsg $chan :$response"
putlog "Response sent to channel: $chan"
}
putlog "ChatGTP cargado"