package market import ( "bytes" "fmt" "image" "io" "log" "math/rand" "net/http" "time" "git.niplace.ru/XoxJlopeZi4BB/imageutils" tg "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) func shuffle[S ~[]E, E any](a S) { rand.Seed(time.Now().UnixNano()) for i := len(a) - 1; i > 0; i-- { j := rand.Intn(i + 1) a[i], a[j] = a[j], a[i] } } func takeOne[S ~[]T, T any](s S) T { v := s[0] s = s[1:] return v } var ShuffledCardPool []int = func(n int) []int { arr := make([]int, n, n) for i := 0; i < n; i++ { arr[i] = i + 1 } shuffle(arr) return arr }(116) var RupeesOnCards []int = func() (ns []int) { rand.Seed(time.Now().UnixNano()) for i := 0; i < 2; i++ { ns = append(ns, rand.Intn(4)) } return }() var LinkFormat string = "https://rally-the-troops.com/" + "pax-pamir/cards/card_%d.jpg" type Button struct { Text string `json:"text"` Data string `json:"data"` } type ListEntry struct { Images []string Buttons []Button } func Buttons(b []Button) tg.InlineKeyboardMarkup { return tg.NewInlineKeyboardMarkup( func() []tg.InlineKeyboardButton { var row []tg.InlineKeyboardButton for _, Button := range b { row = append(row, tg.NewInlineKeyboardButtonData( Button.Text, Button.Data, ), ) } return row }(), ) } func HandleCommand(update tg.Update, bot *tg.BotAPI) { maxCost := 5 for i := 0; i <= maxCost; i++ { var ( msg tg.PhotoConfig buf *bytes.Buffer img image.Image ) for j := 0; j < 2; j++ { cardNum := ShuffledCardPool[0] ShuffledCardPool = ShuffledCardPool[1:] resp, err := http.Get(fmt.Sprintf(LinkFormat, cardNum)) if err != nil { log.Println("http GET error:", err) } defer resp.Body.Close() io.Copy(buf, resp.Body) } msg = tg.NewPhoto( update.Message.Chat.ID, tg.FileBytes(buf.Bytes()), ) buttons := []Button{} for j := 0; j < 2; j++ { text := func() (s string) { s = fmt.Sprintf("buy for %d", i) if RupeesOnCard[j] >= i { s += fmt.Sprintf(", get 1") } return }() buttons := append(buttons, Button{Text: text}) } _, err := bot.Send(msg) // get response message if err != nil { log.Println("failed to send message on command:", err) } log.Print("message sent. id:", responseMsg.MessageID) } } func HandleUpdate(update tg.Update, bot *tg.BotAPI) { switch { case update.Message != nil: log.Print("new message. id:", update.Message.MessageID) if update.Message.IsCommand() { HandleCommand(update, bot) } else { log.Println("message is not a command:\n", update.Message.Text) } case update.CallbackQuery != nil: ConsumeCallback(update, bot) } return } func ConsumeCallback(update tg.Update, bot *tg.BotAPI) { updateMsg := update.CallbackQuery.Message log.Print("new CallbackQuery. id:", updateMsg.MessageID) callback := tg.NewCallback( update.CallbackQuery.ID, update.CallbackQuery.Data) // Recieve callback req, err := bot.Request(callback) if err != nil { log.Println("failed on request:", err) } log.Print("request sent. response:", req.Description) }