2 Commits

Author SHA1 Message Date
eneller
dede950958 wip: try to pin message 2026-01-30 21:19:09 +01:00
eneller
33f4c84d03 add cli pin flag 2026-01-30 21:18:49 +01:00

View File

@@ -14,6 +14,7 @@ import (
"github.com/mdp/qrterminal/v3" "github.com/mdp/qrterminal/v3"
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
"go.mau.fi/whatsmeow" "go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/proto/waCommon"
"go.mau.fi/whatsmeow/proto/waE2E" "go.mau.fi/whatsmeow/proto/waE2E"
"go.mau.fi/whatsmeow/store/sqlstore" "go.mau.fi/whatsmeow/store/sqlstore"
"go.mau.fi/whatsmeow/types" "go.mau.fi/whatsmeow/types"
@@ -69,6 +70,38 @@ func initClient() (*whatsmeow.Client, *sqlstore.Container, waLog.Logger) {
return client, container, dbLog return client, container, dbLog
} }
func sendMessage2(message *waE2E.Message, jidStr string, client *whatsmeow.Client) {
if message != nil {
var JID types.JID
JID, err := types.ParseJID(jidStr)
if err != nil {
slog.Error("Failed to parse", "JID", jidStr)
} else {
slog.Info("Parsed correctly", "JID", JID)
}
m, err := client.SendMessage(context.Background(), JID, message)
key := waCommon.MessageKey{ID: proto.String(m.ID), RemoteJID: proto.String(jidStr), FromMe: proto.Bool(true)}
m, err = client.SendMessage(context.Background(), JID, &waE2E.Message{
MessageContextInfo: &waE2E.MessageContextInfo{
MessageAddOnDurationInSecs: proto.Uint32(604800),
},
PinInChatMessage: &waE2E.PinInChatMessage{
Key: &key,
//Key: client.BuildMessageKey(JID, *client.Store.ID, m.ID),
Type: waE2E.PinInChatMessage_PIN_FOR_ALL.Enum(),
SenderTimestampMS: proto.Int64(time.Now().UnixMilli()),
},
EventMessage: &waE2E.EventMessage{},
})
// FIXME showing error even when successful
if err != nil {
slog.Error("Failed to send message", "error", err)
} else {
slog.Info("Sent Message successfully")
}
}
}
func sendMessage(message *waE2E.Message, jidStr string, client *whatsmeow.Client) { func sendMessage(message *waE2E.Message, jidStr string, client *whatsmeow.Client) {
if message != nil { if message != nil {
var JID types.JID var JID types.JID
@@ -97,6 +130,13 @@ func main() {
cmd := &cli.Command{ cmd := &cli.Command{
Usage: "Run WhatsApp actions from your CLI. User JID has to end with '@s.whatsapp.net', Group ID with '@g.us'." + Usage: "Run WhatsApp actions from your CLI. User JID has to end with '@s.whatsapp.net', Group ID with '@g.us'." +
"Defaults to listening on stdin for batch processing.", "Defaults to listening on stdin for batch processing.",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "pin",
Aliases: []string{"p"},
Usage: "Pin the message in a group chat",
},
},
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Println("No command specified. Reading from stdin. Press Ctrl+D to exit or run with --help to get help.") fmt.Println("No command specified. Reading from stdin. Press Ctrl+D to exit or run with --help to get help.")
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
@@ -130,7 +170,7 @@ func main() {
}, },
Action: func(ctx context.Context, cmd *cli.Command) error { Action: func(ctx context.Context, cmd *cli.Command) error {
message = &waE2E.Message{Conversation: proto.String(text)} message = &waE2E.Message{Conversation: proto.String(text)}
sendMessage(message, jidStr, client) sendMessage2(message, jidStr, client)
return nil return nil
}, },
}, },
@@ -169,6 +209,7 @@ func main() {
// before // before
client, container, _ = initClient() client, container, _ = initClient()
// run // run
//FIXME running cmd.Run after initclient() makes it impossible to print help without logging in
cmd.Run(context.Background(), os.Args) cmd.Run(context.Background(), os.Args)
// after // after
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)