wechaty / go-wechaty

Go Wechaty is a Conversational SDK for Chatbot Makers Written in Go
https://github.com/wechaty/go-wechaty-getting-started
Apache License 2.0
489 stars 92 forks source link

当OnMessage接收到的Message的Type是MessageTypeLocation,怎么获取到Location信息呢? #161

Closed jmjoy closed 6 months ago

jmjoy commented 6 months ago

我看Text是空的,也没有ToLocation之类的方法,怎么能获取到Location的经纬度?

dosubot[bot] commented 6 months ago

嘿,@jmjoy,又见面了!希望你今天心情不错 😜

在go-wechaty中,当OnMessage接收到的MessageTypeMessageTypeLocation时,获取经纬度信息的方法并不直接。根据提供的上下文,Message结构体中并没有直接的方法或属性可以让你直接获取位置消息的经纬度。

在go-wechaty中,MessageTypeLocation消息的位置信息通常会被封装在特定于位置消息的有效负载中。由于提供的代码中没有直接访问位置信息(如经纬度)的方法,你需要扩展Message结构体的功能,或者使用底层的Puppet接口来访问包含位置信息的原始消息数据。

如果wechaty-puppet接口或者你正在使用的特定puppet支持位置消息,它可能会提供一个类似于MessageFileMessageContactMessageURLMessageMiniProgram的方法来获取位置详情。你需要查看puppet的文档或源代码以找到这样的方法。

在没有直接方法的情况下,一种方法是在Message结构体中实现一个新的方法,该方法使用GetPuppet()方法来访问puppet的API,以获取包含MessageTypeLocation消息的位置详情的原始消息数据。这种方法需要了解puppet的API以及它如何存储或提供位置数据的访问。

鉴于提供的上下文的限制,以下是你可能开始实现这样一个方法的假设示例,假设puppet的API有一个名为MessageLocation的方法,该方法返回位置详情:

// ToLocation retrieves the location information from a location type message.
func (m *Message) ToLocation() (*LocationPayload, error) {
    if m.Type() != schemas.MessageTypeLocation {
        return nil, errors.New("message not a location type")
    }
    locationPayload, err := m.GetPuppet().MessageLocation(m.id)
    if err != nil {
        return nil, err
    }
    return locationPayload, nil
}

这个示例假设存在一个LocationPayload结构体来保存位置数据,以及puppet接口中的一个MessageLocation方法,这些在提供的上下文中都不存在。你需要根据你正在使用的puppet的实际能力和API来调整这种方法。

来源:

Sources

#### About Dosu This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
dchaofei commented 6 months ago

暂时还不支持 MessageTypeLocation 类型的消息处理, 不过我刚提了一个 pr:https://github.com/wechaty/go-wechaty/pull/162 去支持,还没来得及测试

dchaofei commented 6 months ago

已在 v0.4.12 版本支持

jmjoy commented 6 months ago

感谢大佬!