Added !s command and fixed links bug

This commit is contained in:
Artyom Belousov 2019-05-11 23:39:26 +03:00
parent 27b1385a92
commit 3ee53b7ad1
5 changed files with 70 additions and 12 deletions

View file

@ -1,9 +1,11 @@
package vk
import (
"errors"
"github.com/flygrounder/mtg-price-vk/cardsinfo"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
const CARDSLIMIT = 8
@ -18,16 +20,23 @@ func min(a, b int) int {
func HandleMessage(c *gin.Context) {
var req MessageRequest
c.BindJSON(&req)
if (req.Type == "confirmation") && (req.GroupId == GROUPID) {
c.String(http.StatusOK, CONFIRMATION_STRING)
return
}
defer c.String(http.StatusOK, "ok")
if req.Secret != SECRET_KEY {
return
}
cardName := cardsinfo.GetOriginalName(req.Object.Body)
if cardName == "" {
switch req.Type {
case "confirmation":
handleConfirmation(c, &req)
case "message_new":
handleSearch(c, &req)
}
}
func handleSearch(c *gin.Context, req *MessageRequest) {
defer c.String(http.StatusOK, "ok")
cardName, err := getCardNameByCommand(req.Object.Body)
if err != nil {
Message(req.Object.UserId, "Некорректная команда")
} else if cardName == "" {
Message(req.Object.UserId, "Карта не найдена")
} else {
prices, _ := cardsinfo.GetSCGPrices(cardName)
@ -37,3 +46,26 @@ func HandleMessage(c *gin.Context) {
Message(req.Object.UserId, priceInfo)
}
}
func getCardNameByCommand(command string) (string, error) {
var name string
switch {
case strings.HasPrefix(command, "!s"):
split := strings.Split(command, " ")
if len(split) < 3 {
return "", errors.New("wrong command")
}
set := split[1]
number := split[2]
name = cardsinfo.GetNameByCardId(set, number)
default:
name = cardsinfo.GetOriginalName(command)
}
return name, nil
}
func handleConfirmation(c *gin.Context, req *MessageRequest) {
if (req.Type == "confirmation") && (req.GroupId == GROUPID) {
c.String(http.StatusOK, CONFIRMATION_STRING)
}
}