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){
c.Sendf("Please, use /start ")
})).WithCommands(
tg.NewCommand("info").
tg.NewCommand("info", "info desc").
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").
tg.NewCommand(
"start",
"start or restart the bot or move to the start screen",
).Go("/"),
tg.NewCommand("hello", "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").
tg.NewCommand("read", "reads a string and sends it back").
WithWidget(
tg.Func(func(c *tg.Context){
str := c.ReadString("Type a string and I will send it back")
c.Sendf2("You typed `%s`", str)
}),
),
tg.NewCommand("image").
Desc("sends a sample image").
tg.NewCommand("image", "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").
tg.NewCommand("botname", "get the bot name").
WithAction(tg.Func(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").
tg.NewCommand("dynamic", "check of the dynamic work").
WithWidget(tg.Func(func(c *tg.Context){
})),
tg.NewCommand("history").
Desc("print go history").
tg.NewCommand("history", "print go history").
WithAction(tg.Func(func(c *tg.Context){
c.Sendf("%q", c.History())
})),
tg.NewCommand("washington").
Desc("Send location of the Washington").
tg.NewCommand("washington", "send location of the Washington").
WithAction(tg.Func(func(c *tg.Context){
c.Sendf("Washington location")
c.Send(
tg.NewMessage("").Location(
47.751076, -120.740135,

View file

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

View file

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