vk

package module
v0.0.0-...-014d9b8 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2020 License: MIT Imports: 7 Imported by: 8

README

go-vk-api

GoDoc Go Report Card

Golang bindings for the VK API

Install

Install the package with:

go get github.com/go-vk-api/vk

Import it with:

import "github.com/go-vk-api/vk"

and use vk as the package name inside the code.

Example

Full example with errors handling

package main

import (
	"log"
	"net/http"
	"os"

	"github.com/go-vk-api/vk"
	lp "github.com/go-vk-api/vk/longpoll/user"
)

func main() {
	client, _ := vk.NewClientWithOptions(
		vk.WithToken(os.Getenv("VK_ACCESS_TOKEN")),
	)

	_ = printMe(client)

	longpoll, _ := lp.NewWithOptions(client, lp.WithMode(lp.ReceiveAttachments))

	stream, _ := longpoll.GetUpdatesStream(0)

	for update := range stream.Updates {
		switch data := update.Data.(type) {
		case *lp.NewMessage:
			if data.Text == "/hello" {
				_ = client.CallMethod("messages.send", vk.RequestParams{
					"peer_id":          data.PeerID,
					"message":          "Hello!",
					"forward_messages": data.ID,
					"random_id":        0,
				}, nil)
			}
		}
	}
}

func printMe(api *vk.Client) error {
	var users []struct {
		ID        int64  `json:"id"`
		FirstName string `json:"first_name"`
		LastName  string `json:"last_name"`
	}

	_ = api.CallMethod("users.get", vk.RequestParams{}, &users)

	me := users[0]

	log.Println(me.ID, me.FirstName, me.LastName)

	return nil
}

Documentation

Index

Constants

View Source
const (
	DefaultBaseURL = "https://api.vk.com/method"
	DefaultLang    = "en"
	DefaultVersion = "5.103"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	BaseURL string
	Lang    string
	Version string

	Token string

	HTTPClient httputil.RequestDoer
}

Client manages communication with VK API.

func NewClient

func NewClient() (*Client, error)

NewClient initializes a new VK API client with default values.

func NewClientWithOptions

func NewClientWithOptions(options ...Option) (*Client, error)

NewClientWithOptions initializes a new VK API client with default values. It takes functors to modify values when creating it, like `NewClientWithOptions(WithToken(…))`.

func (*Client) CallMethod

func (c *Client) CallMethod(method string, params RequestParams, response interface{}) error

CallMethod invokes the named method and stores the result in the value pointed to by response.

type MethodError

type MethodError struct {
	Code          int64  `json:"error_code"`
	Message       string `json:"error_msg"`
	RequestParams []struct {
		Key   string `json:"key"`
		Value string `json:"value"`
	} `json:"request_params"`
}

MethodError represents a VK API method call error.

func (*MethodError) Error

func (err *MethodError) Error() string

type Option

type Option func(*Client) error

Option is a configuration option to initialize a client.

func WithHTTPClient

func WithHTTPClient(doer httputil.RequestDoer) Option

WithHTTPClient overrides the client http client with the specified one.

func WithToken

func WithToken(token string) Option

WithToken overrides the client token with the specified one.

type RequestParams

type RequestParams map[string]interface{}

RequestParams are the params for invoking methods.

func (RequestParams) URLValues

func (params RequestParams) URLValues() (url.Values, error)

URLValues translates the params to url.Values.

Directories

Path Synopsis
longpoll

Jump to

Keyboard shortcuts

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