Code Monkey home page Code Monkey logo

easyvk-go's Introduction

EasyVK GoDoc

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

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("[email protected]", "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:

easyvk-go's People

Contributors

blukai avatar lanzay avatar vorkytaka avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.