127 lines
2.4 KiB
Go
127 lines
2.4 KiB
Go
package util
|
|
|
|
import (
|
|
"encoding/json"
|
|
)
|
|
|
|
type PayloadAction uint8
|
|
|
|
const (
|
|
PayloadActionYesNo = iota
|
|
PayloadActionVote
|
|
PayloadActionChooseKeyboard
|
|
)
|
|
|
|
type QuestionType uint8
|
|
|
|
const (
|
|
QuestionTypeRedmine = iota
|
|
QuestionTypeRedmineHours
|
|
QuestionTypeRedmineSendResult
|
|
)
|
|
|
|
type Payload struct {
|
|
User int64 `json:"u,omitempty"`
|
|
Chat int64 `json:"c,omitempty"`
|
|
Action PayloadAction `json:"a"`
|
|
Data json.RawMessage `json:"d,omitempty"`
|
|
}
|
|
|
|
func (p Payload) String() string {
|
|
data, _ := json.Marshal(&p)
|
|
return string(data)
|
|
}
|
|
|
|
func (p Payload) Vote() (val Vote) {
|
|
if p.Action != PayloadActionVote {
|
|
return
|
|
}
|
|
_ = json.Unmarshal(p.Data, &val)
|
|
return
|
|
}
|
|
|
|
func (p Payload) KeyboardChoice() (val KBChooserData) {
|
|
if p.Action != PayloadActionVote {
|
|
return
|
|
}
|
|
_ = json.Unmarshal(p.Data, &val)
|
|
return
|
|
}
|
|
|
|
func (p Payload) YesNoAnswer() (val Answer) {
|
|
if p.Action != PayloadActionYesNo {
|
|
return
|
|
}
|
|
_ = json.Unmarshal(p.Data, &val)
|
|
return
|
|
}
|
|
|
|
type Member struct {
|
|
ID int64
|
|
Name string
|
|
}
|
|
|
|
type KBChooserData struct {
|
|
Type uint8 `json:"k"`
|
|
}
|
|
|
|
type Vote struct {
|
|
Vote float32 `json:"v"`
|
|
}
|
|
|
|
type Answer struct {
|
|
Type QuestionType `json:"t"`
|
|
Result bool `json:"r"`
|
|
}
|
|
|
|
func NewKeyboardChooserPayload(userID, chatID int64, kbType uint8) *Payload {
|
|
return &Payload{
|
|
Action: PayloadActionChooseKeyboard,
|
|
User: userID,
|
|
Chat: chatID,
|
|
Data: marshal(KBChooserData{
|
|
Type: kbType,
|
|
}),
|
|
}
|
|
}
|
|
|
|
func NewVotePayload(chatID int64, vote float32) *Payload {
|
|
return &Payload{
|
|
Action: PayloadActionVote,
|
|
Chat: chatID,
|
|
Data: marshal(Vote{Vote: vote}),
|
|
}
|
|
}
|
|
|
|
func NewRedmineQuestionPayload(userID int64, chatID int64, isYes bool) *Payload {
|
|
return &Payload{
|
|
Action: PayloadActionYesNo,
|
|
User: userID,
|
|
Chat: chatID,
|
|
Data: marshal(Answer{Type: QuestionTypeRedmine, Result: isYes}),
|
|
}
|
|
}
|
|
|
|
func NewRedmineHoursQuestionPayload(userID int64, chatID int64, isYes bool) *Payload {
|
|
return &Payload{
|
|
Action: PayloadActionYesNo,
|
|
User: userID,
|
|
Chat: chatID,
|
|
Data: marshal(Answer{Type: QuestionTypeRedmineHours, Result: isYes}),
|
|
}
|
|
}
|
|
|
|
func NewRedmineSendResultQuestionPayload(userID int64, chatID int64, isYes bool) *Payload {
|
|
return &Payload{
|
|
Action: PayloadActionYesNo,
|
|
User: userID,
|
|
Chat: chatID,
|
|
Data: marshal(Answer{Type: QuestionTypeRedmineSendResult, Result: isYes}),
|
|
}
|
|
}
|
|
|
|
func marshal(val interface{}) json.RawMessage {
|
|
data, _ := json.Marshal(val)
|
|
return data
|
|
}
|