easyvk-go

module
v0.0.0-...-1edff12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2020 License: Apache-2.0

README

EasyVK GoDoc

Package EasyVK provides you simple way to work with VK API.

Logo

Installation:
Install:
$ go get -u github.com/vorkytaka/easyvk-go/easyvk
Import:
import "github.com/vorkytaka/easyvk-go/easyvk"
How to work:

Initialize your VK object with your access token.

vk := easyvk.WithToken("token")

Or you can log in by email and password.

// put your email, password, client id and scope
// scope must be a string like "friends,wall"
vk, err := easyvk.WithAuth("my@beautiful.mail", "pa$$word", "9182736", "friends,wall,photos")
if err != nil {
	// doesn't log in
}

Now you can call method of VK API with your vk variable.

Examples:

Get user profile info:

info, err := vk.Account.GetProfileInfo()

Set user status:

// If you want to update status on your page
// then set ID to 0
userID := 0
ok, err := vk.Status.Set("New status", userID)

Post photo on wall:

id := 0

server, err := vk.Photos.GetWallUploadServer(uint(id))
if err != nil {
	log.Fatal(err)
}

// path to the image
path := "D:/x.png"
uploaded, err := vk.Upload.PhotoWall(server.UploadURL, path)
if err != nil {
	log.Fatal(err)
}

saved, err := vk.Photos.SaveWallPhoto(0, uint(id), uploaded.Photo, uploaded.Hash, "", uploaded.Server, 0, 0)
if err != nil {
	log.Fatal(err)
}

text := "Caption for the post"
photoID := "photo" + fmt.Sprint(saved[0].OwnerID) + "_" + fmt.Sprint(saved[0].ID)

params := easyvk.WallPostParams{}
params.OwnerID = id
params.Message = "Test"
params.Attachments = photoID

x, err := vk.Wall.Post(params)
if err != nil {
	log.Fatal(err)
}
fmt.Println(x)
If you need to call method that not done yet:
methodName := "account.banUser"
params := map[string]string{
        "user_id": "1",
}
byteArray, err := vk.Request(methodName, params)
if err != nil {
        
}
// Now work with byteArray.
// Parse it to struct or to interface.
Naming conventions:
API section:

Every API section must be a structure and have name as it called in API and starts with a capital letter.

For example: Account section must be type Account struct {}

API methods:

Every API method must be a method that have a received type of his section. It must have name as it called in API and starts with a capital letter.

For example: Account.banUser method must be func (a *Account) BanUser() {}

Parameters:

If method requests 4 or less parameters, then they must be just a parameters.

For example: func (a *Account) BanUser(userID uint) {}

If method requests 5 or more parameters, then he must get it with a structure. That structure must naming like [SectionName][MethodName]Params.

For example: type WallPostParams struct {}

Responses:

If method return only one field, then you must just return that field.

For example: Board.addTopic return ID of the created topic, so we just return integer.

If method return only 1 if succeeded, then we need to return boolean.

For example: Board.closeTopic return 1 is succeded, so we check if it's 1 and return true, and false otherwise.

If method return object with 2 or more fields, then we need to create a structure for that response. That structure must naming like [SectionName][MethodName]Response.

For example: type AccountGetCountersResponse struct {}

List of finished methods:

Directories

Path Synopsis
Package easyvk provides you simple way to work with VK API.
Package easyvk provides you simple way to work with VK API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL