Added VK part

This commit is contained in:
Artyom Belousov 2019-05-11 14:01:40 +03:00
parent 69d6808cd0
commit 657f967a49
5 changed files with 65 additions and 0 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
mtg-price-vk mtg-price-vk
*/.swp */.swp
vk/token.go

11
main.go
View file

@ -1,4 +1,15 @@
package main package main
import (
"github.com/flygrounder/mtg-price-vk/vk"
"github.com/gin-gonic/gin"
"math/rand"
"time"
)
func main() { func main() {
rand.Seed(time.Now().UTC().UnixNano())
r := gin.Default()
r.POST("callback/message", vk.HandleMessage)
r.Run()
} }

18
vk/handlers.go Normal file
View file

@ -0,0 +1,18 @@
package vk
import (
"github.com/flygrounder/mtg-price-vk/cardsinfo"
"github.com/gin-gonic/gin"
"net/http"
)
func HandleMessage(c *gin.Context) {
defer c.String(http.StatusOK, "ok")
var req MessageRequest
c.BindJSON(&req)
if req.Secret != SECRET_KEY {
return
}
cardName := cardsinfo.GetOriginalName(req.Object.Body)
Message(req.Object.UserId, cardName)
}

24
vk/message.go Normal file
View file

@ -0,0 +1,24 @@
package vk
import (
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
)
const VKURL = "https://api.vk.com/method/messages.send"
func Message(userId int64, message string) {
randomId := rand.Int31()
params := []string{
"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)
}

11
vk/structs.go Normal file
View file

@ -0,0 +1,11 @@
package vk
type MessageRequest struct {
Object UserMessage `json:"object"`
Secret string `json:"secret"`
}
type UserMessage struct {
Body string `json:"body"`
UserId int64 `json:"user_id"`
}