tg/cmd/test/main.go

291 lines
6.3 KiB
Go
Raw Normal View History

2023-07-03 17:22:45 +03:00
package main
2023-07-08 22:26:21 +03:00
import (
"log"
"os"
"strings"
"fmt"
2023-07-08 22:26:21 +03:00
2023-08-19 09:12:26 +03:00
"github.com/mojosa-software/got/tg"
"math/rand"
"strconv"
2023-07-09 01:28:59 +03:00
)
type BotData struct {
Name string
}
type SessionData struct {
2023-08-12 14:35:33 +03:00
Counter int
}
type MutateMessageWidget struct {
Mutate func(string) string
}
func NewMutateMessageWidget(fn func(string) string) *MutateMessageWidget {
ret := &MutateMessageWidget{}
ret.Mutate = fn
return ret
}
func (w *MutateMessageWidget) Serve(c *tg.Context) {
args, ok := c.Arg.(tg.ArgSlice)
if ok {
for _, arg := range args {
c.Sendf("%v", arg)
}
}
for u := range c.Input() {
text := u.Message.Text
c.Sendf("%s", w.Mutate(text))
}
}
func (w *MutateMessageWidget) Filter(u *tg.Update, _ tg.MessageMap) bool {
if u.Message == nil {
return true
}
return false
}
func ExtractSessionData(c *tg.Context) *SessionData {
return c.Session.Data.(*SessionData)
}
2023-08-15 16:02:14 +03:00
var (
2023-09-22 11:17:18 +03:00
homeButton = tg.NewButton("Home").Go("/")
backButton = tg.NewButton("Back").Go("..")
backKeyboard = tg.NewKeyboard().Row(
backButton,
)
incDecKeyboard = tg.NewKeyboard().Row(
2023-08-19 09:12:26 +03:00
tg.NewButton("+").ActionFunc(func(c *tg.Context) {
d := ExtractSessionData(c)
2023-08-12 14:35:33 +03:00
d.Counter++
c.Sendf("%d", d.Counter)
}),
2023-08-19 09:12:26 +03:00
tg.NewButton("-").ActionFunc(func(c *tg.Context) {
d := ExtractSessionData(c)
2023-08-12 14:35:33 +03:00
d.Counter--
c.Sendf("%d", d.Counter)
}),
).Row(
2023-09-22 11:17:18 +03:00
backButton,
2023-08-15 16:02:14 +03:00
)
2023-08-11 11:04:28 +03:00
navKeyboard =
2023-08-11 11:04:28 +03:00
sendLocationKeyboard = tg.NewKeyboard().Row(
tg.NewButton("Send location").
WithSendLocation(true).
ActionFunc(func(c *tg.Context) {
l := c.Message.Location
c.Sendf(
"Longitude: %f\n"+
"Latitude: %f\n"+
"Heading: %d"+
"",
l.Longitude,
l.Latitude,
l.Heading,
)
}),
).Row(
2023-09-22 11:17:18 +03:00
backButton,
).Reply()
2023-08-15 16:02:14 +03:00
)
2023-08-19 09:12:26 +03:00
var beh = tg.NewBehaviour().
WithInitFunc(func(c *tg.Context) {
// The session initialization.
c.Session.Data = &SessionData{}
}).WithRootNode(tg.NewRootNode(
// The "/" widget.
tg.WidgetFunc(func(c *tg.Context) tg.UIs {
return tg.UIs{
tg.NewKeyboard().Row(
tg.NewButton("GoT Github page").
WithUrl("https://github.com/mojosa-software/got"),
).Inline().Widget(
fmt.Sprintf(
"Hello, %s"
"The testing bot started!\n",
"You can see the basics of usage in the ",
"cmd/test/main.go file!",
c.SentFrom().UserName,
),
),
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().Widget(
"Choose the point of your interest",
),
}
)
tg.NewNode(
"mutate-messages", tg.NewPage().WithReply(
tg.NewKeyboard().Row(
tg.NewButton("Upper case").Go("upper-case"),
tg.NewButton("Lower case").Go("lower-case"),
).Row(
backButton,
2023-09-22 12:59:39 +03:00
).Reply().Widget(
"Choose the function to mutate string",
),
),
tg.NewNode(
"upper-case", tg.NewPage().WithReply(
backKeyboard.Reply().
Widget(
"Type a string and the bot will convert it to upper case",
),
).WithSub(
NewMutateMessageWidget(strings.ToUpper),
),
),
tg.NewNode(
"lower-case", tg.NewPage().WithReply(
backKeyboard.Reply().
Widget(
"Type a string and the bot will convert it to lower case",
),
).WithSub(
NewMutateMessageWidget(strings.ToLower),
),
),
),
tg.NewNode(
"inc-dec", tg.NewPage().WithReply(
incDecKeyboard.Reply().Widget("Press the buttons to increment and decrement"),
).ActionFunc(func(c *tg.Context) {
// The function will be calleb before serving page.
d := ExtractSessionData(c)
c.Sendf("Current counter value = %d", d.Counter)
}),
),
2023-08-11 11:04:28 +03:00
tg.NewNode(
"send-location", tg.NewPage().WithReply(
sendLocationKeyboard.Widget("Press the button to send your location!"),
).WithInline(
tg.NewKeyboard().Row(
tg.NewButton(
"Check",
).WithData(
"check",
).ActionFunc(func(c *tg.Context) {
d := ExtractSessionData(c)
c.Sendf("Counter = %d", d.Counter)
}),
).Inline().Widget("Press the button to display your counter"),
2023-08-15 16:02:14 +03:00
),
),
)).WithCommands(
2023-09-22 11:17:18 +03:00
tg.NewCommand("info").
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("start").
Desc(
"start or restart the bot or move to the start screen",
).Go("/"),
tg.NewCommand("hello").
Desc("sends the 'Hello, World!' message back").
ActionFunc(func(c *tg.Context) {
c.Sendf("Hello, World!")
}),
tg.NewCommand("read").
Desc("reads a string and sends it back").
2023-09-22 11:17:18 +03:00
WithWidget(
2023-09-22 12:59:39 +03:00
tg.Func(func(c *tg.Context){
str := c.ReadString("Type a string and I will send it back")
c.Sendf2("You typed `%s`", str)
}),
2023-09-22 11:17:18 +03:00
),
tg.NewCommand("image").
Desc("sends a sample image").
ActionFunc(func(c *tg.Context) {
img := tg.NewFile("media/cat.jpg").Image().Caption("A cat!")
c.Send(img)
}),
tg.NewCommand("botname").
Desc("get the bot name").
ActionFunc(func(c *tg.Context) {
bd := c.Bot.Data.(*BotData)
c.Sendf("My name is %q", bd.Name)
}),
tg.NewCommand("dynamic").
Desc("check of the dynamic work").
WidgetFunc(func(c *tg.Context){
nRow, nBtn := rand.Int()%10, rand.Int()%5
rows := []tg.ButtonRow{}
for i:=0 ; i<nRow ; i++ {
row := []*tg.Button{}
for j:=0 ; j<nBtn ; j++ {
row = append(row, tg.NewButton(
strconv.Itoa(i) + " " + strconv.Itoa(j),
))
}
rows = append(rows, row)
}
kbd := tg.NewKeyboard(rows...).ActionFunc(func(c *tg.Context){
c.Sendf(u.)
})Inline().Widget("sample text")
c.Send(kbd)
kbdChn := c.RunWidget(kbd)
for u := range c.Input() {
if kbd.Filter(u, nil) {
continue
}
kbdChn.Send(u)
}
}),
)
2023-07-09 01:28:59 +03:00
2023-08-19 09:12:26 +03:00
var gBeh = tg.NewGroupBehaviour().
InitFunc(func(c *tg.GC) {
2023-08-13 15:37:36 +03:00
}).
WithCommands(
2023-08-19 09:12:26 +03:00
tg.NewGroupCommand("hello").ActionFunc(func(c *tg.GC) {
c.Sendf("Hello, World!")
2023-08-13 15:37:36 +03:00
}),
2023-08-19 09:12:26 +03:00
tg.NewGroupCommand("mycounter").ActionFunc(func(c *tg.GC) {
d := c.Session().Data.(*SessionData)
c.Sendf("Your counter value is %d", d.Counter)
2023-08-13 15:37:36 +03:00
}),
)
2023-07-03 17:22:45 +03:00
func main() {
log.Println(beh.Screens)
2023-07-08 22:26:21 +03:00
token := os.Getenv("BOT_TOKEN")
2023-08-19 09:12:26 +03:00
bot, err := tg.NewBot(token)
if err != nil {
log.Panic(err)
}
2023-08-13 15:37:36 +03:00
bot = bot.
WithBehaviour(beh).
Debug(true)
2023-07-08 22:26:21 +03:00
bot.Data = &BotData{
Name: "Jay",
}
log.Printf("Authorized on account %s", bot.Api.Self.UserName)
err = bot.Run()
if err != nil {
panic(err)
}
}