获取发送者的实时位置

huangapple go评论100阅读模式
英文:

whatsmeow get live location from sender

问题

我正在使用golang包https://pkg.go.dev/go.mau.fi/whatsmeow中的whatsmeow。我尝试了示例并成功运行,但我很好奇,是否有办法从消息中获取发送者的实时位置?

这是我的日志:

05:57:27.711 [Client/Recv DEBUG] <message from="6281312000300@s.whatsapp.net" id="974EEAED36D9D7B9354CF31D78BBE2A8" notify="Rolly Maulana Awangga" t="1663801047" type="media"><enc duration="3599" mediatype="livelocation" type="pkmsg" v="2"><!-- 3415 bytes --></enc></message>
05:57:27.712 [Client DEBUG] Decrypting 1 messages from 6281312000300@s.whatsapp.net
05:57:27.712 [Client/Send DEBUG] <ack class="message" id="974EEAED36D9D7B9354CF31D78BBE2A8" to="6281312000300@s.whatsapp.net"/>
GetConversation :  
Sender :  6281312000300@s.whatsapp.net
Sender Number :  6281312000300
IsGroup :  false
MessageSource :  {6281312000300@s.whatsapp.net 6281312000300@s.whatsapp.net false false }
ID :  974EEAED36D9D7B9354CF31D78BBE2A8
PushName :  Rolly Maulana Awangga
BroadcastListOwner :
Category :
Chat :  6281312000300@s.whatsapp.net
DeviceSentMeta :  <nil>
IsFromMe :  false
MediaType :  livelocation
Multicast :  false
Info.Chat.Server :  s.whatsapp.net
05:57:27.773 [Client/Send DEBUG] <receipt id="974EEAED36D9D7B9354CF31D78BBE2A8" to="6281312000300@s.whatsapp.net" type="inactive"/>

发送者向接收者发送了实时位置。接收者可以从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()
}
英文:

I'm using whatsmeow from golang package https://pkg.go.dev/go.mau.fi/whatsmeow

I try the example and run without problem, but I'm curious, is there a way to get sender's live location from the message??

this is my log :


05:57:27.711 [Client/Recv DEBUG] &lt;message from=&quot;6281312000300@s.whatsapp.net&quot; id=&quot;974EEAED36D9D7B9354CF31D78BBE2A8&quot; notify=&quot;Rolly Maulana Awangga&quot; t=&quot;1663801047&quot; type=&quot;media&quot;&gt;&lt;enc duration=&quot;3599&quot; mediatype=&quot;livelocation&quot; type=&quot;pkmsg&quot; v=&quot;2&quot;&gt;&lt;!-- 3415 bytes --&gt;&lt;/enc&gt;&lt;/message&gt;
05:57:27.712 [Client DEBUG] Decrypting 1 messages from 6281312000300@s.whatsapp.net
05:57:27.712 [Client/Send DEBUG] &lt;ack class=&quot;message&quot; id=&quot;974EEAED36D9D7B9354CF31D78BBE2A8&quot; to=&quot;6281312000300@s.whatsapp.net&quot;/&gt;
GetConversation :  
Sender :  6281312000300@s.whatsapp.net
Sender Number :  6281312000300
IsGroup :  false
MessageSource :  {6281312000300@s.whatsapp.net 6281312000300@s.whatsapp.net false false }
ID :  974EEAED36D9D7B9354CF31D78BBE2A8
PushName :  Rolly Maulana Awangga
BroadcastListOwner :
Category :
Chat :  6281312000300@s.whatsapp.net
DeviceSentMeta :  &lt;nil&gt;
IsFromMe :  false
MediaType :  livelocation
Multicast :  false
Info.Chat.Server :  s.whatsapp.net
05:57:27.773 [Client/Send DEBUG] &lt;receipt id=&quot;974EEAED36D9D7B9354CF31D78BBE2A8&quot; to=&quot;6281312000300@s.whatsapp.net&quot; type=&quot;inactive&quot;/&gt;

The sender sends the live location to the receiver. the receiver can look up Medyatype is livelocation from whatsmeow go package. so there is a way to get the live location coordinate?
and this is my code :


package main
import (
&quot;context&quot;
&quot;fmt&quot;
&quot;os&quot;
&quot;os/signal&quot;
&quot;syscall&quot;
_ &quot;github.com/mattn/go-sqlite3&quot;
&quot;go.mau.fi/whatsmeow&quot;
&quot;go.mau.fi/whatsmeow/store/sqlstore&quot;
&quot;go.mau.fi/whatsmeow/types/events&quot;
waLog &quot;go.mau.fi/whatsmeow/util/log&quot;
)
var client *whatsmeow.Client
func eventHandler(evt interface{}) {
switch v := evt.(type) {
case *events.Message:
if !v.Info.IsFromMe {
fmt.Println(&quot;GetConversation : &quot;, v.Message.GetConversation())
fmt.Println(&quot;Sender : &quot;, v.Info.Sender)
fmt.Println(&quot;Sender Number : &quot;, v.Info.Sender.User)
fmt.Println(&quot;IsGroup : &quot;, v.Info.IsGroup)
fmt.Println(&quot;MessageSource : &quot;, v.Info.MessageSource)
fmt.Println(&quot;ID : &quot;, v.Info.ID)
fmt.Println(&quot;PushName : &quot;, v.Info.PushName)
fmt.Println(&quot;BroadcastListOwner : &quot;, v.Info.BroadcastListOwner)
fmt.Println(&quot;Category : &quot;, v.Info.Category)
fmt.Println(&quot;Chat : &quot;, v.Info.Chat)
fmt.Println(&quot;DeviceSentMeta : &quot;, v.Info.DeviceSentMeta)
fmt.Println(&quot;IsFromMe : &quot;, v.Info.IsFromMe)
fmt.Println(&quot;MediaType : &quot;, v.Info.MediaType)
fmt.Println(&quot;Multicast : &quot;, v.Info.Multicast)
fmt.Println(&quot;Info.Chat.Server : &quot;, v.Info.Chat.Server)
if v.Info.Chat.Server == &quot;g.us&quot; {
groupInfo, err := client.GetGroupInfo(v.Info.Chat)
fmt.Println(&quot;error GetGroupInfo : &quot;, err)
fmt.Println(&quot;Nama Group : &quot;, groupInfo.GroupName.Name)
}
}
}
}
func main() {
dbLog := waLog.Stdout(&quot;Database&quot;, &quot;DEBUG&quot;, true)
// Make sure you add appropriate DB connector imports, e.g. github.com/mattn/go-sqlite3 for SQLite
container, err := sqlstore.New(&quot;sqlite3&quot;, &quot;file:gowa.db?_foreign_keys=on&quot;, 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(&quot;Client&quot;, &quot;DEBUG&quot;, 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 == &quot;code&quot; {
// 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(&quot;QR code:&quot;, evt.Code)
} else {
fmt.Println(&quot;Login event:&quot;, 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)
&lt;-c
client.Disconnect()
}

答案1

得分: 2

go.mau.fi/whatsmeow/types/events.Message中,消息的实际数据存储在名为Message的字段中,该字段的类型为go.mau.fi/whatsmeow/types/binary/proto.Message

这是一个由protobuf编译器生成的Go结构体。
该结构体为每种类型的消息定义了字段。
如果事件的媒体类型是“livelocation”,那么你应该在v.Message.LiveLocationMessage中找到livelocation数据。

英文:

The actual data of the message in a go.mau.fi/whatsmeow/types/events.Message is in a field called Message, which is of type go.mau.fi/whatsmeow/types/binary/proto.Message.

That is a generated Go struct, generated by the protobuf compiler.
The struct has fields for each type of message.
If the mediatype of the event is "livelocation", then you should expect the livelocation data to be in v.Message.LiveLocationMessage

答案2

得分: 1

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

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

英文:

big thanks. You save me time. this is actually I hope for

fmt.Println("livelocation : ", v.Message.LiveLocationMessage.DegreesLatitude)

huangapple
  • 本文由 发表于 2022年9月22日 07:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/73807988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定