From e85549fe84c3972e4fe8f7d3cd2b0e66a7dda048 Mon Sep 17 00:00:00 2001 From: Jaakko Koistinen Date: Tue, 18 Feb 2025 20:16:35 +0100 Subject: [PATCH] initial commit --- .gitignore | 7 ++++++ README.md | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++++ main.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 go.mod create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b02a6b9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +/.env +/go.sum +/.idea/vcs.xml +/.idea/modules.xml +/go-google-space +/.idea/go-google-space.iml +.idea/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4c31a8b --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +# Go Google Space Bot + +A simple Go application to send messages to a Google Chat space using its webhook URL. + +## Prerequisites + +To use this application, ensure you have the following installed: +1. Go version `1.23` or later. +2. A properly configured `.env` file with the following variables: + - `SPACE_ID`: The ID of the Google Chat space you want to send messages to. + - `KEY`: The key provided in your Google Chat webhook URL. + - `TOKEN`: The token provided in your Google Chat webhook URL. + +For more info on setting up Google Chat webhooks, see [Sending Chat messages using webhooks](https://developers.google.com/chat/how-tos/webhooks). + +## Installation + +Clone this repository, navigate into the directory, and build the project: + +```bash +git clone +cd go-google-space +go build -o go-google-space +``` + +## Configuration + +Create a `.env` file in the root directory with the following contents: + +```dotenv +SPACE_ID= +KEY= +TOKEN= +``` + +Replace ``, ``, and `` with the respective values from your Google Chat webhook URL. + +## Usage + +Run the built executable, passing the message you want to send as an argument: + +```bash +./go-google-space "Hello, Google Chat!" +``` + +## Example Output + +When the command runs successfully, the message will appear in your configured Google Chat space. + +If any errors occur (e.g., if the `.env` file is missing or the webhook URL configuration is incorrect), error messages will be displayed in the terminal. + +## Dependencies + +This project uses the following dependencies: +- [godotenv](https://github.com/joho/godotenv) for loading environment variables from a `.env` file. + +To install the dependency, run: + +```bash +go get github.com/joho/godotenv +``` + +## License + +This project is licensed under the MIT License. See the LICENSE file for details. + +--- \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1ad4a55 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module go-google-space + +go 1.23 + +require github.com/joho/godotenv v1.5.1 diff --git a/main.go b/main.go new file mode 100644 index 0000000..6847bb4 --- /dev/null +++ b/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "github.com/joho/godotenv" + "io" + "net/http" + "os" +) + +func main() { + + if len(os.Args) < 2 { + fmt.Println("Usage: ./go-google-space ") + return + } + userMessage := os.Args[1] + + client := &http.Client{} + + err := godotenv.Load() + if err != nil { + fmt.Println("Error loading .env file") + } + + chatSpaceID := os.Getenv("SPACE_ID") + key := os.Getenv("KEY") + token := os.Getenv("TOKEN") + url := fmt.Sprintf("https://chat.googleapis.com/v1/spaces/%s/messages?key=%s&token=%s", chatSpaceID, key, token) + + message := map[string]string{"text": userMessage} + messageJson, err := json.Marshal(message) + if err != nil { + fmt.Println("Error marshalling message") + } + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(messageJson)) + if err != nil { + fmt.Println("Error creating request") + return + } + + req.Header.Add("Content-Type", "application/json; charset=UTF-8") + + resp, err := client.Do(req) + if err != nil { + fmt.Println("Error sending request") + return + } + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + fmt.Println("Error closing response body") + } + }(resp.Body) +}