Add delivery messages
This commit is contained in:
parent
f4f2d7a154
commit
d63af22da1
@ -5,4 +5,5 @@ type Action int
|
|||||||
const (
|
const (
|
||||||
UnknownAction Action = iota - 1
|
UnknownAction Action = iota - 1
|
||||||
TextAction
|
TextAction
|
||||||
|
DeliveryAction
|
||||||
)
|
)
|
||||||
|
@ -34,6 +34,10 @@ func main() {
|
|||||||
fmt.Println(r.Text("Hello, World!"))
|
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")
|
fmt.Println("Serving messenger bot on localhost:8080")
|
||||||
|
|
||||||
http.ListenAndServe("localhost:8080", m.Handler())
|
http.ListenAndServe("localhost:8080", m.Handler())
|
||||||
|
12
message.go
12
message.go
@ -3,8 +3,16 @@ package messenger
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Sender int64
|
Sender Sender
|
||||||
Recipient int64
|
Recipient Recipient
|
||||||
Time time.Time
|
Time time.Time
|
||||||
Text string
|
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 {
|
if f := m.handlers[a]; f != nil {
|
||||||
message := Message{
|
message := Message{
|
||||||
Sender: info.Sender.ID,
|
Sender: info.Sender,
|
||||||
Recipient: info.Recipient.ID,
|
Recipient: info.Recipient,
|
||||||
Time: time.Unix(info.Timestamp, 0),
|
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{
|
response := &Response{
|
||||||
@ -100,6 +110,8 @@ func (m *Messenger) dispatch(r Receive) {
|
|||||||
func (m *Messenger) classify(info MessageInfo, e Entry) Action {
|
func (m *Messenger) classify(info MessageInfo, e Entry) Action {
|
||||||
if info.Message != nil {
|
if info.Message != nil {
|
||||||
return TextAction
|
return TextAction
|
||||||
|
} else if info.Delivery != nil {
|
||||||
|
return DeliveryAction
|
||||||
}
|
}
|
||||||
|
|
||||||
return UnknownAction
|
return UnknownAction
|
||||||
|
15
receiving.go
15
receiving.go
@ -12,10 +12,11 @@ type Entry struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type MessageInfo struct {
|
type MessageInfo struct {
|
||||||
Sender Sender `json:"sender"`
|
Sender Sender `json:"sender"`
|
||||||
Recipient Recipient `json:"recipient"`
|
Recipient Recipient `json:"recipient"`
|
||||||
Timestamp int64 `json:"timestamp"`
|
Timestamp int64 `json:"timestamp"`
|
||||||
Message *MessageCallback `json:"message"`
|
Message *MessageCallback `json:"message"`
|
||||||
|
Delivery *DeliveryCallback `json:"delivery"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Sender struct {
|
type Sender struct {
|
||||||
@ -31,3 +32,9 @@ type MessageCallback struct {
|
|||||||
Seq int `json:"seq"`
|
Seq int `json:"seq"`
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DeliveryCallback struct {
|
||||||
|
Mids []string `json:"mids"`
|
||||||
|
Watermark int64 `json:"watermark"`
|
||||||
|
Seq int `json:"seq"`
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user