1
0
mirror of synced 2024-11-22 04:46:05 +03:00

Add delivery messages

This commit is contained in:
Harrison Shoebridge 2016-04-13 19:12:23 +10:00
parent f4f2d7a154
commit d63af22da1
5 changed files with 41 additions and 9 deletions

View File

@ -5,4 +5,5 @@ type Action int
const (
UnknownAction Action = iota - 1
TextAction
DeliveryAction
)

View File

@ -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())

View File

@ -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
}

View File

@ -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

View File

@ -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"`
}