Lists of commands
This commit is contained in:
		
							
								
								
									
										24
									
								
								command.go
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								command.go
									
									
									
									
									
								
							@ -2,7 +2,10 @@ package nechotron
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/NicoNex/echotron/v3"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Command struct {
 | 
					type Command struct {
 | 
				
			||||||
@ -26,12 +29,29 @@ func (c *Command) Param(text string) string {
 | 
				
			|||||||
	return strings.TrimPrefix(text, "/"+c.Body+" ")
 | 
						return strings.TrimPrefix(text, "/"+c.Body+" ")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func MakeCommandList(commands []*Command, format string) string {
 | 
					func MakeCommandList(format string, commands ...*Command) string {
 | 
				
			||||||
	result := ""
 | 
						result := ""
 | 
				
			||||||
	for _, command := range commands {
 | 
						for _, command := range commands {
 | 
				
			||||||
		result += fmt.Sprintf(format, command.String(), command.Description)
 | 
							result += fmt.Sprintf(format, command.String(), EscapeMd2(command.Description))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return result
 | 
						return result
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func botCommands(commands ...*Command) []echotron.BotCommand {
 | 
				
			||||||
 | 
						cmds := []echotron.BotCommand{}
 | 
				
			||||||
 | 
						for _, c := range commands {
 | 
				
			||||||
 | 
							cmds = append(cmds, echotron.BotCommand{Command: c.Body, Description: c.Description})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return cmds
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func SetMyCommands(api echotron.API, langCode string, scope echotron.BotCommandScope, commands ...*Command) error {
 | 
				
			||||||
 | 
						_, err := api.SetMyCommands(&echotron.CommandOptions{LanguageCode: "", Scope: scope}, botCommands(commands...)...)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Println(err)
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// func HandleCommand(command *Command, handler cmdFunc) (bool, error)
 | 
					// func HandleCommand(command *Command, handler cmdFunc) (bool, error)
 | 
				
			||||||
 | 
				
			|||||||
@ -196,8 +196,11 @@ func (u *Update) Text() string {
 | 
				
			|||||||
	if u.IsText() {
 | 
						if u.IsText() {
 | 
				
			||||||
		return u.Message.Text
 | 
							return u.Message.Text
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if u.IsMessage() {
 | 
				
			||||||
		return u.Message.Caption
 | 
							return u.Message.Caption
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						return ""
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (u *Update) Entities() []*echotron.MessageEntity {
 | 
					func (u *Update) Entities() []*echotron.MessageEntity {
 | 
				
			||||||
	if u.IsText() {
 | 
						if u.IsText() {
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user