fix: now command description and name cannot be empty making the program

panicing before the bot starts.
This commit is contained in:
Andrey Parhomenko 2023-10-11 14:45:35 +03:00
parent 6324599eed
commit 0a6e1d4d70
3 changed files with 30 additions and 30 deletions

View file

@ -271,51 +271,45 @@ WithUsage(tg.Func(func(c *tg.Context){
})).WithPreStart(tg.Func(func(c *tg.Context){ })).WithPreStart(tg.Func(func(c *tg.Context){
c.Sendf("Please, use /start ") c.Sendf("Please, use /start ")
})).WithCommands( })).WithCommands(
tg.NewCommand("info"). 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`) c.SendfHTML(`<a href="https://res.cloudinary.com/demo/image/upload/v1312461204/sample.jpg">cock</a><strong>cock</strong> die`)
}), }),
tg.NewCommand("start"). tg.NewCommand(
Desc( "start",
"start or restart the bot or move to the start screen", "start or restart the bot or move to the start screen",
).Go("/"), ).Go("/"),
tg.NewCommand("hello"). tg.NewCommand("hello", "sends the 'Hello, World!' message back").
Desc("sends the 'Hello, World!' message back").
ActionFunc(func(c *tg.Context) { ActionFunc(func(c *tg.Context) {
c.Sendf("Hello, World!") c.Sendf("Hello, World!")
}), }),
tg.NewCommand("read"). tg.NewCommand("read", "reads a string and sends it back").
Desc("reads a string and sends it back").
WithWidget( 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") str := c.ReadString("Type a string and I will send it back")
c.Sendf2("You typed `%s`", str) c.Sendf2("You typed `%s`", str)
}), }),
), ),
tg.NewCommand("image"). tg.NewCommand("image", "sends a sample image").
Desc("sends a sample image").
ActionFunc(func(c *tg.Context) { ActionFunc(func(c *tg.Context) {
img := tg.NewFile("media/cat.jpg").Image().Caption("A cat!") img := tg.NewFile("media/cat.jpg").Image().Caption("A cat!")
c.Send(img) c.Send(img)
}), }),
tg.NewCommand("botname"). tg.NewCommand("botname", "get the bot name").
Desc("get the bot name").
WithAction(tg.Func(func(c *tg.Context) { WithAction(tg.Func(func(c *tg.Context) {
bd := c.Bot.Data.(*BotData) bd := c.Bot.Data.(*BotData)
c.Sendf("My name is %q", bd.Name) c.Sendf("My name is %q", bd.Name)
})), })),
tg.NewCommand("dynamic"). tg.NewCommand("dynamic", "check of the dynamic work").
Desc("check of the dynamic work").
WithWidget(tg.Func(func(c *tg.Context){ WithWidget(tg.Func(func(c *tg.Context){
})), })),
tg.NewCommand("history"). tg.NewCommand("history", "print go history").
Desc("print go history").
WithAction(tg.Func(func(c *tg.Context){ WithAction(tg.Func(func(c *tg.Context){
c.Sendf("%q", c.History()) c.Sendf("%q", c.History())
})), })),
tg.NewCommand("washington"). tg.NewCommand("washington", "send location of the Washington").
Desc("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( c.Send(
tg.NewMessage("").Location( tg.NewMessage("").Location(
47.751076, -120.740135, 47.751076, -120.740135,

View file

@ -117,13 +117,13 @@ func (bot *Bot) DeleteCommands() {
func (bot *Bot) SetCommands( func (bot *Bot) SetCommands(
scope tgbotapi.BotCommandScope, scope tgbotapi.BotCommandScope,
cmdMap CommandMap, cmdMap CommandMap,
) { ) error {
// First the private commands. // First the private commands.
names := []string{} names := []string{}
for name := range cmdMap { for name := range cmdMap {
names = append(names, string(name)) names = append(names, string(name))
} }
sort.Strings([]string(names)) sort.Strings(names)
cmds := []*Command{} cmds := []*Command{}
for _, name := range names { for _, name := range names {
@ -144,7 +144,11 @@ func (bot *Bot) SetCommands(
botCmds..., botCmds...,
) )
bot.Api.Request(cfg) _, err := bot.Api.Request(cfg)
if err != nil {
return err
}
return nil
} }
// Run the bot with the Behaviour. // Run the bot with the Behaviour.

View file

@ -24,9 +24,13 @@ type Command struct {
} }
type CommandMap map[CommandName]*Command type CommandMap map[CommandName]*Command
func NewCommand(name CommandName) *Command { func NewCommand(name CommandName, desc string) *Command {
if name == "" || desc == "" {
panic("name and description cannot be an empty string")
}
return &Command{ return &Command{
Name: name, Name: name,
Description: desc,
} }
} }
@ -55,11 +59,6 @@ func (c *Command) ToApi() tgbotapi.BotCommand {
return ret return ret
} }
func (c *Command) Desc(desc string) *Command {
c.Description = desc
return c
}
func (c *Command) Go(pth Path, args ...any) *Command { func (c *Command) Go(pth Path, args ...any) *Command {
return c.WithAction(ScreenGo{ return c.WithAction(ScreenGo{
Path: pth, Path: pth,
@ -139,10 +138,13 @@ func (compo *CommandCompo) Serve(c *Context) {
commanders[k] = v commanders[k] = v
}*/ }*/
c.Bot.DeleteCommands() c.Bot.DeleteCommands()
c.Bot.SetCommands( err := c.Bot.SetCommands(
tgbotapi.NewBotCommandScopeAllPrivateChats(), tgbotapi.NewBotCommandScopeChat(c.Session.Id.ToApi()),
compo.Commands, compo.Commands,
) )
if err != nil {
c.Sendf("error: %q", err)
}
var cmdUpdates *UpdateChan var cmdUpdates *UpdateChan
for u := range c.Input() { for u := range c.Input() {