Skip to content

Commit

Permalink
Improvement for illegal command
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow authored and waybackarchiver committed May 23, 2021
1 parent a57a7dc commit beb8933
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions service/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/base64"
"fmt"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -176,12 +177,12 @@ func (t *Telegram) process(message *telegram.Message) error {
}
}
return nil
case command == "/":
case command != "":
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.Payload, fallback))
t.reply(message, fmt.Sprintf("/%s is an illegal command%s", command, fallback))
case len(urls) == 0:
logger.Info("[telegram] archives failure, URL no found.")
metrics.IncrementWayback(metrics.ServiceTelegram, metrics.StatusRequest)
Expand Down Expand Up @@ -367,16 +368,22 @@ func callbackPrefix() string {
}

func command(message string) string {
matchCmd := func(str string) string {
re := regexp.MustCompile(`(?m)^\/\w+`)
for _, match := range re.FindAllString(str, -1) {
return strings.TrimLeft(match, "/")
}
return ""
}

switch {
case strings.HasPrefix(message, "/help"), strings.HasPrefix(message, "/start"):
return "help"
case strings.HasPrefix(message, "/playback"):
return "playback"
case strings.HasPrefix(message, "/metrics"):
return "metrics"
case strings.HasPrefix(message, "/"):
return "/"
default:
return ""
return matchCmd(message)
}
}

0 comments on commit beb8933

Please sign in to comment.