Rebranding.

This commit is contained in:
Andrey Parhomenko 2023-10-22 20:41:01 +03:00
parent 32ec32e8c9
commit 70846dbe22
45 changed files with 72 additions and 374 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ exe
*.exe
*.swp
tmp
testbot*

View file

View file

View file

@ -1,13 +0,0 @@
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 => ./../..

View file

@ -1,6 +0,0 @@
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=

View file

@ -1,223 +0,0 @@
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/tg"
)
type UserData struct {
Counter int
}
type Code struct {
Code string
Add int
}
func NewCode(code string) *Code {
return &Code{
Code: code,
}
}
func (c *Code) Act(a *tg.Context) {
var err error
fmt.Println("In Act")
e := env.NewEnv()
e.Define("a", a)
e.Define("NotAvailableErr", tg.NotAvailableErr)
e.Define("panic", func(v any) { panic(v) })
err = e.DefineType("UserData", UserData{})
if err != nil {
panic(err)
}
_, err = vm.Execute(e, nil, c.Code)
if err != nil {
panic(err)
}
}
func main() {
tg.DefineAction("goscript", &Code{})
var startScreenButton = tg.NewButton("🏠 To the start screen").
WithAction(NewCode(`
a.ChangeScreen("start")
`))
var (
incDecKeyboard = tg.NewKeyboard("").Row(
tg.NewButton("+").WithAction(NewCode(`
d = a.V
d.Counter++
a.Sendf("%d", d.Counter)
`)),
tg.NewButton("-").WithAction(NewCode(`
d = a.V
d.Counter--
a.Sendf("%d", d.Counter)
`)),
).Row(
startScreenButton,
)
// The navigational keyboard.
navKeyboard = tg.NewKeyboard("").Row(
tg.NewButton("Inc/Dec").WithAction(NewCode(`a.ChangeScreen("inc/dec")`)),
).Row(
tg.NewButton("Upper case").WithAction(NewCode(`a.ChangeScreen("upper-case")`)),
tg.NewButton("Lower case").WithAction(NewCode(`a.ChangeScreen("lower-case")`)),
).Row(
tg.NewButton("Send location").
WithSendLocation(true).
WithAction(NewCode(`
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)
}
`)),
)
inlineKeyboard = tg.NewKeyboard("").Row(
tg.NewButton("My Telegram").
WithUrl("https://t.me/surdeus"),
)
// The keyboard to return to the start screen.
navToStartKeyboard = tg.NewKeyboard("nav-start").Row(
startScreenButton,
)
)
var beh = tg.NewBehaviour().
// The function will be called every time
// the bot is started.
WithInit(NewCode(`
a.V = new(UserData)
`)).
WithScreens(
tg.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!",
).WithKeyboard(navKeyboard).
WithIKeyboard(inlineKeyboard),
tg.NewScreen("inc/dec").
WithText(
"The screen shows how "+
"user separated data works "+
"by saving the counter for each of users "+
"separately. ",
).
WithKeyboard(incDecKeyboard).
// The function will be called when reaching the screen.
WithAction(NewCode(`
d = a.V
a.Sendf("Current counter value = %d", d.Counter)
`)),
tg.NewScreen("upper-case").
WithText("Type text and the bot will send you the upper case version to you").
WithKeyboard(navToStartKeyboard).
WithAction(NewCode(`
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)
}
}
`)),
tg.NewScreen("lower-case").
WithText("Type text and the bot will send you the lower case version").
WithKeyboard(navToStartKeyboard).
WithAction(NewCode(`
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(
tg.NewCommand("start").
Desc("start or restart the bot").
WithAction(NewCode(`
a.ChangeScreen("start")
`)),
tg.NewCommand("hello").
Desc("sends the 'Hello, World!' message back").
WithAction(NewCode(`
a.Send("Hello, World!")
`)),
tg.NewCommand("read").
Desc("reads a string and sends it back").
WithAction(NewCode(`
a.Send("Type some text:")
msg, err = a.ReadTextMessage()
if err != nil {
return
}
a.Sendf("You typed %q", msg)
`)),
)
bts, err := json.MarshalIndent(beh, "", "\t")
if err != nil {
panic(err)
}
fmt.Printf("%s", bts)
jBeh := &tg.Behaviour{}
err = json.Unmarshal(bts, jBeh)
if err != nil {
panic(err)
}
bot, err := tg.NewBot(os.Getenv("BOT_TOKEN"))
if err != nil {
panic(err)
}
bot = bot.WithBehaviour(jBeh)
err = bot.Run()
if err != nil {
panic(err)
}
}

View file

@ -1,5 +0,0 @@
all:
go build
run:V:
./jsoned

View file

@ -6,9 +6,7 @@ import (
"strings"
"fmt"
"github.com/mojosa-software/got/tg"
//"math/rand"
//"strconv"
"github.com/reklesio/tg"
)
type BotData struct {
@ -188,7 +186,6 @@ WithInitFunc(func(c *tg.Context) {
inline, std, onlyInc, onlyDec *tg.Inline
)
d := ExtractSessionData(c)
format := "Press the buttons to increment and decrement.\n" +
"Current counter value = %d"
@ -227,7 +224,6 @@ WithInitFunc(func(c *tg.Context) {
inline = std
}
kbd = tg.NewMessage(
fmt.Sprintf(format, d.Counter),
).Inline(inline)
@ -322,6 +318,7 @@ WithUsage(tg.Func(func(c *tg.Context){
WithAction(tg.Func(func(c *tg.Context) {
})),
))
func main() {
log.Println(beh.Screens)
token := os.Getenv("BOT_TOKEN")

View file

7
go.mod
View file

@ -1,8 +1,5 @@
module github.com/mojosa-software/got
module github.com/reklesio/tg
go 1.20
require (
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
github.com/mojosa-software/godat v0.0.0-20230711170316-a335bad31575 // indirect
)
require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1

2
go.sum
View file

@ -1,4 +1,2 @@
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/godat v0.0.0-20230711170316-a335bad31575 h1:wtaYzLbEND7JSY7L+huFU4IDT4nbuDolKRfnQ1d6KTc=
github.com/mojosa-software/godat v0.0.0-20230711170316-a335bad31575/go.mod h1:E6ohOj8PpUJBQOSRdrLygjoO+Te6yfeox3ZtaItsHUg=

View file

View file

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2023 Andrey Parhomenko
Copyright (c) 2023 surdeus
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

Binary file not shown.

Before

Width:  |  Height:  |  Size: 723 KiB

BIN
media/gopher.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 KiB

11
mkfile
View file

@ -1,11 +0,0 @@
all: build
run-air:V:
air -c airfile
build:V:
go build -o exe/ ./cmd/...
clean:
rm -rf exe/*

View file

View file

@ -1,5 +1,6 @@
package tg
// Still to be implemented.
type Poll struct {
}

View file

@ -1,7 +1,14 @@
# got v0.3.2
# TeleGopher
Go Telegram.
![Put here TeleGopher image, please]()
The module implements behaviour based interactions with user,
screens with buttons, etc.
The high level API to implement complicated
behaviour based Telegram bots in W3 manner
with URLs, screens, UIs and so on.
## Tasks
- [] Tech
- [] Added easy way to work with payments
- [] Make more documentation including the Go embedded one
- [] Do some examples to show how it works

View file

6
taskfile.yml Normal file
View file

@ -0,0 +1,6 @@
version: 3
tasks:
build:
cmds:
- go build -o testbot ./cmd/test/

View file

@ -1,37 +0,0 @@
package tg
import (
"reflect"
)
var (
actionMapByReflect = make(map[reflect.Type]string)
actionMapByTypeName = make(map[string]reflect.Type)
Init func()
)
func initEncoding() {
actions := map[string]Action{
"action-func": ActionFunc(nil),
"screen-change": ScreenGo{},
}
for k, action := range actions {
DefineAction(k, action)
}
}
// Define interface to make it marshalable to JSON etc.
// Like in GOB. Must be done both on client and server
// if one is provided.
func DefineAction(typeName string, a Action) error {
t := reflect.TypeOf(a)
actionMapByReflect[t] = typeName
actionMapByTypeName[typeName] = t
return nil
}
/*func DefineGroupAction(typ string, a GroupAction) error {
return nil
}*/

View file

@ -1,2 +0,0 @@
package tg

View file

@ -1,5 +0,0 @@
package tg
func init() {
initEncoding()
}

View file

@ -1,2 +0,0 @@
package tg

View file

@ -1,2 +0,0 @@
package tg

View file

@ -1,3 +0,0 @@
package tg

View file