From 705a135cacf2a6336a143d60221c526dbd492888 Mon Sep 17 00:00:00 2001 From: Artyom Belousov Date: Mon, 4 Nov 2019 19:42:23 +0300 Subject: [PATCH] Changed log printing on error only --- vk/message.go | 10 +++++++--- vk/structs.go | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/vk/message.go b/vk/message.go index 15d2771..50b71d8 100644 --- a/vk/message.go +++ b/vk/message.go @@ -1,6 +1,7 @@ package vk import ( + "encoding/json" "io/ioutil" "log" "math/rand" @@ -24,10 +25,13 @@ func Message(userId int64, message string) { paramString := strings.Join(params, "&") resp, err := http.Get(SendMessageUrl + "?" + paramString) if err != nil || resp.StatusCode != http.StatusOK { - log.Print("Could not send message\n user: %lld", userId) + log.Printf("Could not send message\n user: %d", userId) return } responseBytes, _ := ioutil.ReadAll(resp.Body) - log.Printf("Message sent\n user: %d\n message: %s\n server response: %s", userId, message, - string(responseBytes)) + var response SendMessageResponse + _ = json.Unmarshal(responseBytes, &response) + if response.Error.ErrorCode != 0 { + log.Printf("Message was not sent message\n user: %d\n error message: %s", userId, response.Error.ErrorMsg) + } } diff --git a/vk/structs.go b/vk/structs.go index b48c8ef..9a7c3e6 100644 --- a/vk/structs.go +++ b/vk/structs.go @@ -11,3 +11,12 @@ type UserMessage struct { Body string `json:"text"` UserId int64 `json:"peer_id"` } + +type SendMessageResponse struct { + Error ErrorResponse `json:"error"` +} + +type ErrorResponse struct { + ErrorCode int `json:"error_code"` + ErrorMsg string `json:"error_msg"` +}