Skip to content

Commit cbec08a

Browse files
authored
feat(location): support location msg (#162)
1 parent 6e2107c commit cbec08a

File tree

7 files changed

+141
-9
lines changed

7 files changed

+141
-9
lines changed

wechaty-puppet-mock/puppet_mock.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ type PuppetMock struct {
1212
*wechatyPuppet.Puppet
1313
}
1414

15+
func (p *PuppetMock) MessageLocation(messageID string) (*schemas.LocationPayload, error) {
16+
//TODO implement me
17+
panic("implement me")
18+
}
19+
20+
func (p *PuppetMock) MessageSendLocation(conversationID string, payload *schemas.LocationPayload) (string, error) {
21+
//TODO implement me
22+
panic("implement me")
23+
}
24+
1525
func NewPuppetMock(option wechatyPuppet.Option) (*PuppetMock, error) {
1626
puppetAbstract, err := wechatyPuppet.NewPuppet(option)
1727
if err != nil {

wechaty-puppet-service/puppet_service.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,52 @@ func (p *PuppetService) MessageFile(id string) (*filebox.FileBox, error) {
594594
return NewFileBoxFromMessageFileStream(response)
595595
}
596596

597+
// MessageLocation get location payload
598+
func (p *PuppetService) MessageLocation(messageID string) (*schemas.LocationPayload, error) {
599+
log.Tracef("PuppetService MessageLocation(%s)\n", messageID)
600+
601+
response, err := p.grpcClient.MessageLocation(context.Background(), &pbwechatypuppet.MessageLocationRequest{
602+
Id: messageID,
603+
})
604+
if err != nil {
605+
return nil, err
606+
}
607+
if response.Location == nil {
608+
return &schemas.LocationPayload{
609+
Address: "NOADDRESS",
610+
Name: "NONAME",
611+
}, nil
612+
}
613+
return &schemas.LocationPayload{
614+
Accuracy: response.Location.Accuracy,
615+
Address: response.Location.Address,
616+
Latitude: response.Location.Latitude,
617+
Longitude: response.Location.Longitude,
618+
Name: response.Location.Name,
619+
}, nil
620+
}
621+
622+
// MessageSendLocation send location
623+
func (p *PuppetService) MessageSendLocation(conversationID string, payload *schemas.LocationPayload) (string, error) {
624+
log.Tracef("PuppetService MessageSendLocation(%s, %+v)\n", conversationID, payload)
625+
626+
response, err := p.grpcClient.MessageSendLocation(context.Background(), &pbwechatypuppet.MessageSendLocationRequest{
627+
ConversationId: conversationID,
628+
Location: &pbwechatypuppet.LocationPayload{
629+
Accuracy: payload.Accuracy,
630+
Address: payload.Address,
631+
Latitude: payload.Latitude,
632+
Longitude: payload.Longitude,
633+
Name: payload.Name,
634+
},
635+
})
636+
if err != nil {
637+
return "", err
638+
}
639+
640+
return response.Id, nil
641+
}
642+
597643
// MessageRawPayload ...
598644
func (p *PuppetService) MessageRawPayload(id string) (*schemas.MessagePayload, error) {
599645
log.Tracef("PuppetService MessagePayload(%s)\n", id)

wechaty-puppet/puppet.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type iPuppet interface {
3131
MessageSendMiniProgram(conversationID string, miniProgramPayload *schemas.MiniProgramPayload) (string, error)
3232
MessageRecall(messageID string) (bool, error)
3333
MessageFile(id string) (*filebox.FileBox, error)
34+
MessageLocation(messageID string) (*schemas.LocationPayload, error)
35+
MessageSendLocation(conversationID string, payload *schemas.LocationPayload) (string, error)
3436
MessageRawPayload(id string) (*schemas.MessagePayload, error)
3537
MessageSendText(conversationID string, text string, mentionIDList ...string) (string, error)
3638
MessageSendFile(conversationID string, fileBox *filebox.FileBox) (string, error)

wechaty-puppet/schemas/location.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package schemas
2+
3+
type LocationPayload struct {
4+
Accuracy float32 `json:"accuracy"` // Estimated horizontal accuracy of this location, radial, in meters. (same as Android & iOS API)
5+
Address string `json:"address"` // 北京市北京市海淀区45 Chengfu Rd
6+
Latitude float64 `json:"latitude"` // 39.995120999999997
7+
Longitude float64 `json:"longitude"` // 116.3341
8+
Name string `json:"name"` // 东升乡人民政府(海淀区成府路45号)
9+
}

wechaty-puppet/schemas/url_link.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package schemas
33
import "encoding/json"
44

55
type UrlLinkPayload struct {
6-
Description string `json:"description"`
7-
ThumbnailUrl string `json:"thumbnailUrl"`
8-
Title string `json:"title"`
9-
Url string `json:"url"`
6+
Description string `json:"description"`
7+
ThumbnailUrl string `json:"thumbnailUrl"`
8+
Title string `json:"title"`
9+
Url string `json:"url"`
1010
}
1111

1212
func (u *UrlLinkPayload) ToJson() string {
13-
b, _ := json.Marshal(u)
14-
return string(b)
13+
b, _ := json.Marshal(u)
14+
return string(b)
1515
}

wechaty/user/location.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package user
2+
3+
import (
4+
"fmt"
5+
"github.com/wechaty/go-wechaty/wechaty-puppet/schemas"
6+
)
7+
8+
type Location struct {
9+
payload *schemas.LocationPayload
10+
}
11+
12+
func NewLocation(payload *schemas.LocationPayload) *Location {
13+
return &Location{
14+
payload: payload,
15+
}
16+
}
17+
18+
func (l *Location) Payload() schemas.LocationPayload {
19+
return *l.payload
20+
}
21+
22+
func (l *Location) String() string {
23+
return fmt.Sprintf("Location<%s>", l.payload.Name)
24+
}
25+
26+
func (l *Location) Address() string {
27+
return l.payload.Address
28+
}
29+
30+
func (l *Location) Latitude() float64 {
31+
return l.payload.Latitude
32+
}
33+
34+
func (l *Location) longitude() float64 {
35+
return l.payload.Longitude
36+
}
37+
38+
func (l *Location) Name() string {
39+
return l.payload.Name
40+
}
41+
42+
func (l *Location) Accuracy() float32 {
43+
return l.payload.Accuracy
44+
}

wechaty/user/message.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,20 @@ func (m *Message) Date() time.Time {
148148
}
149149

150150
// Say reply a Text or Media File message to the sender.
151-
func (m *Message) Say(textOrContactOrFileOrUrlOrMini interface{}) (_interface.IMessage, error) {
151+
// Support msg:
152+
// string
153+
// Contact
154+
// filebox.FileBox
155+
// UrlLink
156+
// MiniProgram
157+
// Location
158+
func (m *Message) Say(sayable interface{}) (_interface.IMessage, error) {
152159
conversationId, err := m.sayId()
153160
if err != nil {
154161
return nil, err
155162
}
156163
var messageID string
157-
switch v := textOrContactOrFileOrUrlOrMini.(type) {
164+
switch v := sayable.(type) {
158165
case string:
159166
messageID, err = m.GetPuppet().MessageSendText(conversationId, v)
160167
case *Contact:
@@ -165,8 +172,10 @@ func (m *Message) Say(textOrContactOrFileOrUrlOrMini interface{}) (_interface.IM
165172
messageID, err = m.GetPuppet().MessageSendURL(conversationId, v.payload)
166173
case *MiniProgram:
167174
messageID, err = m.GetPuppet().MessageSendMiniProgram(conversationId, v.payload)
175+
case *Location:
176+
messageID, err = m.GetPuppet().MessageSendLocation(conversationId, v.payload)
168177
default:
169-
return nil, fmt.Errorf("unknown msg: %v", textOrContactOrFileOrUrlOrMini)
178+
return nil, fmt.Errorf("unknown msg: %v", sayable)
170179
}
171180
if err != nil {
172181
return nil, err
@@ -421,6 +430,18 @@ func (m *Message) ToMiniProgram() (*MiniProgram, error) {
421430
return NewMiniProgram(miniProgramPayload), nil
422431
}
423432

433+
func (m *Message) ToLocation() (*Location, error) {
434+
if m.Type() != schemas.MessageTypeLocation {
435+
return nil, errors.New("message not a Location")
436+
}
437+
438+
payload, err := m.GetPuppet().MessageLocation(m.id)
439+
if err != nil {
440+
return nil, err
441+
}
442+
return NewLocation(payload), nil
443+
}
444+
424445
// ID message id
425446
func (m *Message) ID() string {
426447
return m.id

0 commit comments

Comments
 (0)