Added different formatting for telegram bot

This commit is contained in:
Artyom Belousov 2021-06-06 17:21:29 +03:00
parent 35f8fa5a57
commit 89623f5f6a
18 changed files with 233 additions and 173 deletions

View file

@ -4,6 +4,8 @@ import (
"errors"
"log"
"strings"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
const (
@ -13,10 +15,10 @@ const (
)
type Scenario struct {
Sender Sender
Logger *log.Logger
InfoFetcher CardInfoFetcher
Cache CardCache
Sender Sender
Logger *log.Logger
InfoFetcher CardInfoFetcher
Cache CardCache
}
type UserMessage struct {
@ -25,18 +27,19 @@ type UserMessage struct {
}
type CardCache interface {
Get(cardName string) (string, error)
Set(cardName string, message string)
Get(cardName string) ([]cardsinfo.ScgCardPrice, error)
Set(cardName string, prices []cardsinfo.ScgCardPrice)
}
type CardInfoFetcher interface {
GetFormattedCardPrices(name string) (string, error)
GetNameByCardId(set string, number string) string
GetOriginalName(name string) string
GetPrices(name string) ([]cardsinfo.ScgCardPrice, error)
}
type Sender interface {
Send(userId int64, message string)
SendPrices(userId int64, cardName string, prices []cardsinfo.ScgCardPrice)
}
func (s *Scenario) HandleSearch(msg *UserMessage) {
@ -48,29 +51,22 @@ func (s *Scenario) HandleSearch(msg *UserMessage) {
s.Sender.Send(msg.UserId, cardNotFoundMessage)
s.Logger.Printf("[info] Could not find card. User input: %s", msg.Body)
} else {
message, err := s.getMessage(cardName)
prices, err := s.Cache.Get(cardName)
if err == nil {
s.Sender.SendPrices(msg.UserId, cardName, prices)
return
}
prices, err = s.InfoFetcher.GetPrices(cardName)
if err != nil {
s.Sender.Send(msg.UserId, pricesUnavailableMessage)
s.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
return
}
s.Sender.Send(msg.UserId, message)
s.Cache.Set(cardName, prices)
s.Sender.SendPrices(msg.UserId, cardName, prices)
}
}
func (s *Scenario) getMessage(cardName string) (string, error) {
val, err := s.Cache.Get(cardName)
if err != nil {
message, err := s.InfoFetcher.GetFormattedCardPrices(cardName)
if err != nil {
return "", err
}
s.Cache.Set(cardName, message)
return message, nil
}
return val, nil
}
func (s *Scenario) getCardNameByCommand(command string) (string, error) {
var name string
switch {

View file

@ -10,8 +10,8 @@ import (
func TestScenario_HandleSearch_BadCommand(t *testing.T) {
testCtx := GetTestScenarioCtx()
testCtx.Scenario.HandleSearch(&UserMessage{
Body: "!s",
UserId: 1,
Body: "!s",
UserId: 1,
})
assert.Equal(t, []testMessage{
{
@ -25,8 +25,8 @@ func TestScenario_HandleSearch_BadCommand(t *testing.T) {
func TestScenario_HandleSearch_GoodCommand(t *testing.T) {
testCtx := GetTestScenarioCtx()
testCtx.Scenario.HandleSearch(&UserMessage{
Body: "!s grn 228",
UserId: 1,
Body: "!s grn 228",
UserId: 1,
})
assert.Equal(t, []testMessage{
{
@ -39,8 +39,8 @@ func TestScenario_HandleSearch_GoodCommand(t *testing.T) {
func TestScenario_HandleSearch_NotFoundCard(t *testing.T) {
testCtx := GetTestScenarioCtx()
testCtx.Scenario.HandleSearch(&UserMessage{
Body: "absolutely_random_card",
UserId: 1,
Body: "absolutely_random_card",
UserId: 1,
})
assert.Equal(t, []testMessage{
{
@ -54,8 +54,8 @@ func TestScenario_HandleSearch_NotFoundCard(t *testing.T) {
func TestScenario_HandleSearch_BadCard(t *testing.T) {
testCtx := GetTestScenarioCtx()
testCtx.Scenario.HandleSearch(&UserMessage{
Body: "bad",
UserId: 1,
Body: "bad",
UserId: 1,
})
assert.Equal(t, []testMessage{
{
@ -68,8 +68,8 @@ func TestScenario_HandleSearch_BadCard(t *testing.T) {
func TestScenario_HandleSearch_Uncached(t *testing.T) {
testCtx := GetTestScenarioCtx()
testCtx.Scenario.HandleSearch(&UserMessage{
Body: "uncached",
UserId: 1,
Body: "uncached",
UserId: 1,
})
assert.Equal(t, []testMessage{
{
@ -77,6 +77,6 @@ func TestScenario_HandleSearch_Uncached(t *testing.T) {
message: "uncached",
},
}, testCtx.Sender.sent)
msg, _ := testCtx.Scenario.Cache.Get("uncached")
assert.Equal(t, "uncached", msg)
_, err := testCtx.Scenario.Cache.Get("uncached")
assert.Nil(t, err)
}

View file

@ -1,19 +1,23 @@
package scenario
import "errors"
import (
"errors"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
type testCache struct {
table map[string]string
table map[string][]cardsinfo.ScgCardPrice
}
func (t *testCache) Get(cardName string) (string, error) {
func (t *testCache) Get(cardName string) ([]cardsinfo.ScgCardPrice, error) {
msg, ok := t.table[cardName]
if !ok {
return "", errors.New("test")
return nil, errors.New("test")
}
return msg, nil
}
func (t *testCache) Set(cardName string, message string) {
t.table[cardName] = message
func (t *testCache) Set(cardName string, prices []cardsinfo.ScgCardPrice) {
t.table[cardName] = prices
}

View file

@ -3,6 +3,8 @@ package scenario
import (
"bytes"
"log"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
type TestScenarioCtx struct {
@ -21,8 +23,14 @@ func GetTestScenarioCtx() TestScenarioCtx {
Logger: log.New(buf, "", 0),
InfoFetcher: &testInfoFetcher{},
Cache: &testCache{
table: map[string]string{
"good": "good",
table: map[string][]cardsinfo.ScgCardPrice{
"good": {
{
Price: "1",
Edition: "alpha",
Link: "scg",
},
},
},
},
},

View file

@ -2,15 +2,17 @@ package scenario
import (
"errors"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
type testInfoFetcher struct{}
func (t *testInfoFetcher) GetFormattedCardPrices(name string) (string, error) {
func (t *testInfoFetcher) GetPrices(name string) ([]cardsinfo.ScgCardPrice, error) {
if name == "good" || name == "uncached" {
return name, nil
return nil, nil
}
return "", errors.New("test")
return nil, errors.New("test")
}
func (t *testInfoFetcher) GetNameByCardId(_ string, _ string) string {

View file

@ -1,9 +1,18 @@
package scenario
import "gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
type testSender struct {
sent []testMessage
}
func (s *testSender) SendPrices(userId int64, cardName string, prices []cardsinfo.ScgCardPrice) {
s.sent = append(s.sent, testMessage{
userId: userId,
message: cardName,
})
}
type testMessage struct {
userId int64
message string