diff --git a/cmd/json/main.go b/cmd/json/main.go index a5b3695..2056bd1 100644 --- a/cmd/json/main.go +++ b/cmd/json/main.go @@ -11,7 +11,7 @@ import ( "github.com/mojosa-software/goscript/env" "github.com/mojosa-software/goscript/vm" - "github.com/mojosa-software/got/src/tx" + "github.com/mojosa-software/got/tg" ) type UserData struct { @@ -29,12 +29,12 @@ func NewCode(code string) *Code { } } -func (c *Code) Act(a *tx.A) { +func (c *Code) Act(a *tg.Context) { var err error fmt.Println("In Act") e := env.NewEnv() e.Define("a", a) - e.Define("NotAvailableErr", tx.NotAvailableErr) + e.Define("NotAvailableErr", tg.NotAvailableErr) e.Define("panic", func(v any) { panic(v) }) err = e.DefineType("UserData", UserData{}) if err != nil { @@ -48,21 +48,21 @@ func (c *Code) Act(a *tx.A) { } func main() { - tx.DefineAction("goscript", &Code{}) + tg.DefineAction("goscript", &Code{}) - var startScreenButton = tx.NewButton("🏠 To the start screen"). + var startScreenButton = tg.NewButton("🏠 To the start screen"). WithAction(NewCode(` a.ChangeScreen("start") `)) var ( - incDecKeyboard = tx.NewKeyboard("").Row( - tx.NewButton("+").WithAction(NewCode(` + incDecKeyboard = tg.NewKeyboard("").Row( + tg.NewButton("+").WithAction(NewCode(` d = a.V d.Counter++ a.Sendf("%d", d.Counter) `)), - tx.NewButton("-").WithAction(NewCode(` + tg.NewButton("-").WithAction(NewCode(` d = a.V d.Counter-- a.Sendf("%d", d.Counter) @@ -72,13 +72,13 @@ func main() { ) // The navigational keyboard. - navKeyboard = tx.NewKeyboard("").Row( - tx.NewButton("Inc/Dec").WithAction(NewCode(`a.ChangeScreen("inc/dec")`)), + navKeyboard = tg.NewKeyboard("").Row( + tg.NewButton("Inc/Dec").WithAction(NewCode(`a.ChangeScreen("inc/dec")`)), ).Row( - tx.NewButton("Upper case").WithAction(NewCode(`a.ChangeScreen("upper-case")`)), - tx.NewButton("Lower case").WithAction(NewCode(`a.ChangeScreen("lower-case")`)), + tg.NewButton("Upper case").WithAction(NewCode(`a.ChangeScreen("upper-case")`)), + tg.NewButton("Lower case").WithAction(NewCode(`a.ChangeScreen("lower-case")`)), ).Row( - tx.NewButton("Send location"). + tg.NewButton("Send location"). WithSendLocation(true). WithAction(NewCode(` err = nil @@ -94,24 +94,24 @@ func main() { `)), ) - inlineKeyboard = tx.NewKeyboard("").Row( - tx.NewButton("My Telegram"). + inlineKeyboard = tg.NewKeyboard("").Row( + tg.NewButton("My Telegram"). WithUrl("https://t.me/surdeus"), ) // The keyboard to return to the start screen. - navToStartKeyboard = tx.NewKeyboard("nav-start").Row( + navToStartKeyboard = tg.NewKeyboard("nav-start").Row( startScreenButton, ) ) - var beh = tx.NewBehaviour(). + var beh = tg.NewBehaviour(). // The function will be called every time // the bot is started. WithInit(NewCode(` a.V = new(UserData) `)). WithScreens( - tx.NewScreen("start"). + tg.NewScreen("start"). WithText( "The bot started!"+ " The bot is supposed to provide basic"+ @@ -121,7 +121,7 @@ func main() { ).WithKeyboard(navKeyboard). WithIKeyboard(inlineKeyboard), - tx.NewScreen("inc/dec"). + tg.NewScreen("inc/dec"). WithText( "The screen shows how "+ "user separated data works "+ @@ -135,7 +135,7 @@ func main() { a.Sendf("Current counter value = %d", d.Counter) `)), - tx.NewScreen("upper-case"). + tg.NewScreen("upper-case"). WithText("Type text and the bot will send you the upper case version to you"). WithKeyboard(navToStartKeyboard). WithAction(NewCode(` @@ -155,7 +155,7 @@ func main() { } `)), - tx.NewScreen("lower-case"). + tg.NewScreen("lower-case"). WithText("Type text and the bot will send you the lower case version"). WithKeyboard(navToStartKeyboard). WithAction(NewCode(` @@ -175,17 +175,17 @@ func main() { } `)), ).WithCommands( - tx.NewCommand("start"). + tg.NewCommand("start"). Desc("start or restart the bot"). WithAction(NewCode(` a.ChangeScreen("start") `)), - tx.NewCommand("hello"). + tg.NewCommand("hello"). Desc("sends the 'Hello, World!' message back"). WithAction(NewCode(` a.Send("Hello, World!") `)), - tx.NewCommand("read"). + tg.NewCommand("read"). Desc("reads a string and sends it back"). WithAction(NewCode(` a.Send("Type some text:") @@ -202,13 +202,13 @@ func main() { } fmt.Printf("%s", bts) - jBeh := &tx.Behaviour{} + jBeh := &tg.Behaviour{} err = json.Unmarshal(bts, jBeh) if err != nil { panic(err) } - bot, err := tx.NewBot(os.Getenv("BOT_TOKEN")) + bot, err := tg.NewBot(os.Getenv("BOT_TOKEN")) if err != nil { panic(err) } diff --git a/cmd/test/main.go b/cmd/test/main.go index b8ad2e3..c3c68ae 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -5,7 +5,7 @@ import ( "os" "strings" - "github.com/mojosa-software/got/src/tx" + "github.com/mojosa-software/got/tg" ) type UserData struct { @@ -13,16 +13,16 @@ type UserData struct { } var ( - startScreenButton = tx.NewButton("🏠 To the start screen"). + startScreenButton = tg.NewButton("🏠 To the start screen"). ScreenChange("start") - incDecKeyboard = tx.NewKeyboard("").Row( - tx.NewButton("+").ActionFunc(func(c *tx.Context) { + incDecKeyboard = tg.NewKeyboard("").Row( + tg.NewButton("+").ActionFunc(func(c *tg.Context) { d := c.V.(*UserData) d.Counter++ c.Sendf("%d", d.Counter) }), - tx.NewButton("-").ActionFunc(func(c *tx.Context) { + tg.NewButton("-").ActionFunc(func(c *tg.Context) { d := c.SessionValue().(*UserData) d.Counter-- c.Sendf("%d", d.Counter) @@ -31,22 +31,22 @@ var ( startScreenButton, ) - navKeyboard = tx.NewKeyboard("Choose your interest"). + navKeyboard = tg.NewKeyboard("Choose your interest"). WithOneTime(true). Row( - tx.NewButton("Inc/Dec").ScreenChange("inc/dec"), + tg.NewButton("Inc/Dec").ScreenChange("inc/dec"), ).Row( - tx.NewButton("Upper case").ScreenChange("upper-case"), - tx.NewButton("Lower case").ScreenChange("lower-case"), + tg.NewButton("Upper case").ScreenChange("upper-case"), + tg.NewButton("Lower case").ScreenChange("lower-case"), ).Row( - tx.NewButton("Send location").ScreenChange("send-location"), + tg.NewButton("Send location").ScreenChange("send-location"), ) - sendLocationKeyboard = tx.NewKeyboard("Press the button to send your location"). + sendLocationKeyboard = tg.NewKeyboard("Press the button to send your location"). Row( - tx.NewButton("Send location"). + tg.NewButton("Send location"). WithSendLocation(true). - ActionFunc(func(c *tx.Context) { + ActionFunc(func(c *tg.Context) { var err error if c.Message.Location != nil { l := c.Message.Location @@ -71,19 +71,19 @@ var ( ) // The keyboard to return to the start screen. - navToStartKeyboard = tx.NewKeyboard("").Row( + navToStartKeyboard = tg.NewKeyboard("").Row( startScreenButton, ) ) -var beh = tx.NewBehaviour(). - WithInitFunc(func(c *tx.Context) { +var beh = tg.NewBehaviour(). + WithInitFunc(func(c *tg.Context) { // The session initialization. c.V = &UserData{} c.ChangeScreen("start") }).WithScreens( - tx.NewScreen("start"). + tg.NewScreen("start"). WithText( "The bot started!"+ " The bot is supposed to provide basic"+ @@ -93,13 +93,13 @@ var beh = tx.NewBehaviour(). ).WithKeyboard(navKeyboard). // The inline keyboard with link to GitHub page. WithIKeyboard( - tx.NewKeyboard("istart").Row( - tx.NewButton("GoT Github page"). + tg.NewKeyboard("istart").Row( + tg.NewButton("GoT Github page"). WithUrl("https://github.com/mojosa-software/got"), ), ), - tx.NewScreen("inc/dec"). + tg.NewScreen("inc/dec"). WithText( "The screen shows how "+ "user separated data works "+ @@ -108,43 +108,43 @@ var beh = tx.NewBehaviour(). ). WithKeyboard(incDecKeyboard). // The function will be called when reaching the screen. - ActionFunc(func(c *tx.Context) { + ActionFunc(func(c *tg.Context) { d := c.V.(*UserData) c.Sendf("Current counter value = %d", d.Counter) }), - tx.NewScreen("upper-case"). + tg.NewScreen("upper-case"). WithText("Type text and the bot will send you the upper case version to you"). WithKeyboard(navToStartKeyboard). ActionFunc(mutateMessage(strings.ToUpper)), - tx.NewScreen("lower-case"). + tg.NewScreen("lower-case"). WithText("Type text and the bot will send you the lower case version"). WithKeyboard(navToStartKeyboard). ActionFunc(mutateMessage(strings.ToLower)), - tx.NewScreen("send-location"). + tg.NewScreen("send-location"). WithText("Send your location and I will tell where you are!"). WithKeyboard(sendLocationKeyboard). WithIKeyboard( - tx.NewKeyboard("").Row( - tx.NewButton("Check"). + tg.NewKeyboard("").Row( + tg.NewButton("Check"). WithData("check"). - ActionFunc(func(a *tx.Context) { + ActionFunc(func(a *tg.Context) { d := a.V.(*UserData) a.Sendf("Counter = %d", d.Counter) }), ), ), ).WithCommands( - tx.NewCommand("hello"). + tg.NewCommand("hello"). Desc("sends the 'Hello, World!' message back"). - ActionFunc(func(c *tx.Context) { + ActionFunc(func(c *tg.Context) { c.Send("Hello, World!") }), - tx.NewCommand("read"). + tg.NewCommand("read"). Desc("reads a string and sends it back"). - ActionFunc(func(c *tx.Context) { + ActionFunc(func(c *tg.Context) { c.Send("Type some text:") msg, err := c.ReadTextMessage() if err != nil { @@ -154,11 +154,11 @@ var beh = tx.NewBehaviour(). }), ) -func mutateMessage(fn func(string) string) tx.ActionFunc { - return func(c *tx.Context) { +func mutateMessage(fn func(string) string) tg.ActionFunc { + return func(c *tg.Context) { for { msg, err := c.ReadTextMessage() - if err == tx.NotAvailableErr { + if err == tg.NotAvailableErr { break } else if err != nil { panic(err) @@ -172,14 +172,14 @@ func mutateMessage(fn func(string) string) tx.ActionFunc { } } -var gBeh = tx.NewGroupBehaviour(). - InitFunc(func(c *tx.GC) { +var gBeh = tg.NewGroupBehaviour(). + InitFunc(func(c *tg.GC) { }). WithCommands( - tx.NewGroupCommand("hello").ActionFunc(func(c *tx.GC) { + tg.NewGroupCommand("hello").ActionFunc(func(c *tg.GC) { c.Send("Hello, World!") }), - tx.NewGroupCommand("mycounter").ActionFunc(func(c *tx.GC) { + tg.NewGroupCommand("mycounter").ActionFunc(func(c *tg.GC) { d := c.SessionValue().(*UserData) c.Sendf("Your counter value is %d", d.Counter) }), @@ -188,7 +188,7 @@ var gBeh = tx.NewGroupBehaviour(). func main() { token := os.Getenv("BOT_TOKEN") - bot, err := tx.NewBot(token) + bot, err := tg.NewBot(token) if err != nil { log.Panic(err) } diff --git a/src/tx/action.go b/tg/action.go similarity index 97% rename from src/tx/action.go rename to tg/action.go index 7812ed3..bc67b26 100644 --- a/src/tx/action.go +++ b/tg/action.go @@ -1,4 +1,4 @@ -package tx +package tg import ( "reflect" diff --git a/src/tx/beh.go b/tg/beh.go similarity index 99% rename from src/tx/beh.go rename to tg/beh.go index 2e95ecc..192baef 100644 --- a/src/tx/beh.go +++ b/tg/beh.go @@ -1,4 +1,4 @@ -package tx +package tg // The package implements // behaviour for the Telegram bots. diff --git a/src/tx/bot.go b/tg/bot.go similarity index 99% rename from src/tx/bot.go rename to tg/bot.go index 4a5ba5e..18ff4d4 100644 --- a/src/tx/bot.go +++ b/tg/bot.go @@ -1,4 +1,4 @@ -package tx +package tg import ( "errors" diff --git a/src/tx/button.go b/tg/button.go similarity index 99% rename from src/tx/button.go rename to tg/button.go index bbc78e4..d6813a8 100644 --- a/src/tx/button.go +++ b/tg/button.go @@ -1,4 +1,4 @@ -package tx +package tg import ( apix "github.com/go-telegram-bot-api/telegram-bot-api/v5" diff --git a/src/tx/command.go b/tg/command.go similarity index 99% rename from src/tx/command.go rename to tg/command.go index df2c93d..cc0a3f2 100644 --- a/src/tx/command.go +++ b/tg/command.go @@ -1,4 +1,4 @@ -package tx +package tg import ( //"flag" diff --git a/src/tx/context.go b/tg/context.go similarity index 99% rename from src/tx/context.go rename to tg/context.go index b6f84af..0ab6e46 100644 --- a/src/tx/context.go +++ b/tg/context.go @@ -1,4 +1,4 @@ -package tx +package tg import ( "fmt" diff --git a/src/tx/encoding.go b/tg/encoding.go similarity index 98% rename from src/tx/encoding.go rename to tg/encoding.go index 8896af8..34569d6 100644 --- a/src/tx/encoding.go +++ b/tg/encoding.go @@ -1,4 +1,4 @@ -package tx +package tg import ( "reflect" diff --git a/src/tx/errors.go b/tg/errors.go similarity index 98% rename from src/tx/errors.go rename to tg/errors.go index de7f982..2923381 100644 --- a/src/tx/errors.go +++ b/tg/errors.go @@ -1,4 +1,4 @@ -package tx +package tg import ( "errors" diff --git a/src/tx/group.go b/tg/group.go similarity index 97% rename from src/tx/group.go rename to tg/group.go index b46205a..a47f401 100644 --- a/src/tx/group.go +++ b/tg/group.go @@ -1,4 +1,4 @@ -package tx +package tg // Customized actions for the group behaviour. type GroupAction interface { diff --git a/src/tx/json.go b/tg/json.go similarity index 98% rename from src/tx/json.go rename to tg/json.go index 75a1ff8..e7dfa2c 100644 --- a/src/tx/json.go +++ b/tg/json.go @@ -1,4 +1,4 @@ -package tx +package tg import ( "encoding/json" diff --git a/src/tx/keyboard.go b/tg/keyboard.go similarity index 99% rename from src/tx/keyboard.go rename to tg/keyboard.go index ed3db80..08a45ad 100644 --- a/src/tx/keyboard.go +++ b/tg/keyboard.go @@ -1,4 +1,4 @@ -package tx +package tg import ( apix "github.com/go-telegram-bot-api/telegram-bot-api/v5" diff --git a/src/tx/main.go b/tg/main.go similarity index 75% rename from src/tx/main.go rename to tg/main.go index fe470ea..261c852 100644 --- a/src/tx/main.go +++ b/tg/main.go @@ -1,4 +1,4 @@ -package tx +package tg func init() { initEncoding() diff --git a/src/tx/private.go b/tg/private.go similarity index 99% rename from src/tx/private.go rename to tg/private.go index 2be40d3..5a889c6 100644 --- a/src/tx/private.go +++ b/tg/private.go @@ -1,4 +1,4 @@ -package tx +package tg // Interface to interact with the user. type Context struct { diff --git a/src/tx/screen.go b/tg/screen.go similarity index 99% rename from src/tx/screen.go rename to tg/screen.go index 909fd53..792553a 100644 --- a/src/tx/screen.go +++ b/tg/screen.go @@ -1,4 +1,4 @@ -package tx +package tg import ( apix "github.com/go-telegram-bot-api/telegram-bot-api/v5" diff --git a/src/tx/session.go b/tg/session.go similarity index 99% rename from src/tx/session.go rename to tg/session.go index d8a606a..7dcb1e5 100644 --- a/src/tx/session.go +++ b/tg/session.go @@ -1,4 +1,4 @@ -package tx +package tg // Represents unique value to identify chats. // In fact is simply ID of the chat.