Add delivery messages
This commit is contained in:
parent
f4f2d7a154
commit
d63af22da1
@ -5,4 +5,5 @@ type Action int
|
||||
const (
|
||||
UnknownAction Action = iota - 1
|
||||
TextAction
|
||||
DeliveryAction
|
||||
)
|
||||
|
@ -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())
|
||||
|
12
message.go
12
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
|
||||
}
|
||||
|
18
messenger.go
18
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
|
||||
|
15
receiving.go
15
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"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user