### Install GoTFY Dependency Source: https://github.com/anthonyhewins/gotfy/blob/master/README.md This command installs the GoTFY library as a dependency for your Go project using the go get command. Ensure you have Go installed and configured in your environment. ```shell go get github.com/AnthonyHewins/gotfy ``` -------------------------------- ### Send Notifications with Emoji Tags in Go Source: https://context7.com/anthonyhewins/gotfy/llms.txt Illustrates sending ntfy messages with emoji tags using the gotfy library. It shows how to utilize predefined emoji constants to add visual cues to notifications, enhancing their distinctiveness. The example includes setting a topic, message, title, and an array of tags. ```go package main import ( "context" "log" "net/url" "github.com/Anthony Hewins/gotfy" ) func main() { server, _ := url.Parse("https://ntfy.sh") publisher, _ := gotfy.NewPublisher(server) // Use predefined emoji constants for tags _, err := publisher.SendMessage(context.Background(), &gotfy.Message{ Topic: "deployments", Message: "Production deployment successful", Title: "Deploy Complete", Tags: []string{ gotfy.Rocket, // Deployment started gotfy.White_check_mark, // Success indicator gotfy.Tada, // Celebration }, }) if err != nil { log.Fatal(err) } // Common emoji constants available: // Status: gotfy.Warning, gotfy.X, gotfy.White_check_mark, gotfy.Exclamation // Objects: gotfy.Bell, gotfy.Fire, gotfy.Rocket, gotfy.Robot, gotfy.Computer // Faces: gotfy.Smile, gotfy.Thinking, gotfy.Sunglasses // See recognized_emojis.go for 1800+ emoji constants } ``` -------------------------------- ### Send Full-Featured Notification (Go) Source: https://context7.com/anthonyhewins/gotfy/llms.txt A comprehensive example showcasing various notification features including custom icons, delayed delivery, email forwarding, phone call triggers, file attachments via URL, and multiple action buttons (ViewAction and HttpAction). This requires the 'gotfy' library and standard Go packages for time and URL handling. ```go package main import ( "context" "log" "net/url" "time" "github.com/AnthonyHewins/gotfy" ) func main() { server, _ := url.Parse("https://ntfy.sh") publisher, _ := gotfy.NewPublisher(server, gotfy.WithAuth("user", "pass")) clickURL, _ := url.Parse("https://example.com/dashboard") iconURL, _ := url.Parse("https://example.com/icon.png") attachURL, _ := url.Parse("https://example.com/report.pdf") actionURL, _ := url.Parse("https://example.com/acknowledge") _, err := publisher.SendMessage(context.Background(), &gotfy.Message{ Topic: "critical-alerts", Message: "Database backup completed successfully. Click to view details.", Title: "Backup Complete", Tags: []string{gotfy.White_check_mark, gotfy.Floppy_disk, "backup"}, Priority: gotfy.High, // URL opened when notification is clicked ClickURL: clickURL, // Custom notification icon IconURL: iconURL, // Delay delivery by 5 minutes Delay: 5 * time.Minute, // Also send as email Email: "admin@example.com", // Trigger phone call (requires ntfy server with call support) Call: "+1234567890", // Attach a file via URL AttachURL: attachURL, AttachURLFilename: "backup-report.pdf", // Action buttons Actions: []gotfy.ActionButton{ &gotfy.ViewAction{ Label: "View Report", Link: clickURL, Clear: false, }, &gotfy.HttpAction[string]{ Label: "Acknowledge", URL: actionURL, Method: "POST", Clear: true, }, }, }) if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Create ntfy Publisher with Options (Go) Source: https://context7.com/anthonyhewins/gotfy/llms.txt Demonstrates creating a `Publisher` instance for sending ntfy notifications. Supports default HTTP clients, custom HTTP clients with timeouts, and basic authentication. Multiple functional options can be combined. ```go package main import ( "context" "fmt" "log" "net/http" "net/url" "time" "github.com/AnthonyHewins/gotfy" ) func main() { // Parse the ntfy server URL server, err := url.Parse("https://ntfy.sh") if err != nil { log.Fatal(err) } // Create a basic publisher with default HTTP client publisher, err := gotfy.NewPublisher(server) if err != nil { log.Fatal(err) } // Or create a publisher with custom HTTP client customClient := &http.Client{Timeout: 30 * time.Second} publisherWithClient, err := gotfy.NewPublisher(server, gotfy.WithHTTPClient(customClient)) if err != nil { log.Fatal(err) } // Or create a publisher with basic authentication publisherWithAuth, err := gotfy.NewPublisher(server, gotfy.WithAuth("username", "password")) if err != nil { log.Fatal(err) } // Combine multiple options publisherFull, err := gotfy.NewPublisher( server, gotfy.WithHTTPClient(customClient), gotfy.WithAuth("username", "password"), ) if err != nil { log.Fatal(err) } fmt.Println("Publishers created successfully") _ = publisher _ = publisherWithClient _ = publisherWithAuth _ = publisherFull } ``` -------------------------------- ### Go: Create NTFY Publisher and Send Message Source: https://github.com/anthonyhewins/gotfy/blob/master/README.md This Go code demonstrates how to create a publisher for the NTFY API and send a message. It shows how to initialize the publisher with default or custom HTTP clients, basic authentication, and various message options like topic, message content, title, tags, priority, actions, click URL, icon URL, delay, and email. ```go server, _ := url.Parse("https://server.com") // Create a publisher with default HTTP client tp, err := gotfy.NewPublisher(server) if err != nil { panic("bad config:"+err.Error()) } // Or with custom HTTP client customHTTPClient := http.DefaultClient tp, err := gotfy.NewPublisher(server, gotfy.WithHTTPClient(customHTTPClient)) // Or with basic authentication tp, err := gotfy.NewPublisher(server, gotfy.WithAuth("username", "password")) // Or combine multiple options tp, err := gotfy.NewPublisher( server, gotfy.WithHTTPClient(customHTTPClient), gotfy.WithAuth("username", "password"), ) if err != nil { panic("bad config:"+err.Error()) } pubResp, err := tp.SendMessage(&gotfy.Message{ Topic: "topic", Message: "message", Title: "title", Tags: []string{"emoji1","emoji2","some text"}, Priority: gotfy.High, Actions: []gotfy.ActionButton{ Label: "label", Link: "http://link.sh", Clear: true, }, ClickURL: "http://click.com", IconURL: "http://icon.com", Delay: time.Minute * 5, Email: "email@domain.com", }) if err != nil { panic("something happened "+err.Error()) } fmt.Println(pubResp) // Takes form of: // type PublishResp struct { // ID string `json:"id"` // :"bUhbhgmmbeW0" // Time int `json:"time"` // :1685150791 // Expires int `json:"expires"` // :1685193991 // Event string `json:"event"` // :"message" // Topic string `json:"topic"` // :"TopicName" // Message string `json:"message"` // :"triggered" // } ``` -------------------------------- ### Send Basic ntfy Message (Go) Source: https://context7.com/anthonyhewins/gotfy/llms.txt Shows how to send a simple notification using the `SendMessage` method of the `Publisher`. A topic name and message content are mandatory. The response includes message details like ID, timestamp, and expiration. ```go package main import ( "context" "fmt" "log" "net/url" "github.com/AnthonyHewins/gotfy" ) func main() { server, _ := url.Parse("https://ntfy.sh") publisher, _ := gotfy.NewPublisher(server) // Send a simple message resp, err := publisher.SendMessage(context.Background(), &gotfy.Message{ Topic: "my-alerts", Message: "Hello from gotfy!", Title: "Notification Title", }) if err != nil { log.Fatal(err) } fmt.Printf("Message sent successfully!\n") fmt.Printf("ID: %s\n", resp.ID) // e.g., "bUhbhgmmbeW0" fmt.Printf("Time: %d\n", resp.Time) // Unix timestamp fmt.Printf("Expires: %d\n", resp.Expires) // Expiration timestamp fmt.Printf("Event: %s\n", resp.Event) // "message" fmt.Printf("Topic: %s\n", resp.Topic) // "my-alerts" } ``` -------------------------------- ### Define and Use Priority Constants in Go Source: https://context7.com/anthonyhewins/gotfy/llms.txt Demonstrates how to use predefined priority constants provided by the gotfy library. These constants, of type int8, define message urgency levels affecting notification display, sound, and vibration. They are crucial for managing how alerts are perceived by recipients. ```go package main import ( "fmt" "github.com/Anthony Hewins/gotfy" ) func main() { // Priority is an int8 type with these predefined values: priorities := map[string]gotfy.Priority{ "UnspecifiedPriority": gotfy.UnspecifiedPriority, // 0 - Not set "Min": gotfy.Min, // 1 - Minimum "Low": gotfy.Low, // 2 - Low "Default": gotfy.Default, // 3 - Default "High": gotfy.High, // 4 - High "Max": gotfy.Max, // 5 - Maximum/Urgent } for name, priority := range priorities { fmt.Printf("%s: %d\n", name, priority) } // Usage in messages: // gotfy.Min - No sound, no vibration, lowest visibility // gotfy.Low - No sound, may vibrate // gotfy.Default - Default notification behavior // gotfy.High - Sound and vibration, higher visibility // gotfy.Max - Urgent, bypasses DND on some devices } ``` -------------------------------- ### Send Notification with View Action Button (Go) Source: https://context7.com/anthonyhewins/gotfy/llms.txt Demonstrates sending a notification with a 'ViewAction' button that opens a specified URL when clicked. This action can optionally clear the notification upon interaction. It requires the 'gotfy' library and standard Go packages for context, logging, and URL parsing. ```go package main import ( "context" "log" "net/url" "github.com/AnthonyHewins/gotfy" ) func main() { server, _ := url.Parse("https://ntfy.sh") publisher, _ := gotfy.NewPublisher(server) // Create a URL for the action button docsURL, _ := url.Parse("https://docs.ntfy.sh/publish/#icons") _, err := publisher.SendMessage(context.Background(), &gotfy.Message{ Topic: "updates", Message: "New documentation available", Title: "Documentation Update", Actions: []gotfy.ActionButton{ &gotfy.ViewAction{ Label: "View Docs", Link: docsURL, Clear: true, // Clears notification after clicking }, }, }) if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Send ntfy Message with Priority and Tags (Go) Source: https://context7.com/anthonyhewins/gotfy/llms.txt Illustrates sending an ntfy notification with custom priority levels and emoji tags. The library provides constants for priorities (Min to Max) and common emoji tags for enhanced message signaling. ```go package main import ( "context" "log" "net/url" "github.com/AnthonyHewins/gotfy" ) func main() { server, _ := url.Parse("https://ntfy.sh") publisher, _ := gotfy.NewPublisher(server) // Send a high-priority message with emoji tags _, err := publisher.SendMessage(context.Background(), &gotfy.Message{ Topic: "server-alerts", Message: "CPU usage exceeded 90%", Title: "Server Alert", Priority: gotfy.High, Tags: []string{gotfy.Warning, gotfy.Computer, "server01"}, }) if err != nil { log.Fatal(err) } // Available priority levels: // gotfy.Min (1) - Minimum priority // gotfy.Low (2) - Low priority // gotfy.Default (3) - Default priority // gotfy.High (4) - High priority // gotfy.Max (5) - Maximum priority // Example emoji tag constants: // gotfy.Warning, gotfy.Fire, gotfy.Rocket, gotfy.Bell // gotfy.White_check_mark, gotfy.X, gotfy.Robot // See recognized_emojis.go for the complete list } ``` -------------------------------- ### Send Notification with HTTP Action Button (Go) Source: https://context7.com/anthonyhewins/gotfy/llms.txt Illustrates sending a notification with an 'HttpAction' button that triggers an HTTP request to a specified URL when clicked. This is useful for invoking webhooks or server-side actions. The action can include custom headers, a request body, and optionally clear the notification after execution. It depends on the 'gotfy' library and standard Go packages. ```go package main import ( "context" "log" "net/url" "github.com/AnthonyHewins/gotfy" ) func main() { server, _ := url.Parse("https://ntfy.sh") publisher, _ := gotfy.NewPublisher(server) // URL to call when action is clicked webhookURL, _ := url.Parse("https://api.example.com/webhook") _, err := publisher.SendMessage(context.Background(), &gotfy.Message{ Topic: "home-automation", Message: "Motion detected at front door", Title: "Security Alert", Actions: []gotfy.ActionButton{ &gotfy.HttpAction[string]{ Label: "Dismiss Alert", URL: webhookURL, Method: "POST", Headers: map[string]string{ "Authorization": "Bearer token123", "Content-Type": "application/json", }, Body: `{"action": "dismiss", "alert_id": "12345"}`, Clear: true, }, }, }) if err != nil { log.Fatal(err) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.