Renamed tx => tg .

This commit is contained in:
Andrey Parhomenko 2023-08-19 09:12:26 +03:00
parent 34faec2f8b
commit b5fbdac972
17 changed files with 80 additions and 80 deletions

View file

@ -11,7 +11,7 @@ import (
"github.com/mojosa-software/goscript/env" "github.com/mojosa-software/goscript/env"
"github.com/mojosa-software/goscript/vm" "github.com/mojosa-software/goscript/vm"
"github.com/mojosa-software/got/src/tx" "github.com/mojosa-software/got/tg"
) )
type UserData struct { 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 var err error
fmt.Println("In Act") fmt.Println("In Act")
e := env.NewEnv() e := env.NewEnv()
e.Define("a", a) e.Define("a", a)
e.Define("NotAvailableErr", tx.NotAvailableErr) e.Define("NotAvailableErr", tg.NotAvailableErr)
e.Define("panic", func(v any) { panic(v) }) e.Define("panic", func(v any) { panic(v) })
err = e.DefineType("UserData", UserData{}) err = e.DefineType("UserData", UserData{})
if err != nil { if err != nil {
@ -48,21 +48,21 @@ func (c *Code) Act(a *tx.A) {
} }
func main() { 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(` WithAction(NewCode(`
a.ChangeScreen("start") a.ChangeScreen("start")
`)) `))
var ( var (
incDecKeyboard = tx.NewKeyboard("").Row( incDecKeyboard = tg.NewKeyboard("").Row(
tx.NewButton("+").WithAction(NewCode(` tg.NewButton("+").WithAction(NewCode(`
d = a.V d = a.V
d.Counter++ d.Counter++
a.Sendf("%d", d.Counter) a.Sendf("%d", d.Counter)
`)), `)),
tx.NewButton("-").WithAction(NewCode(` tg.NewButton("-").WithAction(NewCode(`
d = a.V d = a.V
d.Counter-- d.Counter--
a.Sendf("%d", d.Counter) a.Sendf("%d", d.Counter)
@ -72,13 +72,13 @@ func main() {
) )
// The navigational keyboard. // The navigational keyboard.
navKeyboard = tx.NewKeyboard("").Row( navKeyboard = tg.NewKeyboard("").Row(
tx.NewButton("Inc/Dec").WithAction(NewCode(`a.ChangeScreen("inc/dec")`)), tg.NewButton("Inc/Dec").WithAction(NewCode(`a.ChangeScreen("inc/dec")`)),
).Row( ).Row(
tx.NewButton("Upper case").WithAction(NewCode(`a.ChangeScreen("upper-case")`)), tg.NewButton("Upper case").WithAction(NewCode(`a.ChangeScreen("upper-case")`)),
tx.NewButton("Lower case").WithAction(NewCode(`a.ChangeScreen("lower-case")`)), tg.NewButton("Lower case").WithAction(NewCode(`a.ChangeScreen("lower-case")`)),
).Row( ).Row(
tx.NewButton("Send location"). tg.NewButton("Send location").
WithSendLocation(true). WithSendLocation(true).
WithAction(NewCode(` WithAction(NewCode(`
err = nil err = nil
@ -94,24 +94,24 @@ func main() {
`)), `)),
) )
inlineKeyboard = tx.NewKeyboard("").Row( inlineKeyboard = tg.NewKeyboard("").Row(
tx.NewButton("My Telegram"). tg.NewButton("My Telegram").
WithUrl("https://t.me/surdeus"), WithUrl("https://t.me/surdeus"),
) )
// The keyboard to return to the start screen. // The keyboard to return to the start screen.
navToStartKeyboard = tx.NewKeyboard("nav-start").Row( navToStartKeyboard = tg.NewKeyboard("nav-start").Row(
startScreenButton, startScreenButton,
) )
) )
var beh = tx.NewBehaviour(). var beh = tg.NewBehaviour().
// The function will be called every time // The function will be called every time
// the bot is started. // the bot is started.
WithInit(NewCode(` WithInit(NewCode(`
a.V = new(UserData) a.V = new(UserData)
`)). `)).
WithScreens( WithScreens(
tx.NewScreen("start"). tg.NewScreen("start").
WithText( WithText(
"The bot started!"+ "The bot started!"+
" The bot is supposed to provide basic"+ " The bot is supposed to provide basic"+
@ -121,7 +121,7 @@ func main() {
).WithKeyboard(navKeyboard). ).WithKeyboard(navKeyboard).
WithIKeyboard(inlineKeyboard), WithIKeyboard(inlineKeyboard),
tx.NewScreen("inc/dec"). tg.NewScreen("inc/dec").
WithText( WithText(
"The screen shows how "+ "The screen shows how "+
"user separated data works "+ "user separated data works "+
@ -135,7 +135,7 @@ func main() {
a.Sendf("Current counter value = %d", d.Counter) 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"). WithText("Type text and the bot will send you the upper case version to you").
WithKeyboard(navToStartKeyboard). WithKeyboard(navToStartKeyboard).
WithAction(NewCode(` 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"). WithText("Type text and the bot will send you the lower case version").
WithKeyboard(navToStartKeyboard). WithKeyboard(navToStartKeyboard).
WithAction(NewCode(` WithAction(NewCode(`
@ -175,17 +175,17 @@ func main() {
} }
`)), `)),
).WithCommands( ).WithCommands(
tx.NewCommand("start"). tg.NewCommand("start").
Desc("start or restart the bot"). Desc("start or restart the bot").
WithAction(NewCode(` WithAction(NewCode(`
a.ChangeScreen("start") a.ChangeScreen("start")
`)), `)),
tx.NewCommand("hello"). tg.NewCommand("hello").
Desc("sends the 'Hello, World!' message back"). Desc("sends the 'Hello, World!' message back").
WithAction(NewCode(` WithAction(NewCode(`
a.Send("Hello, World!") a.Send("Hello, World!")
`)), `)),
tx.NewCommand("read"). tg.NewCommand("read").
Desc("reads a string and sends it back"). Desc("reads a string and sends it back").
WithAction(NewCode(` WithAction(NewCode(`
a.Send("Type some text:") a.Send("Type some text:")
@ -202,13 +202,13 @@ func main() {
} }
fmt.Printf("%s", bts) fmt.Printf("%s", bts)
jBeh := &tx.Behaviour{} jBeh := &tg.Behaviour{}
err = json.Unmarshal(bts, jBeh) err = json.Unmarshal(bts, jBeh)
if err != nil { if err != nil {
panic(err) panic(err)
} }
bot, err := tx.NewBot(os.Getenv("BOT_TOKEN")) bot, err := tg.NewBot(os.Getenv("BOT_TOKEN"))
if err != nil { if err != nil {
panic(err) panic(err)
} }

View file

@ -5,7 +5,7 @@ import (
"os" "os"
"strings" "strings"
"github.com/mojosa-software/got/src/tx" "github.com/mojosa-software/got/tg"
) )
type UserData struct { type UserData struct {
@ -13,16 +13,16 @@ type UserData struct {
} }
var ( var (
startScreenButton = tx.NewButton("🏠 To the start screen"). startScreenButton = tg.NewButton("🏠 To the start screen").
ScreenChange("start") ScreenChange("start")
incDecKeyboard = tx.NewKeyboard("").Row( incDecKeyboard = tg.NewKeyboard("").Row(
tx.NewButton("+").ActionFunc(func(c *tx.Context) { tg.NewButton("+").ActionFunc(func(c *tg.Context) {
d := c.V.(*UserData) d := c.V.(*UserData)
d.Counter++ d.Counter++
c.Sendf("%d", 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 := c.SessionValue().(*UserData)
d.Counter-- d.Counter--
c.Sendf("%d", d.Counter) c.Sendf("%d", d.Counter)
@ -31,22 +31,22 @@ var (
startScreenButton, startScreenButton,
) )
navKeyboard = tx.NewKeyboard("Choose your interest"). navKeyboard = tg.NewKeyboard("Choose your interest").
WithOneTime(true). WithOneTime(true).
Row( Row(
tx.NewButton("Inc/Dec").ScreenChange("inc/dec"), tg.NewButton("Inc/Dec").ScreenChange("inc/dec"),
).Row( ).Row(
tx.NewButton("Upper case").ScreenChange("upper-case"), tg.NewButton("Upper case").ScreenChange("upper-case"),
tx.NewButton("Lower case").ScreenChange("lower-case"), tg.NewButton("Lower case").ScreenChange("lower-case"),
).Row( ).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( Row(
tx.NewButton("Send location"). tg.NewButton("Send location").
WithSendLocation(true). WithSendLocation(true).
ActionFunc(func(c *tx.Context) { ActionFunc(func(c *tg.Context) {
var err error var err error
if c.Message.Location != nil { if c.Message.Location != nil {
l := c.Message.Location l := c.Message.Location
@ -71,19 +71,19 @@ var (
) )
// The keyboard to return to the start screen. // The keyboard to return to the start screen.
navToStartKeyboard = tx.NewKeyboard("").Row( navToStartKeyboard = tg.NewKeyboard("").Row(
startScreenButton, startScreenButton,
) )
) )
var beh = tx.NewBehaviour(). var beh = tg.NewBehaviour().
WithInitFunc(func(c *tx.Context) { WithInitFunc(func(c *tg.Context) {
// The session initialization. // The session initialization.
c.V = &UserData{} c.V = &UserData{}
c.ChangeScreen("start") c.ChangeScreen("start")
}).WithScreens( }).WithScreens(
tx.NewScreen("start"). tg.NewScreen("start").
WithText( WithText(
"The bot started!"+ "The bot started!"+
" The bot is supposed to provide basic"+ " The bot is supposed to provide basic"+
@ -93,13 +93,13 @@ var beh = tx.NewBehaviour().
).WithKeyboard(navKeyboard). ).WithKeyboard(navKeyboard).
// The inline keyboard with link to GitHub page. // The inline keyboard with link to GitHub page.
WithIKeyboard( WithIKeyboard(
tx.NewKeyboard("istart").Row( tg.NewKeyboard("istart").Row(
tx.NewButton("GoT Github page"). tg.NewButton("GoT Github page").
WithUrl("https://github.com/mojosa-software/got"), WithUrl("https://github.com/mojosa-software/got"),
), ),
), ),
tx.NewScreen("inc/dec"). tg.NewScreen("inc/dec").
WithText( WithText(
"The screen shows how "+ "The screen shows how "+
"user separated data works "+ "user separated data works "+
@ -108,43 +108,43 @@ var beh = tx.NewBehaviour().
). ).
WithKeyboard(incDecKeyboard). WithKeyboard(incDecKeyboard).
// The function will be called when reaching the screen. // The function will be called when reaching the screen.
ActionFunc(func(c *tx.Context) { ActionFunc(func(c *tg.Context) {
d := c.V.(*UserData) d := c.V.(*UserData)
c.Sendf("Current counter value = %d", d.Counter) 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"). WithText("Type text and the bot will send you the upper case version to you").
WithKeyboard(navToStartKeyboard). WithKeyboard(navToStartKeyboard).
ActionFunc(mutateMessage(strings.ToUpper)), 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"). WithText("Type text and the bot will send you the lower case version").
WithKeyboard(navToStartKeyboard). WithKeyboard(navToStartKeyboard).
ActionFunc(mutateMessage(strings.ToLower)), ActionFunc(mutateMessage(strings.ToLower)),
tx.NewScreen("send-location"). tg.NewScreen("send-location").
WithText("Send your location and I will tell where you are!"). WithText("Send your location and I will tell where you are!").
WithKeyboard(sendLocationKeyboard). WithKeyboard(sendLocationKeyboard).
WithIKeyboard( WithIKeyboard(
tx.NewKeyboard("").Row( tg.NewKeyboard("").Row(
tx.NewButton("Check"). tg.NewButton("Check").
WithData("check"). WithData("check").
ActionFunc(func(a *tx.Context) { ActionFunc(func(a *tg.Context) {
d := a.V.(*UserData) d := a.V.(*UserData)
a.Sendf("Counter = %d", d.Counter) a.Sendf("Counter = %d", d.Counter)
}), }),
), ),
), ),
).WithCommands( ).WithCommands(
tx.NewCommand("hello"). tg.NewCommand("hello").
Desc("sends the 'Hello, World!' message back"). Desc("sends the 'Hello, World!' message back").
ActionFunc(func(c *tx.Context) { ActionFunc(func(c *tg.Context) {
c.Send("Hello, World!") c.Send("Hello, World!")
}), }),
tx.NewCommand("read"). tg.NewCommand("read").
Desc("reads a string and sends it back"). Desc("reads a string and sends it back").
ActionFunc(func(c *tx.Context) { ActionFunc(func(c *tg.Context) {
c.Send("Type some text:") c.Send("Type some text:")
msg, err := c.ReadTextMessage() msg, err := c.ReadTextMessage()
if err != nil { if err != nil {
@ -154,11 +154,11 @@ var beh = tx.NewBehaviour().
}), }),
) )
func mutateMessage(fn func(string) string) tx.ActionFunc { func mutateMessage(fn func(string) string) tg.ActionFunc {
return func(c *tx.Context) { return func(c *tg.Context) {
for { for {
msg, err := c.ReadTextMessage() msg, err := c.ReadTextMessage()
if err == tx.NotAvailableErr { if err == tg.NotAvailableErr {
break break
} else if err != nil { } else if err != nil {
panic(err) panic(err)
@ -172,14 +172,14 @@ func mutateMessage(fn func(string) string) tx.ActionFunc {
} }
} }
var gBeh = tx.NewGroupBehaviour(). var gBeh = tg.NewGroupBehaviour().
InitFunc(func(c *tx.GC) { InitFunc(func(c *tg.GC) {
}). }).
WithCommands( WithCommands(
tx.NewGroupCommand("hello").ActionFunc(func(c *tx.GC) { tg.NewGroupCommand("hello").ActionFunc(func(c *tg.GC) {
c.Send("Hello, World!") c.Send("Hello, World!")
}), }),
tx.NewGroupCommand("mycounter").ActionFunc(func(c *tx.GC) { tg.NewGroupCommand("mycounter").ActionFunc(func(c *tg.GC) {
d := c.SessionValue().(*UserData) d := c.SessionValue().(*UserData)
c.Sendf("Your counter value is %d", d.Counter) c.Sendf("Your counter value is %d", d.Counter)
}), }),
@ -188,7 +188,7 @@ var gBeh = tx.NewGroupBehaviour().
func main() { func main() {
token := os.Getenv("BOT_TOKEN") token := os.Getenv("BOT_TOKEN")
bot, err := tx.NewBot(token) bot, err := tg.NewBot(token)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }

View file

@ -1,4 +1,4 @@
package tx package tg
import ( import (
"reflect" "reflect"

View file

@ -1,4 +1,4 @@
package tx package tg
// The package implements // The package implements
// behaviour for the Telegram bots. // behaviour for the Telegram bots.

View file

@ -1,4 +1,4 @@
package tx package tg
import ( import (
"errors" "errors"

View file

@ -1,4 +1,4 @@
package tx package tg
import ( import (
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5" apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"

View file

@ -1,4 +1,4 @@
package tx package tg
import ( import (
//"flag" //"flag"

View file

@ -1,4 +1,4 @@
package tx package tg
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package tx package tg
import ( import (
"reflect" "reflect"

View file

@ -1,4 +1,4 @@
package tx package tg
import ( import (
"errors" "errors"

View file

@ -1,4 +1,4 @@
package tx package tg
// Customized actions for the group behaviour. // Customized actions for the group behaviour.
type GroupAction interface { type GroupAction interface {

View file

@ -1,4 +1,4 @@
package tx package tg
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,4 +1,4 @@
package tx package tg
import ( import (
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5" apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"

View file

@ -1,4 +1,4 @@
package tx package tg
func init() { func init() {
initEncoding() initEncoding()

View file

@ -1,4 +1,4 @@
package tx package tg
// Interface to interact with the user. // Interface to interact with the user.
type Context struct { type Context struct {

View file

@ -1,4 +1,4 @@
package tx package tg
import ( import (
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5" apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"

View file

@ -1,4 +1,4 @@
package tx package tg
// Represents unique value to identify chats. // Represents unique value to identify chats.
// In fact is simply ID of the chat. // In fact is simply ID of the chat.