From 0b1e771e9139213e4136652e8fc19fcc83e18508 Mon Sep 17 00:00:00 2001 From: SolidlSnake Date: Wed, 19 Apr 2017 01:27:03 +0300 Subject: [PATCH] Wall.post --- README.md | 33 +++++++++++++++++++ easyvk/vk.go | 2 ++ easyvk/wall-response.go | 7 ++++ easyvk/wall.go | 71 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 easyvk/wall-response.go create mode 100644 easyvk/wall.go diff --git a/README.md b/README.md index 923f2dd..c172189 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,37 @@ Set user status: userID := 1 ok, err := vk.Status.Set("New status", userID) ``` +Post photo on wall: +```go +// your id or +// id of group +id := 0 + +server, err := vk.Photos.GetWallUploadServer(id) +if err != nil { + log.Fatal(err) +} + +// path to the image +path := "./example/x.png" +uploaded, err := vk.Upload.PhotoWall(server.Response.UploadURL, path) +if err != nil { + log.Fatal(err) +} + +saved, err := vk.Photos.SaveWallPhoto(0, id, uploaded.Photo, uploaded.Hash, "", uploaded.Server, 0, 0) +if err != nil { + log.Fatal(err) +} + +text := "Caption for the post" + +// -id if you post to group wall +postID, err := vk.Wall.Post(id, false, true, false, false, false, text, saved.Response[0].ID, "", "", 0, 0, 0, 0, 0) +if err != nil { + log.Fatal(err) +} +``` ### If you need to call method that not done yet: ```go @@ -58,5 +89,7 @@ if err != nil { * [Status](https://vk.com/dev/status) * [Get](https://vk.com/dev/status.get) * [Set](https://vk.com/dev/status.set) +* [Wall](https://vk.com/dev/wall) + * [Post](https://vk.com/dev/wall.post) * Upload * PhotoWall \ No newline at end of file diff --git a/easyvk/vk.go b/easyvk/vk.go index a251604..49578e9 100644 --- a/easyvk/vk.go +++ b/easyvk/vk.go @@ -21,6 +21,7 @@ type VK struct { Photos Photos Status Status Upload Upload + Wall Wall } // WithToken helps to initialize your @@ -33,6 +34,7 @@ func WithToken(token string) VK { vk.Photos = Photos{&vk } vk.Status = Status{&vk } vk.Upload = Upload{} + vk.Wall = Wall{&vk } return vk } diff --git a/easyvk/wall-response.go b/easyvk/wall-response.go new file mode 100644 index 0000000..674f300 --- /dev/null +++ b/easyvk/wall-response.go @@ -0,0 +1,7 @@ +package easyvk + +type wallPost struct { + Response struct { + PostID int `json:"post_id"` + } `json:"response"` +} diff --git a/easyvk/wall.go b/easyvk/wall.go new file mode 100644 index 0000000..0b7cf4a --- /dev/null +++ b/easyvk/wall.go @@ -0,0 +1,71 @@ +package easyvk + +import ( + "fmt" + "encoding/json" +) + +// A Wall describes a set of methods +// to work with wall. +type Wall struct { + vk *VK +} + +// Post adds a new post on a user wall or community wall. +// Can also be used to publish suggested or scheduled posts. +// Returns id of post. +func (w *Wall) Post(ownerID int, + friendsOnly, fromGroup, signed, markAsAds, adsPromotedStealth bool, + message, attachments, services, guid string, + publishDate, placeID, postID uint, + lat, long float64) (int, error) { + + params := map[string]string{ + "owner_id": fmt.Sprint(ownerID), + "message": message, + "attachments": attachments, + "services": services, + "guid": guid, + "publish_date": fmt.Sprint(publishDate), + "place_id": fmt.Sprint(placeID), + "post_id": fmt.Sprint(postID), + "lat": fmt.Sprint(lat), + "long": fmt.Sprint(long), + } + if friendsOnly { + params["friends_only"] = "1" + } else { + params["friends_only"] = "0" + } + if fromGroup { + params["from_group"] = "1" + } else { + params["from_group"] = "0" + } + if signed { + params["signed"] = "1" + } else { + params["signed"] = "0" + } + if markAsAds { + params["mark_as_ads"] = "1" + } else { + params["mark_as_ads"] = "0" + } + if adsPromotedStealth { + params["ads_promoted_stealth"] = "1" + } else { + params["ads_promoted_stealth"] = "0" + } + + resp, err := w.vk.Request("wall.post", params) + if err != nil { + return 0, nil + } + var info wallPost + err = json.Unmarshal(resp, &info) + if err != nil { + return 0, nil + } + return info.Response.PostID, nil +}