From e3045862c474535915c48c8a52ac55bc265368ad Mon Sep 17 00:00:00 2001 From: surdeus Date: Sat, 12 Aug 2023 19:06:23 +0300 Subject: [PATCH] Implemented completely dynamic based code execution program in cmd/json using the Anko. --- cmd/json/go.mod | 13 +++ cmd/json/go.sum | 6 ++ cmd/json/main.go | 208 +++++++++++++++++++++++++++++++++++++++++++++++ src/tx/action.go | 13 +++ 4 files changed, 240 insertions(+) create mode 100644 cmd/json/go.mod create mode 100644 cmd/json/go.sum create mode 100644 cmd/json/main.go diff --git a/cmd/json/go.mod b/cmd/json/go.mod new file mode 100644 index 0000000..d0fb628 --- /dev/null +++ b/cmd/json/go.mod @@ -0,0 +1,13 @@ +module jsoned + +go 1.20 + +require ( + github.com/mojosa-software/goscript v0.0.0-20230626091305-86a004b7769c + github.com/mojosa-software/got v0.0.0-20230812125405-bbe076f29abe +) + +require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect + +replace github.com/mojosa-software/got => ./../.. + diff --git a/cmd/json/go.sum b/cmd/json/go.sum new file mode 100644 index 0000000..f0fe3cf --- /dev/null +++ b/cmd/json/go.sum @@ -0,0 +1,6 @@ +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= +github.com/mojosa-software/goscript v0.0.0-20230626091305-86a004b7769c h1:y7RQZz/zJDARRJkn4szD8N2rK6K9NU1vUNPwahtW5zw= +github.com/mojosa-software/goscript v0.0.0-20230626091305-86a004b7769c/go.mod h1:LtBn7lQTgA/TMEL8Y+dGkD6XWHV2gxRPZXiqCZt3HRc= +github.com/mojosa-software/got v0.0.0-20230812125405-bbe076f29abe h1:k42GTLSdShyzNC2BAuIwg6FV3ShwMt/42ezBJWCxVgI= +github.com/mojosa-software/got v0.0.0-20230812125405-bbe076f29abe/go.mod h1:eNRBO08YxKEj75u65VRCDpMGCgZ1sqPN6pi6aFdkfK8= diff --git a/cmd/json/main.go b/cmd/json/main.go new file mode 100644 index 0000000..776f5f9 --- /dev/null +++ b/cmd/json/main.go @@ -0,0 +1,208 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + + //"strings" + + _ "github.com/mojosa-software/goscript/packages" + + "github.com/mojosa-software/goscript/env" + "github.com/mojosa-software/goscript/vm" + "github.com/mojosa-software/got/src/tx" +) + +type UserData struct { + Counter int +} + +type Code string + +func (c Code) Act(a *tx.A) { + var err error + fmt.Println("In Act") + e := env.NewEnv() + e.Define("a", a) + e.Define("NotAvailableErr", tx.NotAvailableErr) + e.Define("panic", func(v any) { panic(v) }) + err = e.DefineType("UserData", UserData{}) + if err != nil { + panic(err) + } + + _, err = vm.Execute(e, nil, string(c)) + if err != nil { + panic(err) + } +} + +var startScreenButton = tx.NewButton("🏠 To the start screen"). + WithAction(Code(` + a.ChangeScreen("start") + `)) + +var beh = tx.NewBehaviour(). + + // The function will be called every time + // the bot is started. + WithStart(Code(` + a.V = new(UserData) + a.ChangeScreen("start") + `)).WithKeyboards( + + // Increment/decrement keyboard. + tx.NewKeyboard("inc/dec").Row( + tx.NewButton("+").WithAction(Code(` + d = a.V + d.Counter++ + a.Sendf("%d", d.Counter) + `)), + tx.NewButton("-").WithAction(Code(` + d = a.V + d.Counter-- + a.Sendf("%d", d.Counter) + `)), + ).Row( + startScreenButton, + ), + + // The navigational keyboard. + tx.NewKeyboard("nav").Row( + tx.NewButton("Inc/Dec").WithAction(Code(`a.ChangeScreen("inc/dec")`)), + ).Row( + tx.NewButton("Upper case").WithAction(Code(`a.ChangeScreen("upper-case")`)), + tx.NewButton("Lower case").WithAction(Code(`a.ChangeScreen("lower-case")`)), + ).Row( + tx.NewButton("Send location"). + WithSendLocation(true). + WithAction(Code(` + err = nil + if a.U.Message.Location != nil { + l = a.U.Message.Location + err = a.Sendf("Longitude: %f\nLatitude: %f\nHeading: %d", l.Longitude, l.Latitude, l.Heading) + } else { + err = a.Send("Somehow wrong location was sent") + } + if err != nil { + a.Send(err) + } + `)), + ), + + tx.NewKeyboard("istart").Row( + tx.NewButton("My Telegram"). + WithUrl("https://t.me/surdeus"), + ), + + // The keyboard to return to the start screen. + tx.NewKeyboard("nav-start").Row( + startScreenButton, + ), +).WithScreens( + tx.NewScreen("start"). + WithText( + "The bot started!"+ + " The bot is supposed to provide basic"+ + " understand of how the API works, so just"+ + " horse around a bit to guess everything out"+ + " by yourself!", + ).Keyboard("nav"). + IKeyboard("istart"), + + tx.NewScreen("inc/dec"). + WithText( + "The screen shows how "+ + "user separated data works "+ + "by saving the counter for each of users "+ + "separately. ", + ). + Keyboard("inc/dec"). + // The function will be called when reaching the screen. + WithAction(Code(` + d = a.V + a.Sendf("Current counter value = %d", d.Counter) + `)), + + tx.NewScreen("upper-case"). + WithText("Type text and the bot will send you the upper case version to you"). + Keyboard("nav-start"). + WithAction(Code(` + strings = import("strings") + for { + msg, err = a.ReadTextMessage() + if err == NotAvailableErr { + break + } else if err != nil { + panic(err) + } + + err = a.Sendf("%s", strings.ToUpper(msg)) + if err != nil { + panic(err) + } + } + `)), + + tx.NewScreen("lower-case"). + WithText("Type text and the bot will send you the lower case version"). + Keyboard("nav-start"). + WithAction(Code(` + strings = import("strings") + for { + msg, err = a.ReadTextMessage() + if err == NotAvailableErr { + break + } else if err != nil { + panic(err) + } + + err = a.Sendf("%s", strings.ToLower(msg)) + if err != nil { + panic(err) + } + } + `)), +).WithCommands( + tx.NewCommand("hello"). + Desc("sends the 'Hello, World!' message back"). + WithAction(Code(` + a.Send("Hello, World!") + `)), + tx.NewCommand("read"). + Desc("reads a string and sends it back"). + WithAction(Code(` + a.Send("Type some text:") + msg, err = a.ReadTextMessage() + if err != nil { + return + } + a.Sendf("You typed %q", msg) + `)), +) + +func main() { + bts, err := json.MarshalIndent(beh, "", "\t") + if err != nil { + panic(err) + } + fmt.Printf("%s", bts) + + /*jBeh := &tx.Behaviour{} + err = json.Unmarshal(bts, jBeh) + if err != nil { + panic(err) + }*/ + + bot, err := tx.NewBot(os.Getenv("BOT_TOKEN"), beh, nil) + if err != nil { + panic(err) + } + + err = bot.Run() + if err != nil { + panic(err) + } + +} diff --git a/src/tx/action.go b/src/tx/action.go index ae1b8bf..8e93d84 100644 --- a/src/tx/action.go +++ b/src/tx/action.go @@ -25,6 +25,19 @@ type Action interface { Act(*Arg) } +type JsonTyper interface { + JsonType() string +} + +type JsonAction struct { + Type string + Action Action +} + +func (ja JsonAction) UnmarshalJSON(bts []byte, ptr any) error { + return nil +} + // Customized action for the bot. type ActionFunc func(*Arg)