vklp

package module
v0.0.0-...-a9d0de9 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2017 License: Unlicense Imports: 7 Imported by: 0

README

vklp

Low-level Go (Golang) vk.com longpoll client

WARNING - work in progress!

Example

package main

import (
	"fmt"

	"github.com/mxmCherry/vkapi"
	"github.com/mxmCherry/vklp"
)

func main() {

	// get longpoll connection details first:

	api := vkapi.New(vkapi.Options{
		AccessToken: "YOUR_ACCESS_TOKEN",
		Version:     "5.62",
	})

	lpReq := struct {
		UseSSL  bool `json:"use_ssl,omitempty"`
		NeedPTS bool `json:"need_pts,omitempty"`
	}{
		UseSSL: true,
	}

	lpRes := new(struct {
		Server string `json:"server"`
		Key    string `json:"key"`
		TS     int64  `json:"ts"`
	})

	err := api.Exec("messages.getLongPollServer", vkapi.ToParams(lpReq), lpRes)
	if err != nil {
		panic(err.Error())
	}

	// consume updates with longpoll:

	// instantiate longpoll client:
	lp, err := vklp.New(vklp.Options{
		Server:  lpRes.Server,
		Key:     lpRes.Key,
		TS:      lpRes.TS,
		Mode:    vklp.ModeAttachments | vklp.ModeExtended | vklp.ModePTS | vklp.ModeExtra | vklp.ModeRandomID,
		Version: "1",
	})
	if err != nil {
		panic(err.Error())
	}
	defer lp.Stop()

	// process events:
	for {

		// load next event:
		if err = lp.Next(); err != nil {
			panic(err.Error())
		}

		// decode event type:
		var t uint8
		if err := lp.Decode(&t); err != nil {
			panic(err.Error())
		}

		// decode events by their types:
		switch t {

		case 4: // received new message:
			var (
				messageID uint64
				peerID    int64
				subject   string
				text      string
			)
			// [$message_id, $flags, $peer_id, $timestamp, $subject, $text, $attachments, $random_id]
			if err = lp.Decode(&messageID, vklp.Skip, &peerID, vklp.Skip, &subject, &text); err != nil {
				panic(err.Error())
			}
			fmt.Printf("received new message %d from %d: %s - %s\n", messageID, peerID, subject, text)

		default: // drop unused events:
			fmt.Printf("dropped unused event %d\n", t)
		}

	} // end for
} // end main

Documentation

Index

Constants

View Source
const (
	ModeAttachments = 2
	ModeExtended    = 8
	ModePTS         = 32
	ModeExtra       = 64
	ModeRandomID    = 128
)

Variables

View Source
var Skip struct{}

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func From

func From(httpClient HTTPClient, options Options) (*Client, error)

func New

func New(options Options) (*Client, error)

func (*Client) Decode

func (c *Client) Decode(vs ...interface{}) error

func (*Client) Next

func (c *Client) Next() error

func (*Client) Stop

func (c *Client) Stop() error

type Error

type Error uint8

func (Error) Error

func (e Error) Error() string

type HTTPClient

type HTTPClient interface {
	Do(*http.Request) (*http.Response, error)
}

type Options

type Options struct {
	Server  string
	Key     string
	TS      int64
	Wait    int64
	Mode    uint64
	Version string
}

Jump to

Keyboard shortcuts

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