diff --git a/actions.go b/actions.go index e535d71..6b545c0 100644 --- a/actions.go +++ b/actions.go @@ -5,4 +5,5 @@ type Action int const ( UnknownAction Action = iota - 1 TextAction + DeliveryAction ) diff --git a/cmd/bot/main.go b/cmd/bot/main.go index fec281d..f1edd0a 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -34,6 +34,10 @@ func main() { fmt.Println(r.Text("Hello, World!")) }) + m.Handle(messenger.DeliveryAction, func(m messenger.Message, r *messenger.Response) { + fmt.Println(m.Delivery.Watermark.Format(time.UnixDate)) + }) + fmt.Println("Serving messenger bot on localhost:8080") http.ListenAndServe("localhost:8080", m.Handler()) diff --git a/message.go b/message.go index 28228fb..bc43581 100644 --- a/message.go +++ b/message.go @@ -3,8 +3,16 @@ package messenger import "time" type Message struct { - Sender int64 - Recipient int64 + Sender Sender + Recipient Recipient Time time.Time Text string + Seq int + Delivery *Delivery +} + +type Delivery struct { + Mids []string + Watermark time.Time + Seq int } diff --git a/messenger.go b/messenger.go index 1cd4e47..71d5956 100644 --- a/messenger.go +++ b/messenger.go @@ -80,10 +80,20 @@ func (m *Messenger) dispatch(r Receive) { if f := m.handlers[a]; f != nil { message := Message{ - Sender: info.Sender.ID, - Recipient: info.Recipient.ID, + Sender: info.Sender, + Recipient: info.Recipient, Time: time.Unix(info.Timestamp, 0), - Text: info.Message.Text, + } + + switch a { + case TextAction: + message.Text = info.Message.Text + case DeliveryAction: + message.Delivery = &Delivery{ + Mids: info.Delivery.Mids, + Seq: info.Delivery.Seq, + Watermark: time.Unix(info.Delivery.Watermark, 0), + } } response := &Response{ @@ -100,6 +110,8 @@ func (m *Messenger) dispatch(r Receive) { func (m *Messenger) classify(info MessageInfo, e Entry) Action { if info.Message != nil { return TextAction + } else if info.Delivery != nil { + return DeliveryAction } return UnknownAction diff --git a/receiving.go b/receiving.go index 2548488..8a87514 100644 --- a/receiving.go +++ b/receiving.go @@ -12,10 +12,11 @@ type Entry struct { } type MessageInfo struct { - Sender Sender `json:"sender"` - Recipient Recipient `json:"recipient"` - Timestamp int64 `json:"timestamp"` - Message *MessageCallback `json:"message"` + Sender Sender `json:"sender"` + Recipient Recipient `json:"recipient"` + Timestamp int64 `json:"timestamp"` + Message *MessageCallback `json:"message"` + Delivery *DeliveryCallback `json:"delivery"` } type Sender struct { @@ -31,3 +32,9 @@ type MessageCallback struct { Seq int `json:"seq"` Text string `json:"text"` } + +type DeliveryCallback struct { + Mids []string `json:"mids"` + Watermark int64 `json:"watermark"` + Seq int `json:"seq"` +}