aichaos / rivescript-go

A RiveScript interpreter for Go. RiveScript is a scripting language for chatterbots.
https://www.rivescript.com/
MIT License
60 stars 16 forks source link

Javascript nonfunctional? #46

Closed jerrimus closed 1 year ago

jerrimus commented 1 year ago

Tried with compiled and self-compiled versions.

Using examples from https://pkg.go.dev/github.com/aichaos/rivescript-go/lang/javascript any time I try to execute any javascript, I just get my reply for unknown question.

I got the pre-compiled from the releases page, just pulled and built the cmd directory here. Am I compiling it the wrong way?

kirsle commented 1 year ago

Here is a complete example where JavaScript works:

package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"

    "github.com/aichaos/rivescript-go"
    "github.com/aichaos/rivescript-go/lang/javascript"
)

func main() {
    bot := rivescript.New(&rivescript.Config{Debug: true})
    jsHandler := javascript.New(bot)
    bot.SetHandler("javascript", jsHandler)

    bot.Stream(`
    > object add javascript
        var a = args[0];
        var b = args[1];
        return parseInt(a) + parseInt(b);
    < object

    > object setvar javascript
        // Set the user's name via JavaScript
        var uid = rs.CurrentUser();
        rs.SetUservar(args[0], args[1], args[2]);
    < object

    + add # and #
    - <star1> + <star2> = <call>add <star1> <star2></call>

    + my name is *
    - I will remember that.<call>setvar <id> name <formal></call>

    + what is my name
    - You are <get name>.
    `)
    bot.SortReplies()

    // Drop into the interactive command shell.
    reader := bufio.NewReader(os.Stdin)
    for {
        fmt.Print("You>")
        text, _ := reader.ReadString('\n')
        text = strings.TrimSpace(text)
        if len(text) == 0 {
            continue
        }

        reply, err := bot.Reply("localuser", text)
        if err != nil {
            fmt.Print("Error>", err.Error(), "\n")
        } else {
            fmt.Print("RiveScript>", reply, "\n")
        }
    }
}

Some notes I found: