Skip to content

Commit

Permalink
hello world
Browse files Browse the repository at this point in the history
  • Loading branch information
janyksteenbeek committed Nov 16, 2024
0 parents commit 81bc0c7
Show file tree
Hide file tree
Showing 17 changed files with 1,382 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
node_modules
Dockerfile*
docker-compose*
.dockerignore
.git
.github
.gitignore
README.md
LICENSE
.vscode
Makefile
helm-charts
.env
.editorconfig
.idea
coverage*
43 changes: 43 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Build and Push Container

on:
push:
branches:
- main
release:
types:
- published

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and Push Image
uses: docker/build-push-action@v5
with:
context: .
push: true
platforms: linux/amd64
tags: |
ghcr.io/${{ github.repository }}:latest
${{ github.event_name == 'release' && 'ghcr.io/${{ github.repository }}:${{ github.event.release.tag_name }}' || '' }}
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/.idea
.DS_Store
/dist
*.exe
*.exe~
*.dll
*.so
*.dylib
*.test
*.out
vendor/
go.work
config.yaml
credentials.json
birdgpt
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:1.23-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/birdgpt ./cmd/main.go

FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/birdgpt .
COPY config.yaml .
VOLUME ["/app/config.yaml", "/app/credentials.json"]
CMD ["/app/birdgpt"]
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2024 Janyk Steenbeek

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# BirdGPT

BirdGPT automatically processes invoices from your Gmail inbox and adds them to Moneybird. It uses GPT-4o to extract invoice details and can handle Dutch KVK and BTW numbers.


- Automatically monitors Gmail for new invoices
- Extracts invoice details using GPT-4o
- Creates contacts and purchase invoices in Moneybird
- Handles Dutch KVK and BTW numbers
- Automatically matches correct tax rates
- OAuth authentication for Gmail
- Configurable email label and check interval

[![GitHub release (latest by date)](https://img.shields.io/github/v/release/janyksteenbeek/birdgpt)](https://github.com/janyksteenbeek/birdgpt/releases)
[![GitHub](https://img.shields.io/github/license/janyksteenbeek/birdgpt)](LICENSE.md)
[![GitHub issues](https://img.shields.io/github/issues/janyksteenbeek/birdgpt)](https://github.com/janyksteenbeek/birdgpt/issues)

## Prerequisits

You need to request a few API keys to get started. Follow the steps below to get everything set up.

### 1. Moneybird

1. Go to [moneybird.com/user/applications/new](https://moneybird.com/user/applications/new)
2. Create a personal token
3. Note your administration ID from the URL when logged into Moneybird (format: /123456789)

### 2. Gmail

1. Go to [Google Cloud Console](https://console.cloud.google.com)
2. Create a new project
3. Enable Gmail API
4. Create OAuth 2.0 credentials
5. Download credentials and save as `credentials.json` in the project directory

### 3. OpenAI

1. Go to [platform.openai.com](https://platform.openai.com)
2. Create an API key


## Configuration

Create a `config.yaml` file in the project root. See `config.example.yaml` for all available options.

Required fields:
- `moneybird.token`: Your Moneybird personal token
- `moneybird.admin_id`: Your Moneybird administration ID
- `gmail.credentials_file`: Path to your Gmail OAuth credentials file
- `openai.api_key`: Your OpenAI API key


## Running

Build and run:

```bash
go build -o birdgpt cmd/main.go
./birdgpt
```

On first run:
1. You'll be prompted to visit a URL for Gmail authorization
2. After authorizing, copy the code and paste it back in the terminal
3. The application will start monitoring your emails

> I highly recommend testing the application with a separate Moneybird administration before using it with your actual administration. The application is still in development and may contain bugs. You can create a sandbox administration for free [here](https://moneybird.com/administrations/sandboxes/new).
## License

BirdGPT is released under the MIT License. See the [LICENSE](LICENSE) file for more details.

## Security

If you discover any security-related issues, please email [[email protected]](mailto:[email protected]) instead of using the
issue tracker. All security vulnerabilities will be promptly addressed.

## Disclaimer

This project is not affiliated with Moneybird, OpenAI or Google.
108 changes: 108 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"bufio"
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"

"github.com/janyksteenbeek/birdgpt/config"
"github.com/janyksteenbeek/birdgpt/internal/gmail"
"github.com/janyksteenbeek/birdgpt/internal/moneybird"
"github.com/janyksteenbeek/birdgpt/internal/openai"
"github.com/janyksteenbeek/birdgpt/internal/processor"
)

func main() {
log.Println("[github.com/janyksteenbeek/birdgpt]")
log.Println("Starting invoice processor...")

cfg, err := config.LoadConfig()
if err != nil {
log.Fatalf("Configuration error: %v", err)
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
setupGracefulShutdown(cancel)

log.Println("Initializing clients...")
gmailClient, err := initializeGmail(ctx, cfg)
if err != nil {
log.Fatalf("Gmail initialization failed: %v", err)
}

moneybirdClient, err := moneybird.NewClient(cfg.Moneybird.Token, cfg.Moneybird.AdminID)
if err != nil {
log.Fatalf("Moneybird initialization failed: %v", err)
}

openaiClient := openai.NewClient(cfg.OpenAI.APIKey)

log.Println("Testing connections...")
if err := testConnections(ctx, cfg, gmailClient, moneybirdClient); err != nil {
log.Fatalf("Connection test failed: %v", err)
}

proc := processor.New(cfg, gmailClient, moneybirdClient, openaiClient)
log.Printf("Invoice processor started. Checking for new emails every %v...", cfg.App.SleepTime)
if err := proc.Run(ctx); err != nil && err != context.Canceled {
log.Fatalf("Processor error: %v", err)
}
}

func testConnections(ctx context.Context, cfg *config.Config, gmail *gmail.Client, moneybird *moneybird.Client) error {
if _, err := gmail.FetchEmails(ctx, cfg.Gmail.SearchLabel, time.Now().Add(-time.Minute)); err != nil {
return fmt.Errorf("gmail test failed: %w", err)
}

if _, err := moneybird.SearchContacts("test"); err != nil {
return fmt.Errorf("moneybird test failed: %w", err)
}

return nil
}

func setupGracefulShutdown(cancel context.CancelFunc) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

go func() {
<-sigChan
log.Println("Cya later!")
cancel()
}()
}

func initializeGmail(ctx context.Context, cfg *config.Config) (*gmail.Client, error) {
client, authURL, err := gmail.Setup(ctx, cfg.Gmail.CredentialsFile, cfg.Gmail.Token)
if err != nil {
return nil, fmt.Errorf("gmail setup failed: %w", err)
}

if authURL != "" {
fmt.Printf("Visit this URL to authorize Gmail access:\n%s\n", authURL)
fmt.Print("Enter the authorization code: ")

scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()

token, err := gmail.Exchange(ctx, cfg.Gmail.CredentialsFile, scanner.Text())
if err != nil {
return nil, fmt.Errorf("token exchange failed: %w", err)
}

cfg.Gmail.Token = token
if err := config.SaveConfig(cfg); err != nil {
return nil, fmt.Errorf("saving config: %w", err)
}

return initializeGmail(ctx, cfg)
}

return client, nil
}
19 changes: 19 additions & 0 deletions config.yaml.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
moneybird:
client_id: ""
client_secret: ""
redirect_uri: "http://localhost:8080/callback"
token: ""
admin_id: ""

gmail:
credentials_file: "credentials.json"
token: ""
search_label: "Invoices"

openai:
api_key: ""

app:
last_update: "2024-01-01T00:00:00Z"
sleep_time: "5m"
trigger_word: "invoice"
Loading

0 comments on commit 81bc0c7

Please sign in to comment.