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

@ -1,16 +1,38 @@
package telegram
import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
type Sender struct {
API *tgbotapi.BotAPI
API *tgbotapi.BotAPI
}
func (s *Sender) SendPrices(userId int64, cardName string, prices []cardsinfo.ScgCardPrice) {
msg := formatCardPrices(cardName, prices)
s.Send(userId, msg)
}
func (h *Sender) Send(userId int64, message string) {
msg := tgbotapi.NewMessage(userId, message)
msg.DisableWebPagePreview = true
h.API.Send(msg)
msg := tgbotapi.NewMessage(userId, message)
msg.DisableWebPagePreview = true
h.API.Send(msg)
}
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): %v\n", s.Edition, s.Link, s.Price)
}

View file

@ -0,0 +1,25 @@
package telegram
import (
"testing"
"github.com/stretchr/testify/assert"
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
)
func Test_formatCardPrices(t *testing.T) {
prices := []cardsinfo.ScgCardPrice{
{
Price: "1",
Edition: "Alpha",
Link: "scg1",
},
{
Price: "2",
Edition: "Beta",
Link: "scg2",
},
}
result := formatCardPrices("card", prices)
assert.Equal(t, "Оригинальное название: card\n\n1. [Alpha](scg1): 1\n2. [Beta](scg2): 2\n", result)
}