diff --git a/CHANGELOG.md b/CHANGELOG.md index ccc6970..d375227 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,15 @@ and this project adheres to [Semantic Versioning][]. ### Removed --> +## [0.1.3][] - 2025-01-17 + +### Removed + +* `utils/notify` to separate project + [github.com/woozymasta/notify](https://github.com/WoozyMasta/notify) + +[0.1.3]: https://github.com/WoozyMasta/steam/compare/v0.1.2...v0.1.3 + ## [0.1.2][] - 2025-01-12 ### Added diff --git a/README.md b/README.md index 0128c4d..f13787c 100644 --- a/README.md +++ b/README.md @@ -17,13 +17,9 @@ servers, and more. See each package’s README for detailed information. * **[utils/latest]** Offers a threshold-based version selection mechanism, helpful for automatically updating Steam-based game servers. -* **[utils/notify]** - Provides functionality to send, edit, and delete notifications - in various messaging platforms, including Discord and Telegram. [filedetails]: ./filedetails/README.md [serverlist]: ./serverlist/README.md [utils/appid]: ./utils/appid/README.md [utils/latest]: ./utils/latest/README.md -[utils/notify]: ./utils/notify/README.md diff --git a/utils/notify/README.md b/utils/notify/README.md deleted file mode 100644 index a78c29c..0000000 --- a/utils/notify/README.md +++ /dev/null @@ -1,115 +0,0 @@ -# Notify - -The `notify` package provides an easy-to-use interface for sending, editing, -and deleting notifications across multiple messaging platforms, including -Discord and Telegram. It abstracts the underlying API interactions, allowing -developers to integrate messaging functionalities seamlessly into their Go -applications. - -## Features - -* **Discord Integration**: - Send, edit, and delete messages via Discord webhooks. -* **Telegram Integration**: - Send, edit, and delete messages using the Telegram Bot API. -* **Unified Interface**: - Consistent methods for different platforms. -* **Markdown Support**: - Utilize Markdown for message formatting where supported. - -## Installation - -To install the `notify` package, use `go get`: - -```bash -go get github.com/woozymasta/steam -``` - -## Usage - -### Discord - -```go -package main - -import ( - "fmt" - "log" - - "github.com/woozymasta/steam/utils/notify" -) - -func main() { - // Initialize Discord notifier - discordWebhookID := "your_discord_webhook_id" - discordWebhookToken := "your_discord_webhook_token" - discordClient := notify.NewDiscord(discordWebhookID, discordWebhookToken) - - // Send a message - messageID, err := discordClient.Send("Hello, **Discord**!") - if err != nil { - log.Fatalf("Failed to send Discord message: %v", err) - } - - // Edit the message - if err = discordClient.Edit(messageID, "Bye, *Discord*!"); err != nil { - log.Fatalf("Failed to edit Discord message: %v", err) - } - - // Delete the message - if err = discordClient.Delete(messageID); err != nil { - log.Fatalf("Failed to delete Discord message: %v", err) - } -} -``` - -### Telegram - -```go -package main - -import ( - "fmt" - "log" - - "github.com/woozymasta/steam/utils/notify" -) - -func main() { - // Initialize Telegram notifier - telegramBotToken := "your_telegram_bot_token" - telegramChatID := "your_telegram_chat_id" - telegramClient := notify.NewTelegram(telegramBotToken, telegramChatID) - - // Send a message - messageID, err := telegramClient.Send("Hello, *Telegram*!") - if err != nil { - log.Fatalf("Failed to send Telegram message: %v", err) - } - - // Edit the message - if err = telegramClient.Edit(messageID, "Bye, *Telegram*!"); err != nil { - log.Fatalf("Failed to edit Telegram message: %v", err) - } - - // Delete the message - if err = telegramClient.Delete(messageID); err != nil { - log.Fatalf("Failed to delete Telegram message: %v", err) - } -} -``` - -## Testing - -Ensure you have the necessary environment variables set before running tests: - -* `DISCORD_ID`: Discord webhook ID. -* `DISCORD_TOKEN`: Discord webhook token. -* `TELEGRAM_ID`: Telegram chat ID. -* `TELEGRAM_TOKEN`: Telegram bot token. - -Run the tests using: - -```bash -go test ./... -``` diff --git a/utils/notify/discord.go b/utils/notify/discord.go deleted file mode 100644 index 0e1b125..0000000 --- a/utils/notify/discord.go +++ /dev/null @@ -1,164 +0,0 @@ -package notify - -import ( - "bytes" - "fmt" - "net/http" - - json "github.com/json-iterator/go" -) - -// Discord represents a client for interacting with Discord webhooks. -type Discord struct { - webhookID string - webhookToken string -} - -// NewDiscord creates a new instance of a Discord client. -// -// Parameters: -// - webhookID: The Discord webhook ID. -// - webhookToken: The Discord webhook token. -// -// Returns: -// - A pointer to a Discord instance. -func NewDiscord(webhookID, webhookToken string) *Discord { - return &Discord{ - webhookID: webhookID, - webhookToken: webhookToken, - } -} - -// discordMessage represents the structure of a message sent to Discord via webhook. -type discordMessage struct { - Content string `json:"content"` - Username string `json:"username,omitempty"` - Avatar string `json:"avatar_url,omitempty"` -} - -// Send sends a message to a Discord channel using the webhook. -// -// Parameters: -// - msg: The message content to send. -// -// Returns: -// - The ID of the sent message. -// - An error if the operation fails. -func (d *Discord) Send(msg string) (uint64, error) { - discordMsg := discordMessage{ - Content: msg, - } - - data, err := json.Marshal(discordMsg) - if err != nil { - return 0, err - } - - resp, err := http.Post( - fmt.Sprintf("https://discord.com/api/webhooks/%s/%s?wait=true", d.webhookID, d.webhookToken), - "application/json", - bytes.NewBuffer(data), - ) - if err != nil { - return 0, err - } - defer closeBody(resp) - - if resp.StatusCode >= 400 { - return 0, fmt.Errorf("failed to send message, status code: %d", resp.StatusCode) - } - - var result struct { - ID string `json:"id"` - } - - if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { - return 0, err - } - - if result.ID == "" { - return 0, fmt.Errorf("failed to get message ID") - } - - // Convert string ID to uint64 - var messageID uint64 - _, err = fmt.Sscanf(result.ID, "%d", &messageID) - if err != nil { - return 0, fmt.Errorf("invalid message ID format: %v", err) - } - - return messageID, nil -} - -// Edit modifies an existing message in a Discord channel. -// -// Parameters: -// - id: The ID of the message to edit. -// - msg: The new message content. -// -// Returns: -// - An error if the operation fails. -func (d *Discord) Edit(id uint64, msg string) error { - discordMsg := discordMessage{ - Content: msg, - } - - data, err := json.Marshal(discordMsg) - if err != nil { - return err - } - - req, err := http.NewRequest( - http.MethodPatch, - fmt.Sprintf("https://discord.com/api/webhooks/%s/%s/messages/%d", d.webhookID, d.webhookToken, id), - bytes.NewBuffer(data), - ) - if err != nil { - return err - } - req.Header.Set("Content-Type", "application/json") - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - return err - } - defer closeBody(resp) - - if resp.StatusCode >= 400 { - return fmt.Errorf("failed to edit message, status code: %d", resp.StatusCode) - } - - return nil -} - -// Delete removes an existing message from a Discord channel. -// -// Parameters: -// - id: The ID of the message to delete. -// -// Returns: -// - An error if the operation fails. -func (d *Discord) Delete(id uint64) error { - req, err := http.NewRequest( - http.MethodDelete, - fmt.Sprintf("https://discord.com/api/webhooks/%s/%s/messages/%d", d.webhookID, d.webhookToken, id), - nil, - ) - if err != nil { - return err - } - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - return err - } - defer closeBody(resp) - - if resp.StatusCode >= 400 { - return fmt.Errorf("failed to delete message, status code: %d", resp.StatusCode) - } - - return nil -} diff --git a/utils/notify/notify.go b/utils/notify/notify.go deleted file mode 100644 index 63f96a0..0000000 --- a/utils/notify/notify.go +++ /dev/null @@ -1,14 +0,0 @@ -// Package notify provides functionality to send, edit, and delete notifications -// in various messaging platforms, including Discord and Telegram. -package notify - -import ( - "fmt" - "net/http" -) - -func closeBody(resp *http.Response) { - if err := resp.Body.Close(); err != nil { - fmt.Printf("Error close response body: %v", err) - } -} diff --git a/utils/notify/notify_test.go b/utils/notify/notify_test.go deleted file mode 100644 index ce3efe9..0000000 --- a/utils/notify/notify_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package notify - -import ( - "os" - "testing" - "time" -) - -// TestSendEditDelete tests the Send, Edit, and Delete functions for both Discord and Telegram. -func TestSendEditDelete(t *testing.T) { - discordID, ok := os.LookupEnv("DISCORD_ID") - if !ok { - t.Error("Discord webhook ID must be set in the environment variable 'DISCORD_ID'") - } - discordToken, ok := os.LookupEnv("DISCORD_TOKEN") - if !ok { - t.Error("Discord webhook token must be set in the environment variable 'DISCORD_TOKEN'") - } - - telegramID, ok := os.LookupEnv("TELEGRAM_ID") - if !ok { - t.Error("Telegram chat ID must be set in the environment variable 'TELEGRAM_ID'") - } - telegramToken, ok := os.LookupEnv("TELEGRAM_TOKEN") - if !ok { - t.Error("Telegram bot token must be set in the environment variable 'TELEGRAM_TOKEN'") - } - - msg := "This is a **bold**, _italic_, [inline URL](http://www.example.com/), `code`." - - discordClient := NewDiscord(discordID, discordToken) - telegramClient := NewTelegram(telegramToken, telegramID) - - // Send messages - dcID, err := discordClient.Send(msg) - if err != nil { - t.Errorf("Failed to send Discord message: %v", err) - } - tgID, err := telegramClient.Send(msg) - if err != nil { - t.Errorf("Failed to send Telegram message: %v", err) - } - - // Wait for messages to be sent - time.Sleep(3 * time.Second) - - // Edit messages - if err := discordClient.Edit(dcID, "Redacted Discord Message"); err != nil { - t.Errorf("Failed to edit Discord message: %v", err) - } - if err := telegramClient.Edit(tgID, "Redacted Telegram Message"); err != nil { - t.Errorf("Failed to edit Telegram message: %v", err) - } - - // Wait for messages to be edited - time.Sleep(3 * time.Second) - - // Delete messages - if err := discordClient.Delete(dcID); err != nil { - t.Errorf("Failed to delete Discord message: %v", err) - } - if err := telegramClient.Delete(tgID); err != nil { - t.Errorf("Failed to delete Telegram message: %v", err) - } -} diff --git a/utils/notify/telegram.go b/utils/notify/telegram.go deleted file mode 100644 index f9fdc6f..0000000 --- a/utils/notify/telegram.go +++ /dev/null @@ -1,183 +0,0 @@ -package notify - -import ( - "bytes" - "fmt" - "net/http" - - json "github.com/json-iterator/go" -) - -// Telegram represents a client for interacting with the Telegram Bot API. -type Telegram struct { - botToken string - chatID string -} - -// NewTelegram creates a new instance of a Telegram client. -// -// Parameters: -// - botToken: The Telegram bot token. -// - chatID: The chat ID where messages will be sent. -// -// Returns: -// - A pointer to a Telegram instance. -func NewTelegram(botToken, chatID string) *Telegram { - return &Telegram{ - botToken: botToken, - chatID: chatID, - } -} - -// tgPayload represents the payload structure for Telegram API requests. -type tgPayload struct { - ChatID string `json:"chat_id"` - Text string `json:"text"` - ParseMode string `json:"parse_mode,omitempty"` - MessageID uint64 `json:"message_id,omitempty"` -} - -// Send sends a message to a Telegram chat. -// -// Parameters: -// - msg: The message content to send. -// -// Returns: -// - The ID of the sent message. -// - An error if the operation fails. -func (t *Telegram) Send(msg string) (uint64, error) { - payload := tgPayload{ - ChatID: t.chatID, - Text: msg, - ParseMode: "Markdown", - } - - data, err := json.Marshal(payload) - if err != nil { - return 0, err - } - - resp, err := http.Post( - fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", t.botToken), - "application/json", - bytes.NewBuffer(data), - ) - if err != nil { - return 0, err - } - defer closeBody(resp) - - if resp.StatusCode >= 400 { - return 0, fmt.Errorf("failed to send message, status code: %d", resp.StatusCode) - } - - var result struct { - Ok bool `json:"ok"` - Result struct { - MessageID uint64 `json:"message_id"` - } `json:"result"` - } - - if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { - return 0, err - } - - if !result.Ok { - return 0, fmt.Errorf("failed to send message") - } - - if result.Result.MessageID == 0 { - return 0, fmt.Errorf("failed to get message ID") - } - - return result.Result.MessageID, nil -} - -// Edit modifies an existing message in a Telegram chat. -// -// Parameters: -// - id: The ID of the message to edit. -// - msg: The new message content. -// -// Returns: -// - An error if the operation fails. -func (t *Telegram) Edit(id uint64, msg string) error { - payload := tgPayload{ - ChatID: t.chatID, - MessageID: id, - Text: msg, - ParseMode: "Markdown", - } - - data, err := json.Marshal(payload) - if err != nil { - return err - } - - resp, err := http.Post( - fmt.Sprintf("https://api.telegram.org/bot%s/editMessageText", t.botToken), - "application/json", - bytes.NewBuffer(data), - ) - if err != nil { - return err - } - defer closeBody(resp) - - var result struct { - Ok bool `json:"ok"` - } - - if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { - return err - } - - if !result.Ok { - return fmt.Errorf("failed to edit message") - } - - return nil -} - -// Delete removes an existing message from a Telegram chat. -// -// Parameters: -// - id: The ID of the message to delete. -// -// Returns: -// - An error if the operation fails. -func (t *Telegram) Delete(id uint64) error { - payload := tgPayload{ - ChatID: t.chatID, - MessageID: id, - } - - data, err := json.Marshal(payload) - if err != nil { - return err - } - - resp, err := http.Post( - fmt.Sprintf("https://api.telegram.org/bot%s/deleteMessage", t.botToken), - "application/json", - bytes.NewBuffer(data), - ) - if err != nil { - return err - } - defer closeBody(resp) - - var result struct { - Ok bool `json:"ok"` - } - - if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { - return err - } - - if !result.Ok { - return fmt.Errorf("failed to delete message") - } - - return nil -}