登录
首页 >  Golang >  Go问答

实时获取发件人位置的Whatsmeow

来源:stackoverflow

时间:2024-02-07 21:54:23 130浏览 收藏

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《实时获取发件人位置的Whatsmeow》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

问题内容

我正在使用 golang 包中的 whatsmeow https://pkg.go.dev/go.mau.fi/whatsmeow

我尝试了该示例并运行没有问题,但我很好奇,有没有办法从消息中获取发件人的实时位置?

这是我的日志:

05:57:27.711 [client/recv debug] 
    05:57:27.712 [client debug] decrypting 1 messages from [email protected]
    05:57:27.712 [client/send debug] 
    getconversation :  
    sender :  [email protected]
    sender number :  6281312000300
    isgroup :  false
    messagesource :  {[email protected] [email protected] false false }
    id :  974eeaed36d9d7b9354cf31d78bbe2a8
    pushname :  rolly maulana awangga
    broadcastlistowner :
    category :
    chat :  [email protected]
    devicesentmeta :  
    isfromme :  false
    mediatype :  livelocation
    multicast :  false
    info.chat.server :  s.whatsapp.net
    05:57:27.773 [client/send debug] 

发送方将实时位置发送给接收方。接收者可以从whatsmeow go包中查找mediatype是否为livelocation。那么有没有办法获取实时位置坐标? 这是我的代码:

package main

    import (
        "context"
        "fmt"
        "os"
        "os/signal"
        "syscall"

        _ "github.com/mattn/go-sqlite3"

        "go.mau.fi/whatsmeow"
        "go.mau.fi/whatsmeow/store/sqlstore"
        "go.mau.fi/whatsmeow/types/events"
        waLog "go.mau.fi/whatsmeow/util/log"
    )

    var client *whatsmeow.Client

    func eventHandler(evt interface{}) {
        switch v := evt.(type) {
        case *events.Message:
            if !v.Info.IsFromMe {
                fmt.Println("GetConversation : ", v.Message.GetConversation())
                fmt.Println("Sender : ", v.Info.Sender)
                fmt.Println("Sender Number : ", v.Info.Sender.User)
                fmt.Println("IsGroup : ", v.Info.IsGroup)
                fmt.Println("MessageSource : ", v.Info.MessageSource)
                fmt.Println("ID : ", v.Info.ID)
                fmt.Println("PushName : ", v.Info.PushName)
                fmt.Println("BroadcastListOwner : ", v.Info.BroadcastListOwner)
                fmt.Println("Category : ", v.Info.Category)
                fmt.Println("Chat : ", v.Info.Chat)
                fmt.Println("DeviceSentMeta : ", v.Info.DeviceSentMeta)
                fmt.Println("IsFromMe : ", v.Info.IsFromMe)
                fmt.Println("MediaType : ", v.Info.MediaType)
                fmt.Println("Multicast : ", v.Info.Multicast)
                fmt.Println("Info.Chat.Server : ", v.Info.Chat.Server)
                if v.Info.Chat.Server == "g.us" {
                    groupInfo, err := client.GetGroupInfo(v.Info.Chat)
                    fmt.Println("error GetGroupInfo : ", err)
                    fmt.Println("Nama Group : ", groupInfo.GroupName.Name)
                }
            }
        }
    }

    func main() {
        dbLog := waLog.Stdout("Database", "DEBUG", true)
        // Make sure you add appropriate DB connector imports, e.g. github.com/mattn/go-sqlite3 for SQLite
        container, err := sqlstore.New("sqlite3", "file:gowa.db?_foreign_keys=on", dbLog)
        if err != nil {
            panic(err)
        }
        // If you want multiple sessions, remember their JIDs and use .GetDevice(jid) or .GetAllDevices() instead.
        deviceStore, err := container.GetFirstDevice()
        if err != nil {
            panic(err)
        }
        clientLog := waLog.Stdout("Client", "DEBUG", true)
        client = whatsmeow.NewClient(deviceStore, clientLog)
        client.AddEventHandler(eventHandler)

        if client.Store.ID == nil {
            // No ID stored, new login
            qrChan, _ := client.GetQRChannel(context.Background())
            err = client.Connect()
            if err != nil {
                panic(err)
            }
            for evt := range qrChan {
                if evt.Event == "code" {
                    // Render the QR code here
                    // e.g. qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
                    // or just manually `echo 2@... | qrencode -t ansiutf8` in a terminal
                    fmt.Println("QR code:", evt.Code)
                } else {
                    fmt.Println("Login event:", evt.Event)
                }
            }
        } else {
            // Already logged in, just connect
            err = client.Connect()
            if err != nil {
                panic(err)
            }
        }

        // Listen to Ctrl+C (you can also do something else that prevents the program from exiting)
        c := make(chan os.Signal)
        signal.Notify(c, os.Interrupt, syscall.SIGTERM)
        <-c

        client.Disconnect()
    }

正确答案


go.mau.fi/whatsmeow/types/events.Message 中消息的实际数据位于名为 Message 的字段中,其类型为 go.mau.fi/whatsmeow/types/binary/proto.Message .

这是一个生成的 Go 结构体,由 protobuf 编译器生成。 该结构具有每种类型消息的字段。 如果事件的媒体类型是“livelocation”,那么您应该期望 livelocation 数据为 v.Message.LiveLocationMessage

非常感谢。你节省了我的时间。这其实是我所希望的

fmt.Println("实时定位:", v.Message.LiveLocationMessage.DegreesLatitude)

今天关于《实时获取发件人位置的Whatsmeow》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>