Skip to content

Commit

Permalink
feat(location): support location msg
Browse files Browse the repository at this point in the history
  • Loading branch information
dchaofei committed Mar 29, 2024
1 parent 6e2107c commit ca15c07
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 9 deletions.
10 changes: 10 additions & 0 deletions wechaty-puppet-mock/puppet_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ type PuppetMock struct {
*wechatyPuppet.Puppet
}

func (p *PuppetMock) MessageLocation(messageID string) (*schemas.LocationPayload, error) {
//TODO implement me
panic("implement me")
}

func (p *PuppetMock) MessageSendLocation(conversationID string, payload *schemas.LocationPayload) (string, error) {
//TODO implement me
panic("implement me")
}

func NewPuppetMock(option wechatyPuppet.Option) (*PuppetMock, error) {
puppetAbstract, err := wechatyPuppet.NewPuppet(option)
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions wechaty-puppet-service/puppet_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,52 @@ func (p *PuppetService) MessageFile(id string) (*filebox.FileBox, error) {
return NewFileBoxFromMessageFileStream(response)
}

// MessageLocation get location payload
func (p *PuppetService) MessageLocation(messageID string) (*schemas.LocationPayload, error) {
log.Tracef("PuppetService MessageLocation(%s)\n", messageID)

response, err := p.grpcClient.MessageLocation(context.Background(), &pbwechatypuppet.MessageLocationRequest{
Id: messageID,
})
if err != nil {
return nil, err
}
if response.Location == nil {
return &schemas.LocationPayload{
Address: "NOADDRESS",
Name: "NONAME",
}, nil
}
return &schemas.LocationPayload{
Accuracy: response.Location.Accuracy,
Address: response.Location.Address,
Latitude: response.Location.Latitude,
Longitude: response.Location.Longitude,
Name: response.Location.Name,
}, nil
}

// MessageSendLocation send location
func (p *PuppetService) MessageSendLocation(conversationID string, payload *schemas.LocationPayload) (string, error) {
log.Tracef("PuppetService MessageSendLocation(%s, %+v)\n", conversationID, payload)

response, err := p.grpcClient.MessageSendLocation(context.Background(), &pbwechatypuppet.MessageSendLocationRequest{
ConversationId: conversationID,
Location: &pbwechatypuppet.LocationPayload{
Accuracy: payload.Accuracy,
Address: payload.Address,
Latitude: payload.Latitude,
Longitude: payload.Longitude,
Name: payload.Name,
},
})
if err != nil {
return "", err
}

return response.Id, nil
}

// MessageRawPayload ...
func (p *PuppetService) MessageRawPayload(id string) (*schemas.MessagePayload, error) {
log.Tracef("PuppetService MessagePayload(%s)\n", id)
Expand Down
2 changes: 2 additions & 0 deletions wechaty-puppet/puppet.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type iPuppet interface {
MessageSendMiniProgram(conversationID string, miniProgramPayload *schemas.MiniProgramPayload) (string, error)
MessageRecall(messageID string) (bool, error)
MessageFile(id string) (*filebox.FileBox, error)
MessageLocation(messageID string) (*schemas.LocationPayload, error)
MessageSendLocation(conversationID string, payload *schemas.LocationPayload) (string, error)
MessageRawPayload(id string) (*schemas.MessagePayload, error)
MessageSendText(conversationID string, text string, mentionIDList ...string) (string, error)
MessageSendFile(conversationID string, fileBox *filebox.FileBox) (string, error)
Expand Down
9 changes: 9 additions & 0 deletions wechaty-puppet/schemas/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package schemas

type LocationPayload struct {
Accuracy float32 `json:"accuracy"` // Estimated horizontal accuracy of this location, radial, in meters. (same as Android & iOS API)
Address string `json:"address"` // 北京市北京市海淀区45 Chengfu Rd
Latitude float64 `json:"latitude"` // 39.995120999999997
Longitude float64 `json:"longitude"` // 116.3341
Name string `json:"name"` // 东升乡人民政府(海淀区成府路45号)
}
12 changes: 6 additions & 6 deletions wechaty-puppet/schemas/url_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package schemas
import "encoding/json"

type UrlLinkPayload struct {
Description string `json:"description"`
ThumbnailUrl string `json:"thumbnailUrl"`
Title string `json:"title"`
Url string `json:"url"`
Description string `json:"description"`
ThumbnailUrl string `json:"thumbnailUrl"`
Title string `json:"title"`
Url string `json:"url"`
}

func (u *UrlLinkPayload) ToJson() string {
b, _ := json.Marshal(u)
return string(b)
b, _ := json.Marshal(u)
return string(b)
}
44 changes: 44 additions & 0 deletions wechaty/user/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package user

import (
"fmt"
"github.com/wechaty/go-wechaty/wechaty-puppet/schemas"
)

type Location struct {
payload *schemas.LocationPayload
}

func NewLocation(payload *schemas.LocationPayload) *Location {
return &Location{
payload: payload,
}
}

func (l *Location) Payload() schemas.LocationPayload {
return *l.payload
}

func (l *Location) String() string {
return fmt.Sprintf("Location<%s>", l.payload.Name)
}

func (l *Location) Address() string {
return l.payload.Address
}

func (l *Location) Latitude() float64 {
return l.payload.Latitude
}

func (l *Location) longitude() float64 {
return l.payload.Longitude
}

func (l *Location) Name() string {
return l.payload.Name
}

func (l *Location) Accuracy() float32 {
return l.payload.Accuracy
}
27 changes: 24 additions & 3 deletions wechaty/user/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,20 @@ func (m *Message) Date() time.Time {
}

// Say reply a Text or Media File message to the sender.
func (m *Message) Say(textOrContactOrFileOrUrlOrMini interface{}) (_interface.IMessage, error) {
// Support msg:
// string
// Contact
// filebox.FileBox
// UrlLink
// MiniProgram
// Location
func (m *Message) Say(sayable interface{}) (_interface.IMessage, error) {
conversationId, err := m.sayId()
if err != nil {
return nil, err
}
var messageID string
switch v := textOrContactOrFileOrUrlOrMini.(type) {
switch v := sayable.(type) {
case string:
messageID, err = m.GetPuppet().MessageSendText(conversationId, v)
case *Contact:
Expand All @@ -165,8 +172,10 @@ func (m *Message) Say(textOrContactOrFileOrUrlOrMini interface{}) (_interface.IM
messageID, err = m.GetPuppet().MessageSendURL(conversationId, v.payload)
case *MiniProgram:
messageID, err = m.GetPuppet().MessageSendMiniProgram(conversationId, v.payload)
case *Location:
messageID, err = m.GetPuppet().MessageSendLocation(conversationId, v.payload)
default:
return nil, fmt.Errorf("unknown msg: %v", textOrContactOrFileOrUrlOrMini)
return nil, fmt.Errorf("unknown msg: %v", sayable)
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -421,6 +430,18 @@ func (m *Message) ToMiniProgram() (*MiniProgram, error) {
return NewMiniProgram(miniProgramPayload), nil
}

func (m *Message) ToLocation() (*Location, error) {
if m.Type() != schemas.MessageTypeLocation {
return nil, errors.New("message not a Location")
}

payload, err := m.GetPuppet().MessageLocation(m.id)
if err != nil {
return nil, err
}
return NewLocation(payload), nil
}

// ID message id
func (m *Message) ID() string {
return m.id
Expand Down

0 comments on commit ca15c07

Please sign in to comment.