Skip to content

Commit

Permalink
new update types
Browse files Browse the repository at this point in the history
  • Loading branch information
vildan-valeev committed Dec 21, 2023
1 parent 514bbcb commit 2fca0fa
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 111 deletions.
9 changes: 9 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ func (a API) MessagesSend(text string, opts *MessagesSendOptions) (res APIRespon
return get[APIResponseMessagesSend](a.base, "messages.send", addValues(vals, opts))
}

func (a API) MessagesSendMessageEventAnswer(text string, opts *MessageEventAnswerOptions) (res APIResponseMessageEventAnswer, err error) {
var vals = make(url.Values)
vals.Set("message", text)
vals.Set("access_token", a.token)
vals.Set("v", APIVersion)
vals.Set("random_id", strconv.Itoa(int(rand.Uint32())))
return get[APIResponseMessageEventAnswer](a.base, "messages.sendMessageEventAnswer", addValues(vals, opts))
}

// UsersGet https://dev.vk.com/ru/method/users.get
func (a API) UsersGet(opts *UsersGetOptions) (res APIResponseUsersGet, err error) {
var vals = make(url.Values)
Expand Down
9 changes: 8 additions & 1 deletion dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func (d *Dispatcher) PollOptions(dropPendingUpdates bool) error {
return err
}

updates, err := result.Updates.UnmarshalCustom()
if err != nil {
return err
}

err = d.check(result)
if err != nil {
return err
Expand All @@ -85,7 +90,9 @@ func (d *Dispatcher) PollOptions(dropPendingUpdates bool) error {
// d.updates <- u
// }
//}
for _, u := range result.Updates {

for _, u := range updates {
fmt.Println("UPDATES!", u.Object.MessageNew)
d.updates <- u
}
//if l := len(response.Result); l > 0 {
Expand Down
30 changes: 26 additions & 4 deletions event_message_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,40 @@ type Keyboard struct {

type Button struct {
Action ButtonAction `json:"action"`
Color string `json:"color,omitempty"` // Button color
Color ButtonColor `json:"color,omitempty"` // Button color
}

type ButtonColor string

const (
ButtonColorPrimary ButtonColor = "primary"
ButtonColorSecondary ButtonColor = "secondary"
ButtonColorNegative ButtonColor = "negative"
ButtonColorPositive ButtonColor = "positive"
)

type ButtonAction struct {
Type ButtonType `json:"type"` // Button type
Label string `json:"label,omitempty"` // Label for button
Payload string `json:"payload,omitempty"` // Additional data sent along with message for developer convenience

AppID int `json:"app_id,omitempty"` // Fragment value in app link like vk.com/app{app_id}_-654321#hash
Hash string `json:"hash,omitempty"` // Fragment value in app link like vk.com/app123456_-654321#{hash}
Label string `json:"label,omitempty"` // Label for button
OwnerID int `json:"owner_id,omitempty"` // Fragment value in app link like vk.com/app123456_{owner_id}#hash
Payload string `json:"payload,omitempty"` // Additional data sent along with message for developer convenience
Type string `json:"type"` // Button type
Link string `json:"link,omitempty"` // Link URL
}

type ButtonType string

const (
ButtonTypeText ButtonType = "text"
ButtonTypeOpenLink ButtonType = "open_link"
ButtonTypeLocation ButtonType = "location"
ButtonTypeVkPay ButtonType = "vkpay"
ButtonTypeOpenApp ButtonType = "open_app"
ButtonTypeCallBack ButtonType = "callback"
)

// BaseMessageGeo struct.
type BaseMessageGeo struct {
Coordinates BaseGeoCoordinates `json:"coordinates"`
Expand Down
60 changes: 33 additions & 27 deletions events.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
package gvk

import "encoding/json"

// MessageNew https://dev.vk.com/ru/api/community-events/json-schema#Сообщения
type MessageNew struct {
Message Message `json:"message"`
ClientInfo ClientInfo `json:"client_info"`
}

//// MessageReplyObject struct.
//type MessageReplyObject object.MessagesMessage
// MessageReply struct.
type MessageReply Message

// // MessageEditObject struct.
// type MessageEditObject object.MessagesMessage
//
//// MessageEditObject struct.
//type MessageEditObject object.MessagesMessage
// // MessageAllowObject struct.
//
//// MessageAllowObject struct.
//type MessageAllowObject struct {
// UserID int `json:"user_id"`
// Key string `json:"key"`
//}
// type MessageAllowObject struct {
// UserID int `json:"user_id"`
// Key string `json:"key"`
// }
//
//// MessageDenyObject struct.
//type MessageDenyObject struct {
// UserID int `json:"user_id"`
//}
// // MessageDenyObject struct.
//
//// MessageTypingStateObject struct.
//type MessageTypingStateObject struct {
// State string `json:"state"`
// FromID int `json:"from_id"`
// ToID int `json:"to_id"`
//}
// type MessageDenyObject struct {
// UserID int `json:"user_id"`
// }
//
//// MessageEventObject struct.
//type MessageEventObject struct {
// UserID int `json:"user_id"`
// PeerID int `json:"peer_id"`
// EventID string `json:"event_id"`
// Payload json.RawMessage `json:"payload"`
// ConversationMessageID int `json:"conversation_message_id"`
//}
// // MessageTypingStateObject struct.
//
// type MessageTypingStateObject struct {
// State string `json:"state"`
// FromID int `json:"from_id"`
// ToID int `json:"to_id"`
// }
//
// // MessageEventObject struct.
type MessageEvent struct {
UserID int64 `json:"user_id"`
PeerID int64 `json:"peer_id"`
EventID string `json:"event_id"`
Payload json.RawMessage `json:"payload"`
ConversationMessageID int64 `json:"conversation_message_id"`
}

//
//// PhotoNewObject struct.
//type PhotoNewObject object.PhotosPhoto
Expand Down
6 changes: 0 additions & 6 deletions example/config.go

This file was deleted.

12 changes: 9 additions & 3 deletions example/main.go → example/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"github.com/vildan-valeev/gvk"
)

const (
groupID = 194299208
token = "b30fae3f8d488e20cdbe041cbec9a0aa62e7c52e6107f97f97a9fd9007abe32223e1373cce590bfabf374"
)

type stateFn func(event *gvk.Update) stateFn

type Bot struct {
Expand All @@ -33,7 +38,7 @@ func (b *Bot) Update(update *gvk.Update) {
}

func (b *Bot) EntryHandler(update *gvk.Update) stateFn {
if strings.HasPrefix(update.MessageNew.Message.Text, "ping") {
if strings.HasPrefix(update.Object.MessageNew.Message.Text, "ping") {
b.MessagesSend("pong", &gvk.MessagesSendOptions{UserID: b.chatID})
return b.handleNext
}
Expand All @@ -44,9 +49,10 @@ func (b *Bot) EntryHandler(update *gvk.Update) stateFn {
}

func (b *Bot) handleNext(update *gvk.Update) stateFn {
b.name = update.MessageNew.Message.Text

b.MessagesSend("pong again )))", &gvk.MessagesSendOptions{
UserID: b.chatID})
UserID: b.chatID,
})

return b.EntryHandler
}
Expand Down
110 changes: 110 additions & 0 deletions example/keyboards/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package main

import (
"fmt"
"log"
"strings"

"github.com/vildan-valeev/gvk"
)

const (
groupID = 194299208
token = "b30fae3f8d488e20cdbe041cbec9a0aa62e7c52e6107f97f97a9fd9007abe32223e1373cce590bfabf374"
)

type stateFn func(event *gvk.Update) stateFn

type Bot struct {
chatID int64
state stateFn
name string

gvk.API
}

func newBot(chatID int64) gvk.Bot {
b := &Bot{
chatID: chatID,
API: gvk.NewAPI(token),
}

b.state = b.EntryHandler
return b
}

func (b *Bot) Update(update *gvk.Update) {
b.state = b.state(update)
}

func (b *Bot) EntryHandler(update *gvk.Update) stateFn {
fmt.Println("INCOME TEXT", update.Object.MessageNew.Message.Text)
if strings.HasPrefix(update.Object.MessageNew.Message.Text, "ping") {
buttons := make([][]gvk.Button, 0)
buttons = append(buttons, []gvk.Button{
{
Color: gvk.ButtonColorPrimary,
Action: gvk.ButtonAction{
Type: gvk.ButtonTypeText,
Label: "Текст кнопки",
},
},
})

b.MessagesSend("pong", &gvk.MessagesSendOptions{
UserID: b.chatID,
Keyboard: gvk.Keyboard{
Inline: false,
Buttons: buttons,
},
})
return b.handleNext
}

b.MessagesSend("not understand...", &gvk.MessagesSendOptions{UserID: b.chatID})

return b.EntryHandler
}

func (b *Bot) handleNext(update *gvk.Update) stateFn {

buttons := make([][]gvk.Button, 0)
buttons = append(buttons, []gvk.Button{
{
Color: gvk.ButtonColorPrimary,
Action: gvk.ButtonAction{
Type: gvk.ButtonTypeCallBack,
Label: "Жми колбек",
Payload: `{"data":"loading"}`,
},
},
})

b.MessagesSend("pong again )))", &gvk.MessagesSendOptions{
UserID: b.chatID,
Keyboard: gvk.Keyboard{
Inline: true,
Buttons: buttons,
},
})

return b.hanleCallback
}

func (b *Bot) hanleCallback(update *gvk.Update) stateFn {
s := string(update.Object.MessageEvent.Payload)
b.MessagesSend(fmt.Sprintf("Callback received %s", s), &gvk.MessagesSendOptions{
UserID: b.chatID,
})

return b.EntryHandler
}

func main() {
fmt.Print("Start with Keyboards! \n")

dsp, err := gvk.NewDispatcher(token, groupID, newBot)

log.Println(err)
log.Println(dsp.Poll())
}
14 changes: 11 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type GetLongPollServerOptions struct {
}

type MessagesSendOptions struct {
UserID int64 `query:"user_id"`
UserID int64 `query:"user_id"`
Keyboard Keyboard `query:"keyboard"`
//RandomID int32 `query:"random_id"`
//PeerID int64 `query:"peer_id"`
}
Expand All @@ -25,6 +26,13 @@ type SetLongPollSettingsOptions struct {
}

type UsersGetOptions struct {
UserIDS string `json:"user_ids"`
Fields string `json:"fields"`
UserIDS string `query:"user_ids"`
Fields string `query:"fields"`
}

type MessageEventAnswerOptions struct {
EventID string `query:"event_id"`
UserID int64 `query:"user_id"`
PeerID int64 `query:"peer_id"`
EventData string `query:"text"`
}
Loading

0 comments on commit 2fca0fa

Please sign in to comment.