Corrrected the script

This commit is contained in:
2025-06-19 21:32:13 +02:00
parent 99c016dba9
commit c8f24c08ec
2 changed files with 90 additions and 54 deletions

View File

@@ -29,13 +29,14 @@ type Parameters struct {
WhatsappSessionFile string WhatsappSessionFile string
WhatsappGroup string WhatsappGroup string
TimeToRun string TimeToRun string
RunOnce bool DevelopmentMode string
HealthchecksURL string HealthchecksURL string
} }
type Album struct { type Album struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"albumName"` Name string `json:"albumName"`
Description string `json:"description"`
Shared bool `json:"shared"` Shared bool `json:"shared"`
HasSharedLink bool `json:"hasSharedLink"` HasSharedLink bool `json:"hasSharedLink"`
StartDate time.Time `json:"startDate"` StartDate time.Time `json:"startDate"`
@@ -88,26 +89,18 @@ func connect(param Parameters) (*whatsmeow.Client, error) {
return client, nil return client, nil
} }
func sendMessage(client *whatsmeow.Client, group string, message string, url string, title string, thumbnail []byte) error { func sendMessage(client *whatsmeow.Client, group string, title string, description string, message string, url string, thumbnail []byte) error {
jid, err := types.ParseJID(group) jid, err := types.ParseJID(group)
if err != nil { if err != nil {
return fmt.Errorf("Incorrect group identifier '%s': %v", group, err) return fmt.Errorf("Incorrect group identifier '%s': %v", group, err)
} }
// msg := &waProto.Message{ExtendedTextMessage: &waProto.ExtendedTextMessage{
// Text: proto.String(message),
// Title: proto.String(title),
// Description: proto.String(title),
// CanonicalUrl: proto.String(url),
// MatchedText: proto.String(url),
// JPEGThumbnail: thumbnail,
// }}
msg := &waProto.Message{ msg := &waProto.Message{
ExtendedTextMessage: &waProto.ExtendedTextMessage{ ExtendedTextMessage: &waProto.ExtendedTextMessage{
Text: proto.String(fmt.Sprintf("%s\n%s\n%s", title, message, url)),
Title: proto.String(title), Title: proto.String(title),
Description: proto.String(message), Description: proto.String(description),
// PreviewType: proto.Uint32(0), // 0 pour un lien standard Text: proto.String(message),
MatchedText: proto.String(url),
JPEGThumbnail: thumbnail, JPEGThumbnail: thumbnail,
}, },
} }
@@ -218,7 +211,7 @@ func getSharingKey(album Album, param Parameters) (string, error) {
return "", fmt.Errorf("Error retrieving sharing key for album '%s': %v", album.Name, err) return "", fmt.Errorf("Error retrieving sharing key for album '%s': %v", album.Name, err)
} }
for _, key := range keys { for _, key := range keys {
if (album.ID == key.Album.ID) { if (key.Album != nil && album.ID == key.Album.ID) {
return key.Key, nil return key.Key, nil
} }
} }
@@ -292,6 +285,29 @@ func getThumbnail(album Album, param Parameters) ([]byte, error) {
return thumbnail, nil return thumbnail, nil
} }
func eventHandler(evt interface{}) {
fmt.Println("Received a message!", evt)
}
func listen(param Parameters) error {
// Create new WhatsApp connection and connect
client, err := connect(param)
if err != nil {
return fmt.Errorf("Error creating connection to WhatsApp: %v", err)
}
<-time.After(3 * time.Second)
defer client.Disconnect()
client.AddEventHandler(eventHandler)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
return nil
}
func runLoop(param Parameters) error { func runLoop(param Parameters) error {
// Create new WhatsApp connection and connect // Create new WhatsApp connection and connect
client, err := connect(param) client, err := connect(param)
@@ -334,51 +350,56 @@ func runLoop(param Parameters) error {
} }
for _, album := range albums { for _, album := range albums {
// Get albums from x years ago if album.Shared {
if album.Shared && (album.StartDate.Month() == time.Now().Month()) && (album.StartDate.Day() == time.Now().Day()) { // Get albums from x years ago
// Retrieve the sharing key if param.DevelopmentMode == "run-last" || (album.StartDate.Month() == time.Now().Month()) && (album.StartDate.Day() == time.Now().Day()) {
sharingKey, err := getSharingKey(album, param) // Retrieve the sharing key
if err != nil { sharingKey, err := getSharingKey(album, param)
return fmt.Errorf("Error retrieving the sharing key for album '%s': %v", album.Name, err) if err != nil {
return fmt.Errorf("Error retrieving the sharing key for album '%s': %v", album.Name, err)
}
// Retrieve the thumbnail
thumbnail, err := getThumbnail(album, param)
if err != nil {
return fmt.Errorf("Error retrieving thumbnail for album '%s': %v", album.Name, err)
}
// Send the message
link := param.ImmichURL + "/share/" + sharingKey
err = sendMessage(client, param.WhatsappGroup, album.Name, album.Description, fmt.Sprintf("Il y a %d an(s) : %s", time.Now().Year()-album.StartDate.Year(), link), link, thumbnail)
if err != nil {
fmt.Fprintf(os.Stderr, "Error sending message to WhatsApp for album '%s': %v\n", album.Name, err)
continue
}
} }
// Retrieve the thumbnail if param.DevelopmentMode == "run-last" {
thumbnail, err := getThumbnail(album, param) return nil
if err != nil {
return fmt.Errorf("Error retrieving thumbnail for album '%s': %v", album.Name, err)
} }
// Send the message // Get albums created yesterday
link := param.ImmichURL + "/share/" + sharingKey if album.CreatedAt.Year() == time.Now().AddDate(0, 0, -1).Year() && (album.CreatedAt.Month() == time.Now().AddDate(0, 0, -1).Month()) && (album.CreatedAt.Day() == time.Now().AddDate(0, 0, -1).Day()) {
err = sendMessage(client, param.WhatsappGroup, fmt.Sprintf("Il y a %d an(s) : %s", time.Now().Year()-album.StartDate.Year(), link), link, album.Name, thumbnail) // Retrieve the sharing key
if err != nil { sharingKey, err := getSharingKey(album, param)
fmt.Fprintf(os.Stderr, "Error sending message to WhatsApp for album '%s': %v\n", album.Name, err) if err != nil {
continue return fmt.Errorf("Error retrieving the sharing key for album '%s': %v", album.Name, err)
} }
}
// Get albums created yesterday // Retrieve the thumbnail
if album.Shared && (album.CreatedAt.Year() == time.Now().AddDate(0, 0, -1).Year()) && (album.CreatedAt.Month() == time.Now().AddDate(0, 0, -1).Month()) && (album.CreatedAt.Day() == time.Now().AddDate(0, 0, -1).Day()) { thumbnail, err := getThumbnail(album, param)
// Retrieve the sharing key if err != nil {
sharingKey, err := getSharingKey(album, param) return fmt.Errorf("Error retrieving thumbnail for album '%s': %v", album.Name, err)
if err != nil { }
return fmt.Errorf("Error retrieving the sharing key for album '%s': %v", album.Name, err)
}
// Retrieve the thumbnail // Send the message
thumbnail, err := getThumbnail(album, param) link := param.ImmichURL + "/share/" + sharingKey
if err != nil { err = sendMessage(client, param.WhatsappGroup, album.Name, album.Description, fmt.Sprintf("Nouvel album : %s", link), link, thumbnail)
return fmt.Errorf("Error retrieving thumbnail for album '%s': %v", album.Name, err) if err != nil {
fmt.Fprintf(os.Stderr, "Error sending message to WhatsApp for album '%s': %v\n", album.Name, err)
continue
}
} }
// Send the message
link := param.ImmichURL + "/share/" + sharingKey
err = sendMessage(client, param.WhatsappGroup, fmt.Sprintf("Nouvel album : %s", link), link, album.Name, thumbnail)
if err != nil {
fmt.Fprintf(os.Stderr, "Error sending message to WhatsApp for album '%s': %v\n", album.Name, err)
continue
}
} }
} }
@@ -426,13 +447,18 @@ func main() {
param.WhatsappGroup = os.Getenv("WHATSAPP-GROUP") param.WhatsappGroup = os.Getenv("WHATSAPP-GROUP")
param.TimeToRun = os.Getenv("TIME-TO-RUN") param.TimeToRun = os.Getenv("TIME-TO-RUN")
param.HealthchecksURL = os.Getenv("HEALTHCHECKS-URL") param.HealthchecksURL = os.Getenv("HEALTHCHECKS-URL")
_, param.RunOnce = os.LookupEnv("RUN-ONCE") param.DevelopmentMode = os.Getenv("DEVELOPMENT-MODE")
if param.RunOnce { if param.DevelopmentMode == "run-once" || param.DevelopmentMode == "run-last" {
err := runLoop(*param) err := runLoop(*param)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err) fmt.Fprintf(os.Stderr, "Error: %v\n", err)
} }
} else if param.DevelopmentMode == "listen" {
err := listen(*param)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
} else { } else {
// Test the connexion on startup // Test the connexion on startup
err := testConnexions(*param) err := testConnexions(*param)

10
immich-souvenirs/test.sh Executable file
View File

@@ -0,0 +1,10 @@
#!/bin/bash
echo "-----------------------------------------------------------------------"
echo "apk add --no-cache git gcc musl-dev"
echo "go mod init github.com/napnap75/multiarch-docker-files/immich-souvenirs"
echo "go mod tidy"
echo "env CGO_ENABLED=1 env DEVELOPMENT-MODE=run-once go run immich-souvenirs.go"
echo "-----------------------------------------------------------------------"
docker run -it -v $(pwd)/immich-souvenirs.go:/app/immich-souvenirs.go -w /app -v immich-souvenirs_config:/config --env-file test.env --rm golang:1.23-alpine /bin/sh