initial commit

This commit is contained in:
Jaakko Koistinen 2025-02-18 20:16:35 +01:00
commit e85549fe84
4 changed files with 137 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
/.env
/go.sum
/.idea/vcs.xml
/.idea/modules.xml
/go-google-space
/.idea/go-google-space.iml
.idea/

67
README.md Normal file
View File

@ -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 <repository-url>
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=<your-space-id>
KEY=<your-key>
TOKEN=<your-token>
```
Replace `<your-space-id>`, `<your-key>`, and `<your-token>` 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.
---

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module go-google-space
go 1.23
require github.com/joho/godotenv v1.5.1

58
main.go Normal file
View File

@ -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 <message>")
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)
}