vkapi

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

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

Go to latest
Published: Sep 30, 2019 License: GPL-3.0 Imports: 16 Imported by: 0

README

VK API wrapper in golang

Installing

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

Authorizing using username and password

client, err := vkapi.NewVKClient(vkapi.DeviceIPhone, "username", "password")

Authorizing using access token

client, err := vkapi.NewVKClientWithToken("token", nil)

Listening longpoll events

// listening received messages
client.AddLongpollCallback("msgin", func(m *vkapi.LongPollMessage) {
	fmt.Printf("new message received from uid %d\n", m.UserID)
})

// listening deleted messages
client.AddLongpollCallback("msgdel", func(m *vkapi.LongPollMessage) {
	fmt.Printf("message %d was deleted\n", m.MessageID)
})

// listening sent messages
client.AddLongpollCallback("msgout", func(m *vkapi.LongPollMessage) {
	fmt.Printf("sent message to uid %d\n", m.UserID)
})

// listening read messages
client.AddLongpollCallback("msgread", func(m *vkapi.LongPollMessage) {
	fmt.Printf("message %d was read\n", m.MessageID)
})

// listening users online
client.AddLongpollCallback("msgonline", func(m *vkapi.LongPollMessage) {
	fmt.Printf("user %d is now online\n", m.UserID)

// starting 
client.ListenLongPollServer()

Documentation

Index

Constants

View Source
const (
	DeviceIPhone = iota
	DeviceWPhone
	DeviceAndroid
)
View Source
const (
	TypePost         = "post"
	TypeComment      = "comment"
	TypePhoto        = "photo"
	TypeDocument     = "doc"
	TypeAudio        = "audio"
	TypeVideo        = "video"
	TypeNote         = "note"
	TypePhotoComment = "photo_comment"
	TypeVideoComment = "video_comment"
	TypeTopicComment = "topic_comment"
	TypeSitepage     = "sitepage"
)
View Source
const (
	ActivityTypeTyping   = "typing"
	ActivityTypeAudioMsg = "audiomessage"
)
View Source
const (
	PlatformMobile
	PlatformIPhone
	PlatfromIPad
	PlatformAndroid
	PlatformWPhone
	PlatformWindows
	PlatformWeb
)

last seen device

Variables

This section is empty.

Functions

func ArrayToStr

func ArrayToStr(a []int) string

func GetFilesSizeMB

func GetFilesSizeMB(files []string) (int, error)

Types

type APIResponse

type APIResponse struct {
	Response      json.RawMessage `json:"response"`
	ResponseError Error           `json:"error"`
}

type AudioAttachment

type AudioAttachment struct {
	ID        int    `json:"id"`
	OwnerID   int    `json:"owner_id"`
	Artist    string `json:"artist"`
	Title     string `json:"title"`
	Duration  int    `json:"duration"`
	URL       string `json:"url"`
	Performer string `json:"performer"`
}

type Comment

type Comment struct {
	Count   int `json:"count"`
	CanPost int `json:"can_post"`
}

type Dialog

type Dialog struct {
	Count    int     `json:"count"`
	Messages []*Item `json:"items"`
}

type DialogMessage

type DialogMessage struct {
	MID               int                  `json:"id"`
	Date              int64                `json:"date"`
	Out               int                  `json:"out"`
	UID               int                  `json:"user_id"`
	ReadState         int                  `json:"read_state"`
	Title             string               `json:"title"`
	Body              string               `json:"body"`
	RandomID          int                  `json:"random_id"`
	ChatID            int64                `json:"chat_id"`
	ChatActive        string               `json:"chat_active"`
	PushSettings      *Push                `json:"push_settings"`
	UsersCount        int                  `json:"users_count"`
	AdminID           int                  `json:"admin_id"`
	Photo50           string               `json:"photo_50"`
	Photo100          string               `json:"photo_100"`
	Photo200          string               `json:"photo_200"`
	ForwardedMessages []*ForwardedMessage  `json:"fwd_messages"`
	Attachments       []*MessageAttachment `json:"attachments"`
}

type DocAttachment

type DocAttachment struct {
	ID         int    `json:"id"`
	OwnerID    int    `json:"owner_id"`
	Title      string `json:"title"`
	Size       int    `json:"size"`
	Extenstion string `json:"ext"`
	URL        string `json:"url"`
	Date       int64  `json:"date"`
	Type       int    `json:"type"`
	IsLicensed int    `json:"is_licensed"`
}

type Docs

type Docs struct {
	Count     int              `json:"count"`
	Documents []*DocAttachment `json:"items"`
}

type Error

type Error struct {
	ErrorCode int    `json:"error_code"`
	ErrorMsg  string `json:"error_msg"`
}

type ForwardedMessage

type ForwardedMessage struct {
	UID               int                  `json:"user_id"`
	Date              int64                `json:"date"`
	Body              string               `json:"body"`
	Attachments       []*MessageAttachment `json:"attachments"`
	ForwardedMessages []*ForwardedMessage  `json:"fwd_messages"`
}

type Friends

type Friends struct {
	Count int     `json:"count"`
	Users []*User `json:"items"`
}

type FriendsRequests

type FriendsRequests struct {
	Count    int        `json:"count"`
	Requests []*Request `json:"items"`
}

type Group

type Group struct {
	ID                int             `json:"id"`
	Name              string          `json:"name"`
	ScreenName        string          `json:"screen_name"`
	Description       string          `json:"description"`
	Activity          string          `json:"activity"`
	Contacts          []*GroupContact `json:"contacts"`
	IsClosed          int             `json:"is_closed"`
	Type              string          `json:"type"`
	IsAdmin           int             `json:"is_admin"`
	IsMember          int             `json:"is_member"`
	HasPhoto          int             `json:"has_photo"`
	IsMessagesBlocked int             `json:"is_messages_blocked"`
	Photo50           string          `json:"photo_50"`
	Photo100          string          `json:"photo_100"`
	Photo200          string          `json:"photo_200"`
	AgeLimit          int             `json:"age_limits"`
	CanCreateTopic    int             `json:"can_create_topic"`
	CanMessage        int             `json:"can_message"`
	CanPost           int             `json:"can_post"`
	CanSeeAllPosts    int             `json:"can_see_all_posts"`
}

type GroupContact

type GroupContact struct {
	UID         int    `json:"user_id"`
	Description string `json:"desc"`
}

type GroupMembers

type GroupMembers struct {
	Count   int     `json:"count"`
	Members []*User `json:"items"`
}

type GroupSearchResult

type GroupSearchResult struct {
	Count  int      `json:"count"`
	Groups []*Group `json:"items"`
}

type HistoryAttachment

type HistoryAttachment struct {
	Attachments []HistoryAttachmentItem `json:"items"`
	NextFrom    string                  `json:"next_from"`
}

type HistoryAttachmentItem

type HistoryAttachmentItem struct {
	MID        int                `json:"message_id"`
	Attachment *MessageAttachment `json:"attachment"`
}

type Item

type Item struct {
	Message *DialogMessage `json:"message"`
	InRead  int            `json:"in_read"`
	OutRead int            `json:"out_read"`
}

type LastSeen

type LastSeen struct {
	Time     int64 `json:"time"`
	Platform int   `json:"platform"`
}

type Like

type Like struct {
	Count      int `json:"count"`
	UserLikes  int `json:"user_likes"`
	CanLike    int `json:"can_like"`
	CanPublish int `json:"can_publish"`
}

type LikeUser

type LikeUser struct {
	Type      string `json:"profile"`
	ID        int    `json:"id"`
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
}

type Likes

type Likes struct {
	Count int         `json:"count"`
	Users []*LikeUser `json:"items"`
}

type LinkAttachment

type LinkAttachment struct {
	URL         string `json:"url"`
	Title       string `json:"title"`
	Description string `json:"description"`
	Target      string `json:"target"`
}

type LongPollMessage

type LongPollMessage struct {
	MessageType  string
	MessageID    int
	MessageFlags int
	UserID       int64
	Date         int64
	Title        string
	Body         string
	Attachments  map[string]string
}

type LongPollServer

type LongPollServer struct {
	Key    string `json:"key"`
	Server string `json:"server"`
	TS     int64  `json:"ts"`
}

type LongPollUpdate

type LongPollUpdate struct {
	Failed  int             `json:"failed"`
	TS      int64           `json:"ts"`
	Updates [][]interface{} `json:"updates"`
}

type Message

type Message struct {
	Count    int              `json:"count"`
	Messages []*DialogMessage `json:"items"`
}

type MessageAttachment

type MessageAttachment struct {
	Type     string             `json:"type"`
	Audio    *AudioAttachment   `json:"audio"`
	Video    *VideoAttachment   `json:"video"`
	Photo    *PhotoAttachment   `json:"photo"`
	Document *DocAttachment     `json:"doc"`
	Link     *LinkAttachment    `json:"link"`
	Wall     *WallPost          `json:"wall"`
	Sticker  *StickerAttachment `json:"sticker"`
}

type Mutual

type Mutual struct {
	Count int   `json:"count"`
	Users []int `json:"users"`
}

type PhotoAttachment

type PhotoAttachment struct {
	ID        int    `json:"id"`
	AID       int    `json:"album_id"`
	OwnerID   int    `json:"owner_id"`
	Photo75   string `json:"photo_75"`
	Photo130  string `json:"photo_130"`
	Photo604  string `json:"photo_604"`
	Photo807  string `json:"photo_807"`
	Photo1280 string `json:"photo_1280"`
	Photo2560 string `json:"photo_2560"`
	Width     int    `json:"width"`
	Height    int    `json:"height"`
	Text      string `json:"text"`
	Created   int64  `json:"created"`
	AccessKey string `json:"access_key"`
}

type Push

type Push struct {
	Sound         int   `json:"sound"`
	DisabledUntil int64 `json:"disabled_until"`
}

type Repost

type Repost struct {
	Count        int `json:"count"`
	UserReposted int `json:"user_reposted"`
}

type Request

type Request struct {
	UserID        int     `json:"user_id"`
	MutualFriends *Mutual `json:"mutual"`
}

type ResolveScreenName

type ResolveScreenName struct {
	Type     string `json:"type"`
	ObjectID int    `json:"object_id"`
}

type Source

type Source struct {
	Type string `json:"type"`
}

type StickerAttachment

type StickerAttachment struct {
	ID        int    `json:"id"`
	ProductID int    `json:"product_id"`
	Photo64   string `json:"photo_64"`
	Photo128  string `json:"photo_128"`
	Photo256  string `json:"photo_256"`
	Photo352  string `json:"photo_352"`
	Photo512  string `json:"photo_512"`
	Width     int    `json:"width"`
	Height    int    `json:"height"`
}

type Token

type Token struct {
	AccessToken      string `json:"access_token"`
	ExpiresIn        int    `json:"expires_in"`
	UID              int    `json:"user_id"`
	Error            string `json:"error"`
	ErrorDescription string `json:"error_description"`
	FirstName        string
	LastName         string
	PicSmall         string
	PicMedium        string
	PicBig           string
}

type TokenOptions

type TokenOptions struct {
	ServiceToken    bool
	ValidateOnStart bool
}

type User

type User struct {
	UID                     int          `json:"id"`
	FirstName               string       `json:"first_name"`
	LastName                string       `json:"last_name"`
	Sex                     int          `json:"sex"`
	Nickname                string       `json:"nickname"`
	ScreenName              string       `json:"screen_name"`
	BDate                   string       `json:"bdate"`
	City                    *UserCity    `json:"city"`
	Country                 *UserCountry `json:"country"`
	Photo                   string       `json:"photo"`
	PhotoMedium             string       `json:"photo_medium"`
	PhotoBig                string       `json:"photo_big"`
	Photo_max_orig          string       `json:"photo_max_orig"`
	Photo_max               string       `json:"photo_max"`
	HasMobile               int          `json:"has_mobile"`
	Online                  int          `json:"online"`
	CanPost                 int          `json:"can_post"`
	CanSeeAllPosts          int          `json:"can_see_all_posts"`
	CanWritePrivateMessages int          `json:"can_write_private_message"`
	Status                  string       `json:"activity"`
	LastOnline              *LastSeen    `json:"last_seen"`
	Hidden                  int          `json:"hidden"`
	Deactivated             string       `json:"deactivated"`
	Relation                int          `json:"relation"`
}

type UserCity

type UserCity struct {
	ID    int    `json:"id"`
	Title string `json:"title"`
}

type UserCountry

type UserCountry struct {
	ID    int    `json:"id"`
	Title string `json:"title"`
}

type VKClient

type VKClient struct {
	Self   Token
	Client *http.Client
	// contains filtered or unexported fields
}

func NewVKClient

func NewVKClient(device int, user string, password string) (*VKClient, error)

func NewVKClientWithToken

func NewVKClientWithToken(token string, options *TokenOptions) (*VKClient, error)

func (*VKClient) AddLongpollCallback

func (client *VKClient) AddLongpollCallback(name string, f func(*LongPollMessage))

func (*VKClient) DeleteLongpollCallback

func (client *VKClient) DeleteLongpollCallback(name string)

func (*VKClient) DialogsGet

func (client *VKClient) DialogsGet(count int, params url.Values) (*Dialog, error)

func (*VKClient) DocsSearch

func (client *VKClient) DocsSearch(query string, count int, params url.Values) (int, []*DocAttachment, error)

func (*VKClient) FriendsAdd

func (client *VKClient) FriendsAdd(userID int, text string, follow int) error

func (*VKClient) FriendsDelete

func (client *VKClient) FriendsDelete(userID int) error

func (*VKClient) FriendsGet

func (client *VKClient) FriendsGet(uid int, count int) (int, []*User, error)

func (*VKClient) FriendsGetRequests

func (client *VKClient) FriendsGetRequests(count int, out int) (int, []*Request, error)

func (*VKClient) GetDocsString

func (client *VKClient) GetDocsString(docs []*DocAttachment) string

func (*VKClient) GetHistoryAttachments

func (client *VKClient) GetHistoryAttachments(peerID int, mediaType string, count int, params url.Values) (*HistoryAttachment, error)

func (*VKClient) GetMutual

func (client *VKClient) GetMutual(sourceUid int, targetUid int) []*int

func (*VKClient) GetPhotosString

func (client *VKClient) GetPhotosString(photos []*PhotoAttachment) string

func (*VKClient) GroupGet

func (client *VKClient) GroupGet(userID int, count int) (int, []*Group, error)

func (*VKClient) GroupGetMembers

func (client *VKClient) GroupGetMembers(group_id int, count int, offset int) (int, []*User, error)

func (*VKClient) GroupSearch

func (client *VKClient) GroupSearch(query string, count int) (int, []*Group, error)

func (*VKClient) GroupSendInvite

func (client *VKClient) GroupSendInvite(groupID int, userID int) error

func (*VKClient) LikesGet

func (client *VKClient) LikesGet(itemType string, ownerID int, itemID int, count int, params url.Values) (int, []*LikeUser, error)

func (*VKClient) ListenLongPollServer

func (client *VKClient) ListenLongPollServer()

func (*VKClient) ListenLongPollServerWithCancel

func (client *VKClient) ListenLongPollServerWithCancel(cancelCtx context.Context)

func (*VKClient) MakeRequest

func (client *VKClient) MakeRequest(method string, params url.Values) (APIResponse, error)

func (*VKClient) MessagesDelete

func (client *VKClient) MessagesDelete(ids []int, spam int, deleteForAll int) (int, error)

func (*VKClient) MessagesGet

func (client *VKClient) MessagesGet(count int, chatID int, isDialog bool, params url.Values) (int, []*DialogMessage, error)

func (*VKClient) MessagesGetByID

func (client *VKClient) MessagesGetByID(message_ids []int, params url.Values) (int, []*DialogMessage, error)

func (*VKClient) MessagesSend

func (client *VKClient) MessagesSend(user interface{}, message string, params url.Values) (APIResponse, error)

func (*VKClient) MessagesSetActivity

func (client *VKClient) MessagesSetActivity(user int, params url.Values) error

func (*VKClient) ResolveScreenName

func (client *VKClient) ResolveScreenName(name string) (ResolveScreenName, error)

func (*VKClient) UploadGroupWallDoc

func (client *VKClient) UploadGroupWallDoc(groupID int, fileName string) (*DocAttachment, error)

func (*VKClient) UploadGroupWallPhotos

func (client *VKClient) UploadGroupWallPhotos(groupID int, files []string) ([]*PhotoAttachment, error)

func (*VKClient) UploadMessagesPhotos

func (client *VKClient) UploadMessagesPhotos(peerID int, files []string) ([]*PhotoAttachment, error)

func (*VKClient) UsersGet

func (client *VKClient) UsersGet(users []int) ([]*User, error)

func (*VKClient) WallGet

func (client *VKClient) WallGet(id interface{}, count int, params url.Values) (*Wall, error)

func (*VKClient) WallGetByID

func (client *VKClient) WallGetByID(id string, params url.Values) (*Wall, error)

func (*VKClient) WallPost

func (client *VKClient) WallPost(ownerID int, message string, params url.Values) (int, error)

func (*VKClient) WallPostComment

func (client *VKClient) WallPostComment(ownerID int, postID int, message string, params url.Values) (int, error)

type VideoAttachment

type VideoAttachment struct {
	ID            int    `json:"id"`
	OwnerID       int    `json:"owner_id"`
	Title         string `json:"title"`
	Duration      int    `json:"duration"`
	Description   string `json:"description"`
	Date          int64  `json:"date"`
	AddingDate    int64  `json:"adding_date"`
	Views         int    `json:"views"`
	Width         int    `json:"width"`
	Height        int    `json:"height"`
	Photo130      string `json:"photo130"`
	Photo320      string `json:"photo320"`
	Photo800      string `json:"photo800"`
	FirstFrame320 string `json:"first_frame_320"`
	FirstFrame160 string `json:"first_frame_160"`
	FirstFrame130 string `json:"first_frame_130"`
	FirstFrame800 string `json:"first_frame_800"`
	Player        string `json:"player"`
	CanEdit       int    `json:"can_edit"`
	CanAdd        int    `json:"can_add"`
}

type Wall

type Wall struct {
	Count    int         `json:"count"`
	Posts    []*WallPost `json:"items"`
	Profiles []*User     `json:"profiles"`
	Groups   []*Group    `json:"groups"`
}

type WallPost

type WallPost struct {
	ID           int                  `json:"id"`
	FromID       int                  `json:"from_id"`
	OwnerID      int                  `json:"owner_id"`
	ToID         int                  `json:"to_id"`
	Date         int64                `json:"date"`
	MarkedAsAd   int                  `json:"marked_as_ads"`
	IsPinned     int                  `json:"is_pinned"`
	PostType     string               `json:"post_type"`
	CopyPostDate int64                `json:"copy_post_date"`
	CopyPostType string               `json:"copy_post_type"`
	CopyOwnerID  int                  `json:"copy_owner_id"`
	CopyPostID   int                  `json:"copy_post_id"`
	CopyHistory  []*WallPost          `json:"copy_history"`
	CreatedBy    int                  `json:"created_by"`
	Text         string               `json:"text"`
	CanDelete    int                  `json:"can_delete"`
	CanPin       int                  `json:"can_pin"`
	Attachments  []*MessageAttachment `json:"attachments"`
	PostSource   *Source              `json:"post_source"`
	Comments     *Comment             `json:"comments"`
	Likes        *Like                `json:"likes"`
	Reposts      *Repost              `json:"reposts"`
	Online       int                  `json:"online"`
	ReplyCount   int                  `json:"reply_count"`
}

Jump to

Keyboard shortcuts

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