Last active
February 23, 2023 18:00
-
-
Save HunterXProgrammer/fe60f4005af16caa6c407af66123558c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case "senddoc": | |
if len(args) < 3 { | |
log.Errorf("Usage: senddoc <jid> <document path> <title> [mime-type]") | |
return | |
} | |
recipient, ok := parseJID(args[0]) | |
if !ok { | |
return | |
} | |
data, err := os.ReadFile(args[1]) | |
if err != nil { | |
log.Errorf("Failed to read %s: %v", args[0], err) | |
return | |
} | |
uploaded, err := cli.Upload(context.Background(), data, whatsmeow.MediaDocument) | |
if err != nil { | |
log.Errorf("Failed to upload file: %v", err) | |
return | |
} | |
if len(args) < 4 { | |
msg := &waProto.Message{DocumentMessage: &waProto.DocumentMessage{ | |
Title: proto.String(args[2]), | |
Url: proto.String(uploaded.URL), | |
DirectPath: proto.String(uploaded.DirectPath), | |
MediaKey: uploaded.MediaKey, | |
Mimetype: proto.String(http.DetectContentType(data)), | |
FileEncSha256: uploaded.FileEncSHA256, | |
FileSha256: uploaded.FileSHA256, | |
FileLength: proto.Uint64(uint64(len(data))), | |
}} | |
resp, err := cli.SendMessage(context.Background(), recipient, msg) | |
if err != nil { | |
log.Errorf("Error sending document message: %v", err) | |
} else { | |
log.Infof("Document message sent (server timestamp: %s)", resp.Timestamp) | |
} | |
} else { | |
msg := &waProto.Message{DocumentMessage: &waProto.DocumentMessage{ | |
Title: proto.String(args[2]), | |
Url: proto.String(uploaded.URL), | |
DirectPath: proto.String(uploaded.DirectPath), | |
MediaKey: uploaded.MediaKey, | |
Mimetype: proto.String(args[3]), | |
FileEncSha256: uploaded.FileEncSHA256, | |
FileSha256: uploaded.FileSHA256, | |
FileLength: proto.Uint64(uint64(len(data))), | |
}} | |
resp, err := cli.SendMessage(context.Background(), recipient, msg) | |
if err != nil { | |
log.Errorf("Error sending document message: %v", err) | |
} else { | |
log.Infof("Document message sent (server timestamp: %s)", resp.Timestamp) | |
} | |
} | |
case "sendvid": | |
if len(args) < 2 { | |
log.Errorf("Usage: sendvid <jid> <video path> [base64 thumbnail]") | |
return | |
} | |
recipient, ok := parseJID(args[0]) | |
if !ok { | |
return | |
} | |
data, err := os.ReadFile(args[1]) | |
if err != nil { | |
log.Errorf("Failed to read %s: %v", args[0], err) | |
return | |
} | |
uploaded, err := cli.Upload(context.Background(), data, whatsmeow.MediaVideo) | |
if err != nil { | |
log.Errorf("Failed to upload file: %v", err) | |
return | |
} | |
if len(args) < 3 { | |
log.Errorf("No preview thumbnail specified") | |
log.Infof("The video message will still be sent, but preview won't be available") | |
msg := &waProto.Message{VideoMessage: &waProto.VideoMessage{ | |
Url: proto.String(uploaded.URL), | |
DirectPath: proto.String(uploaded.DirectPath), | |
MediaKey: uploaded.MediaKey, | |
Mimetype: proto.String(http.DetectContentType(data)), | |
FileEncSha256: uploaded.FileEncSHA256, | |
FileSha256: uploaded.FileSHA256, | |
FileLength: proto.Uint64(uint64(len(data))), | |
}} | |
resp, err := cli.SendMessage(context.Background(), recipient, msg) | |
if err != nil { | |
log.Errorf("Error sending video message: %v", err) | |
} else { | |
log.Infof("Video message sent (server timestamp: %s)", resp.Timestamp) | |
} | |
} else { | |
jpegImageFile, decode_err := base64Decode(args[2]) | |
if decode_err { | |
log.Errorf("Decoding failed.") | |
return | |
} | |
thumbnailResp, err := cli.Upload(context.Background(), []byte(jpegImageFile), whatsmeow.MediaImage) | |
if err != nil { | |
log.Errorf("Failed to upload preview thumbnail file: %v", err) | |
return | |
} | |
msg := &waProto.Message{VideoMessage: &waProto.VideoMessage{ | |
Url: proto.String(uploaded.URL), | |
DirectPath: proto.String(uploaded.DirectPath), | |
ThumbnailDirectPath: &thumbnailResp.DirectPath, | |
ThumbnailSha256: thumbnailResp.FileSHA256, | |
ThumbnailEncSha256: thumbnailResp.FileEncSHA256, | |
JpegThumbnail: []byte(jpegImageFile), | |
MediaKey: uploaded.MediaKey, | |
Mimetype: proto.String(http.DetectContentType(data)), | |
FileEncSha256: uploaded.FileEncSHA256, | |
FileSha256: uploaded.FileSHA256, | |
FileLength: proto.Uint64(uint64(len(data))), | |
}} | |
resp, err := cli.SendMessage(context.Background(), recipient, msg) | |
if err != nil { | |
log.Errorf("Error sending video message: %v", err) | |
} else { | |
log.Infof("Video message sent (server timestamp: %s)", resp.Timestamp) | |
} | |
} | |
case "sendaudio": | |
if len(args) < 2 { | |
log.Errorf("Usage: sendaudio <jid> <audio path>") | |
return | |
} | |
recipient, ok := parseJID(args[0]) | |
if !ok { | |
return | |
} | |
data, err := os.ReadFile(args[1]) | |
if err != nil { | |
log.Errorf("Failed to read %s: %v", args[0], err) | |
return | |
} | |
uploaded, err := cli.Upload(context.Background(), data, whatsmeow.MediaAudio) | |
if err != nil { | |
log.Errorf("Failed to upload file: %v", err) | |
return | |
} | |
if len(args) > 2 && args[2] == "1" { | |
os.Remove(args[1]) | |
} | |
msg := &waProto.Message{AudioMessage: &waProto.AudioMessage{ | |
Url: proto.String(uploaded.URL), | |
DirectPath: proto.String(uploaded.DirectPath), | |
MediaKey: uploaded.MediaKey, | |
Mimetype: proto.String("audio/ogg; codecs=opus"), | |
FileEncSha256: uploaded.FileEncSHA256, | |
FileSha256: uploaded.FileSHA256, | |
FileLength: proto.Uint64(uint64(len(data))), | |
}} | |
resp, err := cli.SendMessage(context.Background(), recipient, msg) | |
if err != nil { | |
log.Errorf("Error sending audio message: %v", err) | |
} else { | |
log.Infof("Audio message sent (server timestamp: %s)", resp.Timestamp) | |
} | |
case "sendimg": | |
if len(args) < 2 { | |
log.Errorf("Usage: sendimg <jid> <image path> [base64 thumbnail] [caption]") | |
return | |
} | |
recipient, ok := parseJID(args[0]) | |
if !ok { | |
return | |
} | |
data, err := os.ReadFile(args[1]) | |
if err != nil { | |
log.Errorf("Failed to read %s: %v", args[0], err) | |
return | |
} | |
uploaded, err := cli.Upload(context.Background(), data, whatsmeow.MediaImage) | |
if err != nil { | |
log.Errorf("Failed to upload file: %v", err) | |
return | |
} | |
if len(args) < 3 { | |
log.Errorf("No preview thumbnail specified") | |
log.Infof("The image message will still be sent, but preview won't be available") | |
msg := &waProto.Message{ImageMessage: &waProto.ImageMessage{ | |
Caption: proto.String(strings.Join(args[3:], " ")), | |
Url: proto.String(uploaded.URL), | |
DirectPath: proto.String(uploaded.DirectPath), | |
MediaKey: uploaded.MediaKey, | |
Mimetype: proto.String(http.DetectContentType(data)), | |
FileEncSha256: uploaded.FileEncSHA256, | |
FileSha256: uploaded.FileSHA256, | |
FileLength: proto.Uint64(uint64(len(data))), | |
}} | |
resp, err := cli.SendMessage(context.Background(), recipient, msg) | |
if err != nil { | |
log.Errorf("Error sending image message: %v", err) | |
} else { | |
log.Infof("Image message sent (server timestamp: %s)", resp.Timestamp) | |
} | |
} else { | |
if args[2] == "null" { | |
log.Errorf("No preview thumbnail specified") | |
log.Infof("The image message will still be sent, but preview won't be available") | |
msg := &waProto.Message{ImageMessage: &waProto.ImageMessage{ | |
Caption: proto.String(strings.Join(args[3:], " ")), | |
Url: proto.String(uploaded.URL), | |
DirectPath: proto.String(uploaded.DirectPath), | |
MediaKey: uploaded.MediaKey, | |
Mimetype: proto.String(http.DetectContentType(data)), | |
FileEncSha256: uploaded.FileEncSHA256, | |
FileSha256: uploaded.FileSHA256, | |
FileLength: proto.Uint64(uint64(len(data))), | |
}} | |
resp, err := cli.SendMessage(context.Background(), recipient, msg) | |
if err != nil { | |
log.Errorf("Error sending image message: %v", err) | |
} else { | |
log.Infof("Image message sent (server timestamp: %s)", resp.Timestamp) | |
} | |
} else { | |
jpegImageFile, decode_err := base64Decode(args[2]) | |
if decode_err { | |
log.Errorf("Decoding failed.") | |
return | |
} | |
thumbnailResp, err := cli.Upload(context.Background(), []byte(jpegImageFile), whatsmeow.MediaImage) | |
if err != nil { | |
log.Errorf("Failed to upload preview thumbnail file: %v", err) | |
return | |
} | |
msg := &waProto.Message{ImageMessage: &waProto.ImageMessage{ | |
Caption: proto.String(strings.Join(args[3:], " ")), | |
Url: proto.String(uploaded.URL), | |
DirectPath: proto.String(uploaded.DirectPath), | |
ThumbnailDirectPath: &thumbnailResp.DirectPath, | |
ThumbnailSha256: thumbnailResp.FileSHA256, | |
ThumbnailEncSha256: thumbnailResp.FileEncSHA256, | |
JpegThumbnail: []byte(jpegImageFile), | |
MediaKey: uploaded.MediaKey, | |
Mimetype: proto.String(http.DetectContentType(data)), | |
FileEncSha256: uploaded.FileEncSHA256, | |
FileSha256: uploaded.FileSHA256, | |
FileLength: proto.Uint64(uint64(len(data))), | |
}} | |
resp, err := cli.SendMessage(context.Background(), recipient, msg) | |
if err != nil { | |
log.Errorf("Error sending image message: %v", err) | |
} else { | |
log.Infof("Image message sent (server timestamp: %s)", resp.Timestamp) | |
} | |
} | |
} | |
case "setstatus": |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment