Added different formatting for telegram bot
This commit is contained in:
parent
35f8fa5a57
commit
89623f5f6a
18 changed files with 233 additions and 173 deletions
22
internal/vk/format.go
Normal file
22
internal/vk/format.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package vk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||
)
|
||||
|
||||
func formatCardPrices(name string, prices []cardsinfo.ScgCardPrice) string {
|
||||
message := fmt.Sprintf("Оригинальное название: %v\n\n", name)
|
||||
for i, v := range prices {
|
||||
message += fmt.Sprintf("%v. %v", i+1, formatPrice(v))
|
||||
}
|
||||
if len(prices) == 0 {
|
||||
message += "Цен не найдено\n"
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
func formatPrice(s cardsinfo.ScgCardPrice) string {
|
||||
return fmt.Sprintf("%v: %v\n%v\n", s.Edition, s.Price, s.Link)
|
||||
}
|
||||
19
internal/vk/format_test.go
Normal file
19
internal/vk/format_test.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package vk
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||
)
|
||||
|
||||
func TestFormatCardPrices(t *testing.T) {
|
||||
formatted := formatCardPrices("card", []cardsinfo.ScgCardPrice{
|
||||
{
|
||||
Price: "1.5$",
|
||||
Edition: "ED",
|
||||
Link: "scg.com",
|
||||
},
|
||||
})
|
||||
assert.Equal(t, "Оригинальное название: card\n\n1. ED: 1.5$\nscg.com\n", formatted)
|
||||
}
|
||||
|
|
@ -9,6 +9,8 @@ import (
|
|||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||
)
|
||||
|
||||
const sendMessageUrl = "https://api.vk.com/method/messages.send"
|
||||
|
|
@ -22,6 +24,11 @@ type ApiSender struct {
|
|||
Logger *log.Logger
|
||||
}
|
||||
|
||||
func (s *ApiSender) SendPrices(userId int64, cardName string, prices []cardsinfo.ScgCardPrice) {
|
||||
msg := formatCardPrices(cardName, prices)
|
||||
s.Send(userId, msg)
|
||||
}
|
||||
|
||||
type sendMessageResponse struct {
|
||||
Error errorResponse `json:"error"`
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue