Refactored code and added logging

This commit is contained in:
Artyom Belousov 2019-11-03 12:16:45 +03:00
parent 8830920900
commit 221ab214cc
11 changed files with 51 additions and 44 deletions

View file

@ -12,7 +12,7 @@ import (
"github.com/gin-gonic/gin"
)
const CARDSLIMIT = 8
const Cardslimit = 8
func min(a, b int) int {
if a < b {
@ -23,20 +23,20 @@ func min(a, b int) int {
func HandleMessage(c *gin.Context) {
var req MessageRequest
c.BindJSON(&req)
if req.Secret != SECRET_KEY {
_ = c.BindJSON(&req)
if req.Secret != SecretKey {
return
}
switch req.Type {
case "confirmation":
handleConfirmation(c, &req)
case "message_new":
go handleSearch(c, &req)
go handleSearch(&req)
c.String(http.StatusOK, "ok")
}
}
func handleSearch(c *gin.Context, req *MessageRequest) {
func handleSearch(req *MessageRequest) {
cardName, err := getCardNameByCommand(req.Object.Body)
if err != nil {
Message(req.Object.UserId, "Некорректная команда")
@ -51,7 +51,7 @@ func handleSearch(c *gin.Context, req *MessageRequest) {
log.Printf("Could not find SCG prices\n error message: %s\n card name: %s", err.Error(), cardName)
return
}
elements := min(CARDSLIMIT, len(prices))
elements := min(Cardslimit, len(prices))
prices = prices[:elements]
priceInfo := cardsinfo.FormatCardPrices(cardName, prices)
Message(req.Object.UserId, priceInfo)
@ -74,7 +74,7 @@ func GetPrices(cardName string) ([]cardsinfo.CardPrice, error) {
client.Set(cardName, string(serialized))
return prices, nil
}
json.Unmarshal([]byte(val), &prices)
_ = json.Unmarshal([]byte(val), &prices)
return prices, nil
}
@ -96,7 +96,7 @@ func getCardNameByCommand(command string) (string, error) {
}
func handleConfirmation(c *gin.Context, req *MessageRequest) {
if (req.Type == "confirmation") && (req.GroupId == GROUPID) {
c.String(http.StatusOK, CONFIRMATION_STRING)
if (req.Type == "confirmation") && (req.GroupId == GroupId) {
c.String(http.StatusOK, ConfirmationString)
}
}

View file

@ -1,6 +1,7 @@
package vk
import (
"log"
"math/rand"
"net/http"
"net/url"
@ -8,17 +9,20 @@ import (
"strings"
)
const VKURL = "https://api.vk.com/method/messages.send"
const SendMessageUrl = "https://api.vk.com/method/messages.send"
func Message(userId int64, message string) {
randomId := rand.Int31()
params := []string{
"access_token=" + TOKEN,
"access_token=" + Token,
"peer_id=" + strconv.FormatInt(userId, 10),
"message=" + url.QueryEscape(message),
"v=5.95",
"random_id=" + strconv.FormatInt(int64(randomId), 10),
}
paramString := strings.Join(params, "&")
http.Get(VKURL + "?" + paramString)
resp, err := http.Get(SendMessageUrl + "?" + paramString)
if err != nil || resp.StatusCode != http.StatusOK {
log.Print("Could not send message\n user: %lld", userId)
}
}

View file

@ -5,7 +5,7 @@ import (
"strconv"
)
var TOKEN = os.Getenv("VK_TOKEN")
var SECRET_KEY = os.Getenv("VK_SECRET_KEY")
var GROUPID, _ = strconv.ParseInt(os.Getenv("VK_GROUP_ID"), 10, 64)
var CONFIRMATION_STRING = os.Getenv("VK_CONFIRMATION_STRING")
var Token = os.Getenv("VK_TOKEN")
var SecretKey = os.Getenv("VK_SECRET_KEY")
var GroupId, _ = strconv.ParseInt(os.Getenv("VK_GROUP_ID"), 10, 64)
var ConfirmationString = os.Getenv("VK_CONFIRMATION_STRING")