Changed log printing on error only

This commit is contained in:
Artyom Belousov 2019-11-04 19:42:23 +03:00
parent ac68bc64b4
commit 705a135cac
2 changed files with 16 additions and 3 deletions

View file

@ -1,6 +1,7 @@
package vk package vk
import ( import (
"encoding/json"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand" "math/rand"
@ -24,10 +25,13 @@ func Message(userId int64, message string) {
paramString := strings.Join(params, "&") paramString := strings.Join(params, "&")
resp, err := http.Get(SendMessageUrl + "?" + paramString) resp, err := http.Get(SendMessageUrl + "?" + paramString)
if err != nil || resp.StatusCode != http.StatusOK { 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 return
} }
responseBytes, _ := ioutil.ReadAll(resp.Body) responseBytes, _ := ioutil.ReadAll(resp.Body)
log.Printf("Message sent\n user: %d\n message: %s\n server response: %s", userId, message, var response SendMessageResponse
string(responseBytes)) _ = 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)
}
} }

View file

@ -11,3 +11,12 @@ type UserMessage struct {
Body string `json:"text"` Body string `json:"text"`
UserId int64 `json:"peer_id"` 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"`
}