This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

help chatGTP

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
flink
Halfop
Posts: 83
Joined: Sun Feb 21, 2021 9:27 am
Location: Canarias

help chatGTP

Post by flink »

hello i would like if someone can review it and give a solution and that the eggdrop answer me in the room because i get this error in the party line:

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"
Mi ingles: no es el mejor, Manda el traductor... :)
h
haferbrei
Voice
Posts: 24
Joined: Wed Apr 08, 2020 8:57 pm

Re: help chatGTP

Post by haferbrei »

It would be best if you could also post the init routine, how the eggdrop is addressed in the channel, and which eggdrop you are using.
Post Reply