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 {
@ -57,8 +55,8 @@ func ExtractSessionData(c *tg.Context) *SessionData {
}
var (
homeButton = tg.NewButton("Home").Go("/")
backButton = tg.NewButton("Back").Go("-")
homeButton = tg.NewButton("Home").Go("/")
backButton = tg.NewButton("Back").Go("-")
backKeyboard = tg.NewKeyboard().Row(
backButton,
)
@ -70,9 +68,9 @@ var (
l := c.Message.Location
c.Sendf(
"Longitude: %f\n"+
"Latitude: %f\n"+
"Heading: %d"+
"",
"Latitude: %f\n"+
"Heading: %d"+
"",
l.Longitude,
l.Latitude,
l.Heading,
@ -84,13 +82,13 @@ var (
)
var beh = tg.NewBehaviour().
WithInitFunc(func(c *tg.Context) {
// The session initialization.
c.Session.Data = &SessionData{}
}).WithRootNode(tg.NewRootNode(
WithInitFunc(func(c *tg.Context) {
// The session initialization.
c.Session.Data = &SessionData{}
}).WithRootNode(tg.NewRootNode(
// The "/" widget.
tg.RenderFunc(func(c *tg.Context) tg.UI {
return tg.UI {
return tg.UI{
tg.NewMessage(fmt.Sprintf(
fmt.Sprint(
"Hello, %s!\n",
@ -108,15 +106,15 @@ WithInitFunc(func(c *tg.Context) {
tg.NewMessage("Choose your interest").Reply(
tg.NewKeyboard().Row(
tg.NewButton("Inc/Dec").Go("/inc-dec"),
).Row(
tg.NewButton("Mutate messages").Go("/mutate-messages"),
).Row(
tg.NewButton("Send location").Go("/send-location"),
).Reply(),
tg.NewButton("Inc/Dec").Go("/inc-dec"),
).Row(
tg.NewButton("Mutate messages").Go("/mutate-messages"),
).Row(
tg.NewButton("Send location").Go("/send-location"),
).Reply(),
),
tg.Func(func(c *tg.Context){
tg.Func(func(c *tg.Context) {
for u := range c.Input() {
if u.EditedMessage != nil {
c.Sendf2("The new message is `%s`", u.EditedMessage.Text)
@ -188,32 +186,31 @@ 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"
incBtn := tg.NewButton("+").ActionFunc(func(c *tg.Context) {
d.Counter++
kbd.Text = fmt.Sprintf(format, d.Counter)
if d.Counter == 5 {
kbd.Inline = onlyDec
} else {
kbd.Inline = std
}
kbd.Update(c)
})
d.Counter++
kbd.Text = fmt.Sprintf(format, d.Counter)
if d.Counter == 5 {
kbd.Inline = onlyDec
} else {
kbd.Inline = std
}
kbd.Update(c)
})
decBtn := tg.NewButton("-").ActionFunc(func(c *tg.Context) {
d.Counter--
kbd.Text = fmt.Sprintf(format, d.Counter)
if d.Counter == -5 {
kbd.Inline = onlyInc
} else {
kbd.Inline = std
}
kbd.Update(c)
//c.Sendf("%d", d.Counter)
})
d.Counter--
kbd.Text = fmt.Sprintf(format, d.Counter)
if d.Counter == -5 {
kbd.Inline = onlyInc
} else {
kbd.Inline = std
}
kbd.Update(c)
//c.Sendf("%d", d.Counter)
})
onlyInc = tg.NewKeyboard().Row(incBtn).Inline()
onlyDec = tg.NewKeyboard().Row(decBtn).Inline()
@ -227,7 +224,6 @@ WithInitFunc(func(c *tg.Context) {
inline = std
}
kbd = tg.NewMessage(
fmt.Sprintf(format, d.Counter),
).Inline(inline)
@ -243,11 +239,11 @@ WithInitFunc(func(c *tg.Context) {
tg.NewNode(
"send-location", tg.RenderFunc(func(c *tg.Context) tg.UI {
return tg.UI {
return tg.UI{
tg.NewMessage(
"Press the button to display your counter",
).Inline(
tg.NewKeyboard().Row(
tg.NewKeyboard().Row(
tg.NewButton(
"Check",
).WithData(
@ -268,13 +264,13 @@ WithInitFunc(func(c *tg.Context) {
}),
),
)).WithRoot(tg.NewCommandCompo().
WithUsage(tg.Func(func(c *tg.Context){
c.Sendf("There is no such command %q", c.Message.Command())
})).WithPreStart(tg.Func(func(c *tg.Context){
WithUsage(tg.Func(func(c *tg.Context) {
c.Sendf("There is no such command %q", c.Message.Command())
})).WithPreStart(tg.Func(func(c *tg.Context) {
c.Sendf("Please, use /start ")
})).WithCommands(
tg.NewCommand("info", "info desc").
ActionFunc(func(c *tg.Context){
ActionFunc(func(c *tg.Context) {
c.SendfHTML(`<a href="https://res.cloudinary.com/demo/image/upload/v1312461204/sample.jpg">cock</a><strong>cock</strong> die`)
}),
tg.NewCommand(
@ -287,7 +283,7 @@ WithUsage(tg.Func(func(c *tg.Context){
}),
tg.NewCommand("read", "reads a string and sends it back").
WithWidget(
tg.Func(func(c *tg.Context){
tg.Func(func(c *tg.Context) {
str := c.ReadString("Type a string and I will send it back")
c.Sendf2("You typed `%s`", str)
}),
@ -303,14 +299,14 @@ WithUsage(tg.Func(func(c *tg.Context){
c.Sendf("My name is %q", bd.Name)
})),
tg.NewCommand("dynamic", "check of the dynamic work").
WithWidget(tg.Func(func(c *tg.Context){
WithWidget(tg.Func(func(c *tg.Context) {
})),
tg.NewCommand("history", "print go history").
WithAction(tg.Func(func(c *tg.Context){
WithAction(tg.Func(func(c *tg.Context) {
c.Sendf("%q", c.History())
})),
tg.NewCommand("washington", "send location of the Washington").
WithAction(tg.Func(func(c *tg.Context){
WithAction(tg.Func(func(c *tg.Context) {
c.Sendf("Washington location")
c.Send(
tg.NewMessage("").Location(
@ -319,9 +315,10 @@ WithUsage(tg.Func(func(c *tg.Context){
)
})),
tg.NewCommand("invoice", "invoice check").
WithAction(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