Skip to content

Commit

Permalink
add wall.post method
Browse files Browse the repository at this point in the history
  • Loading branch information
vildan-valeev committed Jan 13, 2024
1 parent 9fe21ed commit 0bb7933
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 79 deletions.
5 changes: 5 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,8 @@ func (a API) MessagesSendMessageEventAnswer(opts *MessageEventAnswerOptions) (re
func (a API) UsersGet(opts *UsersGetOptions) (res APIResponseUsersGet, err error) {
return get[APIResponseUsersGet](a.base, "users.get", addValues(a.defaultParams(), opts))
}

// WallPost https://dev.vk.com/ru/method/wall.post
func (a API) WallPost(opts *WallPostOptions) (res APIResponseWallPost, err error) {
return get[APIResponseWallPost](a.base, "wall.post", addValues(a.defaultParams(), opts))
}
132 changes: 115 additions & 17 deletions event_message_new.go → event_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const (
// BaseMessageGeo struct.
type BaseMessageGeo struct {
Coordinates BaseGeoCoordinates `json:"coordinates"`
Place BasePlace `json:"place"`
Place Place `json:"place"`
Showmap int `json:"showmap"`
Type string `json:"type"`
}
Expand All @@ -132,23 +132,21 @@ type BaseGeoCoordinates struct {
}

// BasePlace struct.
type BasePlace struct {
Address string `json:"address"`
Checkins int `json:"checkins"`
City interface{} `json:"city"` // BUG(VK): https://github.com/VKCOM/vk-api-schema/issues/143
Country interface{} `json:"country"`
Created int `json:"created"`
ID int `json:"id"`
Icon string `json:"icon"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Title string `json:"title"`
Type string `json:"type"`
IsDeleted bool `json:"is_deleted"`
TotalCheckins int `json:"total_checkins"`
Updated int `json:"updated"`
CategoryObject BaseCategoryObject `json:"category_object"`
type Place struct {
ID int `json:"id"`
Title string `json:"title"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Created int `json:"created"`
Icon string `json:"icon"`
Checkins int `json:"checkins"`
Updated int `json:"updated"`
Type string `json:"type"`
Country int `json:"country"`
City int `json:"city"`
Address string `json:"address"`
}

type BaseCategoryObject struct {
ID int `json:"id"`
Title string `json:"title"`
Expand All @@ -162,3 +160,103 @@ type BaseImage struct {
Width float64 `json:"width"`
Type string `json:"type"`
}

type Photo struct {
ID int `json:"id"` // Photo ID
AlbumID int `json:"album_id"` // Album ID
OwnerID int `json:"owner_id"` // Photo owner's ID
UserID int `json:"user_id"` // ID of the user who have uploaded the photo
Text string `json:"text"` // Photo caption
Date int `json:"date"` // Date when uploaded
Sizes []PhotoSizes `json:"sizes"`
Width int `json:"width"` // Original photo width
Height int `json:"height"` // Original photo height
}

type PhotoSizes struct {
Height float64 `json:"height"`
URL string `json:"url"`
Width float64 `json:"width"`
Type string `json:"type"`
}

type WallPost struct {
ID int `json:"id"` // Post ID
OwnerID int `json:"owner_id"` // Wall owner's ID
FromID int `json:"from_id"` // Post author ID
CreatedBy int `json:"created_by"`
Date int `json:"date"` // Date of publishing in Unixtime
Text string `json:"text"` // Post text
ReplyOwnerID int `json:"reply_owner_id"`
ReplyPostID int `json:"reply_post_id"`
FriendsOnly int `json:"friends_only"` // 1, если запись была создана с опцией «Только для друзей».
Comments Comments `json:"comments"`
Copyright Copyright `json:"copyright"`
Likes Likes `json:"likes"` // Count of likes
Reposts Reposts `json:"reposts"` // Count of reposts
Views Views `json:"views"` // Count of views
PostType string `json:"post_type"`
PostSource PostSource `json:"post_source"`

//Attachments []WallWallpostAttachment `json:"attachments"`
Geo Geo `json:"geo"`
SignerID int `json:"signer_id"` // Post signer ID
CopyHistory []WallPost `json:"copy_history"`
CanPin int `json:"can_pin"`
CanDelete int `json:"can_delete"`
CanEdit int `json:"can_edit"`
IsPinned int `json:"is_pinned"`
IsFavorite int `json:"is_favorite"` // Information whether the post in favorites list
MarkedAsAds int `json:"marked_as_ads"`
PostponedID int `json:"postponed_id"` // ID from scheduled posts
}

// CommentsInfo struct.
type Comments struct {
Count int `json:"count"`
CanPost int `json:"can_post"` // информация о том, может ли текущий пользователь комментировать запись (1 — может, 0 — не может);
GroupsCanPost int `json:"groups_can_post"`
CanClose int `json:"can_close"`
CanOpen int `json:"can_open"`
}

// Copyright information about the source of the post.
type Copyright struct {
ID int `json:"id,omitempty"`
Link string `json:"link"`
Type string `json:"type"`
Name string `json:"name"`
}

// Likes struct.
type Likes struct {
CanLike int `json:"can_like"` // Information whether current user can like the post
CanPublish int `json:"can_publish"` // Information whether current user can repost
UserLikes int `json:"user_likes"` // Information whether current uer has liked the post
Count int `json:"count"` // Likes number
}

// Reposts struct.
type Reposts struct {
Count int `json:"count"`
UserReposted int `json:"user_reposted"`
}

// Views struct.
type Views struct {
Count int `json:"count"` // Count
}

type PostSource struct {
Data string `json:"data"` // Additional data
Platform string `json:"platform"` // Platform name
Type string `json:"type"`
URL string `json:"url"` // URL to an external site used to publish the post
}

// BaseGeo struct.
type Geo struct {
Coordinates string `json:"coordinates"`
Place Place `json:"place"`
Type string `json:"type"`
}
119 changes: 61 additions & 58 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package gvk

import "encoding/json"

// MessageNew https://dev.vk.com/ru/api/community-events/json-schema#Сообщения
/*
https://dev.vk.com/ru/api/community-events/json-schema
*/

// MessageNew struct.
type MessageNew struct {
Message Message `json:"message"`
ClientInfo ClientInfo `json:"client_info"`
Expand All @@ -14,28 +18,25 @@ type MessageReply Message
// MessageEdit struct.
type MessageEdit Message

// // MessageAllowObject struct.
//
// type MessageAllowObject struct {
// UserID int `json:"user_id"`
// Key string `json:"key"`
// }
//
// // MessageDenyObject struct.
//
// type MessageDenyObject struct {
// UserID int `json:"user_id"`
// }
//
// // MessageTypingStateObject struct.
//// MessageAllow struct.
//type MessageAllow struct {
// UserID int `json:"user_id"`
// Key string `json:"key"`
//}
//
// type MessageTypingStateObject struct {
// State string `json:"state"`
// FromID int `json:"from_id"`
// ToID int `json:"to_id"`
// }
//// MessageDeny struct.
//type MessageDeny struct {
// UserID int `json:"user_id"`
//}
//
// // MessageEventObject struct.
//// MessageTypingState struct.
//type MessageTypingState struct {
// State string `json:"state"`
// FromID int `json:"from_id"`
// ToID int `json:"to_id"`
//}

// MessageEvent struct.
type MessageEvent struct {
UserID int64 `json:"user_id"`
PeerID int64 `json:"peer_id"`
Expand All @@ -44,54 +45,56 @@ type MessageEvent struct {
ConversationMessageID int64 `json:"conversation_message_id"`
}

// PhotoNewObject struct.
type PhotoNew Photo

// // PhotoCommentNewObject struct.
// type PhotoCommentNewObject object.WallWallComment
//
//// PhotoNewObject struct.
//type PhotoNewObject object.PhotosPhoto
//
//// PhotoCommentNewObject struct.
//type PhotoCommentNewObject object.WallWallComment
// // PhotoCommentEditObject struct.
// type PhotoCommentEditObject object.WallWallComment
//
//// PhotoCommentEditObject struct.
//type PhotoCommentEditObject object.WallWallComment
// // PhotoCommentRestoreObject struct.
// type PhotoCommentRestoreObject object.WallWallComment
//
//// PhotoCommentRestoreObject struct.
//type PhotoCommentRestoreObject object.WallWallComment
// // PhotoCommentDeleteObject struct.
//
//// PhotoCommentDeleteObject struct.
//type PhotoCommentDeleteObject struct {
// OwnerID int `json:"owner_id"`
// ID int `json:"id"`
// UserID int `json:"user_id"`
// DeleterID int `json:"deleter_id"`
// PhotoID int `json:"photo_id"`
//}
// type PhotoCommentDeleteObject struct {
// OwnerID int `json:"owner_id"`
// ID int `json:"id"`
// UserID int `json:"user_id"`
// DeleterID int `json:"deleter_id"`
// PhotoID int `json:"photo_id"`
// }
//
//// AudioNewObject struct.
//type AudioNewObject object.AudioAudio
// // AudioNewObject struct.
// type AudioNewObject object.AudioAudio
//
//// VideoNewObject struct.
//type VideoNewObject object.VideoVideo
// // VideoNewObject struct.
// type VideoNewObject object.VideoVideo
//
//// VideoCommentNewObject struct.
//type VideoCommentNewObject object.WallWallComment
// // VideoCommentNewObject struct.
// type VideoCommentNewObject object.WallWallComment
//
//// VideoCommentEditObject struct.
//type VideoCommentEditObject object.WallWallComment
// // VideoCommentEditObject struct.
// type VideoCommentEditObject object.WallWallComment
//
//// VideoCommentRestoreObject struct.
//type VideoCommentRestoreObject object.WallWallComment
// // VideoCommentRestoreObject struct.
// type VideoCommentRestoreObject object.WallWallComment
//
//// VideoCommentDeleteObject struct.
//type VideoCommentDeleteObject struct {
// OwnerID int `json:"owner_id"`
// ID int `json:"id"`
// UserID int `json:"user_id"`
// DeleterID int `json:"deleter_id"`
// VideoID int `json:"video_id"`
//}
// // VideoCommentDeleteObject struct.
//
//// WallPostNewObject struct.
//type WallPostNewObject object.WallWallpost
// type VideoCommentDeleteObject struct {
// OwnerID int `json:"owner_id"`
// ID int `json:"id"`
// UserID int `json:"user_id"`
// DeleterID int `json:"deleter_id"`
// VideoID int `json:"video_id"`
// }

// WallPostNewObject struct.
type WallPostNew WallPost

//
//// WallRepostObject struct.
//type WallRepostObject object.WallWallpost
Expand Down
19 changes: 17 additions & 2 deletions example/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import (
)

const (
groupID = 194299208
token = "b30fae3f8d488e20cdbe041cbec9a0aa62e7c52e6107f97f97a9fd9007abe32223e1373cce590bfabf374"
groupID = 194299208
token = "vk1.a.8-WDS15nXOxt3wU9pfCOHjAt0e7LiOZl8_u_su7PzFXVcgRgpJXZbDOB_cpBVKPOitdfTi9_Bp-oGQbrEeRr_ozjdHB3tbCtJOQFSe6VSEfX5C_IzvEUqZ3xnPlODyQTohFEh-EzhP9CcQEK5Ei6s0Xwy2L3JRSYIoup2wZFApIhpkGDTU5tdvQ09Skz7qMO3eg8fmqpM6jIzMfBIkZR6A"
tokenPosting = "vk1.a.F3PWupGyC4SKUFgQs51H1u7NOXcm8uWweVusWkmzWDwZha2uUzCeAItJGq4GBbwSQEnqIrHvVph6tt5xaHOc5w96q0GVKgJShvtUEIjtcEgj81CzXeaN8nJLEjrKN6ZVxpTU54Id45JOhY3sFBxg1giD45JxclmQXrk9FYxem6aRmXTpkSq-9hPnxuTyu5wb"
)

/*
https://api.vk.com/method/wall.post?access_token=vk1.a.F3PWupGyC4SKUFgQs51H1u7NOXcm8uWweVusWkmzWDwZha2uUzCeAItJGq4GBbwSQEnqIrHvVph6tt5xaHOc5w96q0GVKgJShvtUEIjtcEgj81CzXeaN8nJLEjrKN6ZVxpTU54Id45JOhY3sFBxg1giD45JxclmQXrk9FYxem6aRmXTpkSq-9hPnxuTyu5wb&v=5.131&message=pong&from_group=1&owner_id=-194299208
*/

type stateFn func(event *gvk.Update) stateFn

type Bot struct {
Expand Down Expand Up @@ -40,6 +45,16 @@ func (b *Bot) Update(update *gvk.Update) {
func (b *Bot) EntryHandler(update *gvk.Update) stateFn {
if strings.HasPrefix(update.Object.MessageNew.Message.Text, "ping") {
b.MessagesSend(&gvk.MessagesSendOptions{Message: "pong", UserID: b.chatID})

poster := gvk.NewAPI(tokenPosting)
opt := gvk.WallPostOptions{
Message: "pong",
OwnerID: -groupID,
FromGroup: 1,
}
post, err := poster.WallPost(&opt)
log.Println(post.Response.PostID, err)

return b.handleNext
}

Expand Down
21 changes: 19 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type MessagesSendOptions struct {
Message string `query:"message"`
UserID int64 `query:"user_id"`
Keyboard Keyboard `query:"keyboard"`
DontParseLinks int64 `json:"dont_parse_links"` // 1 - true, 0 - false
DisableMentions int64 `json:"disable_mentions"` // 1 - true, 0 - false
DontParseLinks int64 `query:"dont_parse_links"` // 1 - true, 0 - false
DisableMentions int64 `query:"disable_mentions"` // 1 - true, 0 - false
}

type MessagesEditOptions struct {
Expand Down Expand Up @@ -62,3 +62,20 @@ type GroupsIsMemberOptions struct {
UserIDs string `query:"user_ids"`
Extended int64 `query:"extended"`
}

type WallPostOptions struct {
Message string `query:"message"`
OwnerID int64 `query:"owner_id"`
FriendsOnly int64 `query:"friends_only"` // 1 - true, 0 - false
FromGroup int64 `query:"from_group"` // 1 - true, 0 - false
Signed int64 `query:"signed"` // 1 - true, 0 - false
PublishDate int64 `query:"publish_date"`
Lat string `query:"lat"` // float64 as string: from -90 to 90
Long string `query:"long"` // float64 as string: from -90 to 90
MarkAsAds string `query:"mark_as_ads"` // 1 - true, 0 - false
LinkPhotoID string `query:"link_photo_id"`
LinkTitle string `query:"link_title"`
CloseComments int64 `query:"close_comments"` // 1 - true, 0 - false
MuteNotifications int64 `query:"mute_notifications"` // 1 - true, 0 - false
Copyright string `query:"copyright"`
}
1 change: 1 addition & 0 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func get[T Response](base, endpoint string, vals url.Values) (res T, err error)
if vals != nil {
if queries := vals.Encode(); queries != "" {
url = fmt.Sprintf("%s?%s", url, queries)
//log.Println(url)
}
}
//log.Printf("Request URL: %s", url)
Expand Down
Loading

0 comments on commit 0bb7933

Please sign in to comment.