Skip to content

Commit

Permalink
Append defaults telegram command to fallback text
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow committed May 6, 2021
1 parent bb179a2 commit 13e2050
Showing 1 changed file with 60 additions and 11 deletions.
71 changes: 60 additions & 11 deletions service/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ func (t *Telegram) process(ctx context.Context, update telegram.Update) error {
switch {
case command == "help":
t.reply(message, config.Opts.TelegramHelptext())
case command == "playback", command == "search":
case command == "playback":
return t.playback(message, urls)
case command == "status", strings.HasPrefix(command, "metric"):
case command == "metrics":
stats := metrics.Gather.Export("wayback")
if config.Opts.EnabledMetrics() && stats != "" {
return t.reply(message, stats)
}
return nil
case message.IsCommand():
commands := t.myCommands()
if commands != "" {
commands = fmt.Sprintf("\n\nAvailable commands:\n%s", commands)
fallback := t.commandFallback()
if fallback != "" {
fallback = fmt.Sprintf("\n\nAvailable commands:\n%s", fallback)
}
t.reply(message, fmt.Sprintf("/%s is no specified command%s", message.Command(), commands))
t.reply(message, fmt.Sprintf("/%s is no specified command%s", message.Command(), fallback))
case len(urls) == 0:
logger.Info("[telegram] archives failure, URL no found.")
metrics.IncrementWayback(metrics.ServiceTelegram, metrics.StatusRequest)
Expand Down Expand Up @@ -215,18 +215,67 @@ func (t *Telegram) reply(message *telegram.Message, text string) error {
return nil
}

func (t *Telegram) myCommands() string {
func (t *Telegram) commandFallback() string {
commands := t.myCommands()

var list string
for _, command := range commands {
list += fmt.Sprintf("/%s - %s\n", command.Command, command.Description)
}

return list
}

func (t *Telegram) myCommands() []telegram.BotCommand {
commands, err := t.bot.GetMyCommands()
if err != nil {
return ""
logger.Error("[telegram] got my failed: %v", err)
}

var list string
var maps = make(map[string]bool, len(commands))
for _, command := range commands {
list = fmt.Sprintf("/%s - %s\n", command.Command, command.Description)
maps[command.Command] = true
}

return list
for _, command := range defaultCommands() {
if maps[command.Command] {
continue
}
commands = append(commands, command)
}

return commands
}

func (t *Telegram) setCommands() (error, bool) {
commands, err := t.bot.GetMyCommands()
if err != nil {
logger.Error("[telegram] got my failed: %v", err)
return err, false
}
logger.Debug("[telegram] got my commands: %v", commands)

// TODO
telegram.NewSetMyCommands(defaultCommands()...)

return nil, true
}

func defaultCommands() []telegram.BotCommand {
return []telegram.BotCommand{
{
Command: "help",
Description: "Show help information",
},
{
Command: "playback",
Description: "Playback archived url",
},
{
Command: "metrics",
Description: "Show service metrics",
},
}
}

func callbackPrefix() string {
Expand Down

0 comments on commit 13e2050

Please sign in to comment.