GithubHelp home page GithubHelp logo

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
}

go-vk-api's Projects

vk icon vk

Go library for accessing the VK API

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.