Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(location): support location msg #162

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
Comment on lines +15 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MessageLocation method is currently a placeholder. Consider documenting the expected behavior or purpose of this method for future implementation.

Would you like me to suggest a basic implementation or further documentation for this method?


func (p *PuppetMock) MessageSendLocation(conversationID string, payload *schemas.LocationPayload) (string, error) {
//TODO implement me
panic("implement me")
}
Comment on lines +20 to +23
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to MessageLocation, MessageSendLocation is also a placeholder. It would be beneficial to document the expected behavior or purpose of this method.

Would you like assistance in suggesting a basic implementation or further documentation for this method?


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)
Comment on lines +13 to +14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider handling the error returned by json.Marshal to ensure robust error handling and avoid potential issues if marshaling fails.

func (u *UrlLinkPayload) ToJson() string {
-   b, _ := json.Marshal(u)
+   b, err := json.Marshal(u)
+   if err != nil {
+       // Handle error (e.g., log it, return an error message, etc.)
+       return ""
+   }
    return string(b)
}

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
b, _ := json.Marshal(u)
return string(b)
b, err := json.Marshal(u)
if err != nil {
// Handle error (e.g., log it, return an error message, etc.)
return ""
}
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
}
Comment on lines +34 to +36
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method longitude should be exported for consistency with other getters. Consider renaming it to Longitude.

- func (l *Location) longitude() float64 {
+ func (l *Location) Longitude() float64 {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
func (l *Location) longitude() float64 {
return l.payload.Longitude
}
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
Loading