2024-05-09 20:48:59 +03:00
|
|
|
package util
|
2024-05-09 16:37:50 +03:00
|
|
|
|
|
|
|
import (
|
2024-05-09 17:42:42 +03:00
|
|
|
"encoding/json"
|
2024-05-09 16:37:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type PayloadAction uint8
|
|
|
|
|
|
|
|
const (
|
2024-05-09 20:48:59 +03:00
|
|
|
PayloadActionYesNo = iota
|
2024-05-09 16:37:50 +03:00
|
|
|
PayloadActionVote
|
2024-05-09 20:48:59 +03:00
|
|
|
PayloadActionChooseKeyboard
|
2024-05-10 11:46:43 +03:00
|
|
|
PayloadActionVoteFinish
|
|
|
|
PayloadActionVoteSendRedmine
|
2024-05-09 16:37:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type QuestionType uint8
|
|
|
|
|
|
|
|
const (
|
2024-05-09 20:48:59 +03:00
|
|
|
QuestionTypeRedmine = iota
|
2024-05-09 16:37:50 +03:00
|
|
|
QuestionTypeRedmineHours
|
2024-05-09 23:26:21 +03:00
|
|
|
QuestionTypeRedmineSendResult
|
2024-05-09 16:37:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Payload struct {
|
2024-05-10 11:46:43 +03:00
|
|
|
User int64 `json:"u,omitempty"`
|
|
|
|
Chat int64 `json:"c,omitempty"`
|
|
|
|
Message int `json:"m,omitempty"`
|
|
|
|
Action PayloadAction `json:"a"`
|
|
|
|
Data json.RawMessage `json:"d,omitempty"`
|
2024-05-09 16:37:50 +03:00
|
|
|
}
|
|
|
|
|
2024-05-09 20:48:59 +03:00
|
|
|
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) {
|
2024-05-23 14:22:44 +03:00
|
|
|
if p.Action != PayloadActionChooseKeyboard {
|
2024-05-09 20:48:59 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = json.Unmarshal(p.Data, &val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Payload) YesNoAnswer() (val Answer) {
|
|
|
|
if p.Action != PayloadActionYesNo {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = json.Unmarshal(p.Data, &val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-09 16:37:50 +03:00
|
|
|
type Member struct {
|
2024-05-09 17:42:42 +03:00
|
|
|
ID int64
|
2024-05-09 16:37:50 +03:00
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2024-05-09 20:48:59 +03:00
|
|
|
type KBChooserData struct {
|
2024-05-23 14:22:44 +03:00
|
|
|
Type int64 `json:"k"`
|
2024-05-09 20:48:59 +03:00
|
|
|
}
|
|
|
|
|
2024-05-09 16:37:50 +03:00
|
|
|
type Vote struct {
|
2024-05-10 11:46:43 +03:00
|
|
|
Vote float64 `json:"v"`
|
2024-05-09 16:37:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Answer struct {
|
|
|
|
Type QuestionType `json:"t"`
|
|
|
|
Result bool `json:"r"`
|
|
|
|
}
|
|
|
|
|
2024-05-09 23:26:21 +03:00
|
|
|
func NewKeyboardChooserPayload(userID, chatID int64, kbType uint8) *Payload {
|
2024-05-09 16:37:50 +03:00
|
|
|
return &Payload{
|
2024-05-09 20:48:59 +03:00
|
|
|
Action: PayloadActionChooseKeyboard,
|
2024-05-09 16:37:50 +03:00
|
|
|
User: userID,
|
|
|
|
Chat: chatID,
|
2024-05-09 20:48:59 +03:00
|
|
|
Data: marshal(KBChooserData{
|
2024-05-23 14:22:44 +03:00
|
|
|
Type: int64(kbType),
|
2024-05-09 20:48:59 +03:00
|
|
|
}),
|
2024-05-09 16:37:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-10 11:46:43 +03:00
|
|
|
func NewVotePayload(chatID int64, vote float64) *Payload {
|
2024-05-09 16:37:50 +03:00
|
|
|
return &Payload{
|
|
|
|
Action: PayloadActionVote,
|
|
|
|
Chat: chatID,
|
|
|
|
Data: marshal(Vote{Vote: vote}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-10 11:46:43 +03:00
|
|
|
func NewVoteFinishPayload(chatID int64) *Payload {
|
|
|
|
return &Payload{
|
|
|
|
Action: PayloadActionVoteFinish,
|
|
|
|
Chat: chatID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 17:42:42 +03:00
|
|
|
func NewRedmineQuestionPayload(userID int64, chatID int64, isYes bool) *Payload {
|
2024-05-09 16:37:50 +03:00
|
|
|
return &Payload{
|
|
|
|
Action: PayloadActionYesNo,
|
|
|
|
User: userID,
|
|
|
|
Chat: chatID,
|
|
|
|
Data: marshal(Answer{Type: QuestionTypeRedmine, Result: isYes}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 17:42:42 +03:00
|
|
|
func NewRedmineHoursQuestionPayload(userID int64, chatID int64, isYes bool) *Payload {
|
2024-05-09 16:37:50 +03:00
|
|
|
return &Payload{
|
|
|
|
Action: PayloadActionYesNo,
|
|
|
|
User: userID,
|
|
|
|
Chat: chatID,
|
|
|
|
Data: marshal(Answer{Type: QuestionTypeRedmineHours, Result: isYes}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 23:26:21 +03:00
|
|
|
func NewRedmineSendResultQuestionPayload(userID int64, chatID int64, isYes bool) *Payload {
|
|
|
|
return &Payload{
|
|
|
|
Action: PayloadActionYesNo,
|
|
|
|
User: userID,
|
|
|
|
Chat: chatID,
|
|
|
|
Data: marshal(Answer{Type: QuestionTypeRedmineSendResult, Result: isYes}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-10 11:46:43 +03:00
|
|
|
func NewRedmineSendResultPayload(chatID int64, resultToSend float64) *Payload {
|
|
|
|
return &Payload{
|
|
|
|
Action: PayloadActionVoteSendRedmine,
|
|
|
|
Chat: chatID,
|
|
|
|
Data: marshal(Vote{Vote: resultToSend}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 16:37:50 +03:00
|
|
|
func marshal(val interface{}) json.RawMessage {
|
|
|
|
data, _ := json.Marshal(val)
|
|
|
|
return data
|
|
|
|
}
|