diff --git a/bot/commands/code.go b/bot/commands/code.go new file mode 100644 index 0000000..07c7be4 --- /dev/null +++ b/bot/commands/code.go @@ -0,0 +1,46 @@ +package commands + +import ( + "github.com/drypa/ReceiptCollector/bot/backend" + "github.com/drypa/ReceiptCollector/bot/backend/user" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "log" + "regexp" +) + +type ConfirmationCodeCommand struct { + provider *user.Provider + grpcClient *backend.GrpcClient + regexp *regexp.Regexp +} + +func NewConfirmationCodeCommand(provider *user.Provider, grpcClient *backend.GrpcClient) *ConfirmationCodeCommand { + command := ConfirmationCodeCommand{ + provider: provider, + grpcClient: grpcClient, + regexp: regexp.MustCompile(`^/code\s+(?P\d{4})$`), + } + return &command +} + +func (c ConfirmationCodeCommand) Accepted(message string) bool { + return c.regexp.MatchString(message) +} + +func (c ConfirmationCodeCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) error { + userId, err := c.provider.GetUserId(update.Message.From.ID) + if err != nil { + return err + } + code := c.getCodeFromRequest(update.Message.Text) + log.Printf("User %s verify phone with code %s\n", userId, code) + //TODO: send to server + return nil +} + +func (c ConfirmationCodeCommand) getCodeFromRequest(message string) string { + matches := c.regexp.FindStringSubmatch(message) + index := c.regexp.SubexpIndex("code") + code := matches[index] + return code +} diff --git a/bot/commands/code_test.go b/bot/commands/code_test.go new file mode 100644 index 0000000..9c531b6 --- /dev/null +++ b/bot/commands/code_test.go @@ -0,0 +1,60 @@ +package commands + +import "testing" + +func TestAccepted_ConfirmationCodeCommand_Success(t *testing.T) { + command := NewConfirmationCodeCommand(nil, nil) + + variants := make([]string, 0) + variants = append(variants, "/code 1234") + variants = append(variants, "/code 9876") + variants = append(variants, "/code 0000") + variants = append(variants, "/code 0001") + + for _, s := range variants { + res := command.Accepted(s) + if !res { + t.Fatalf("message '%s' is not acceptable for ConfirmationCodeCommand", s) + } + } +} + +func TestAccepted_ConfirmationCodeCommand_Failed(t *testing.T) { + command := NewConfirmationCodeCommand(nil, nil) + + variants := make([]string, 0) + variants = append(variants, "") + variants = append(variants, " ") + variants = append(variants, " ") + variants = append(variants, "/code") + variants = append(variants, "code 1234") + variants = append(variants, "/code1234") + variants = append(variants, " /code 1234 ") + variants = append(variants, "/code +") + variants = append(variants, "/code ++++") + variants = append(variants, "/code dddd") + variants = append(variants, "/code 1") + variants = append(variants, "/code 12") + variants = append(variants, "/code 123") + variants = append(variants, "/code 12345") + variants = append(variants, "/code +.*") + + for _, s := range variants { + res := command.Accepted(s) + if res { + t.Fatalf("message '%s' is accepted for RegisterCommand", s) + } + } +} + +func Test_getCodeFromRequest_Success(t *testing.T) { + command := NewConfirmationCodeCommand(nil, nil) + + message := "/code 0101" + + phone := command.getCodeFromRequest(message) + expected := "0101" + if phone != expected { + t.Fatalf("wrong code returned from getCodeFromRequest. Expeced '%s' but got '%s'", expected, phone) + } +}