From ea784c8da23b9039fd3c67ef98e899f1b404aa3d Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 13 Jan 2026 11:21:36 +0100 Subject: [PATCH] feat: add action when mail received --- action/google.go | 10 ++++++ action/googleDriveNewFile.go | 9 ----- action/googleNewMail.go | 65 ++++++++++++++++++++++++++++++++++ authentification/googleAuth.go | 1 + templates/config.go | 14 ++++++++ 5 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 action/googleNewMail.go diff --git a/action/google.go b/action/google.go index 9a2ddbd..e5a2aa0 100644 --- a/action/google.go +++ b/action/google.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "net/http" "github.com/ValianceTekProject/AreaBack/db" "github.com/ValianceTekProject/AreaBack/initializers" @@ -39,3 +40,12 @@ func GetGoogleToken(ctx context.Context, config map[string]any) (string, string, } return actionID, googleToken, nil } + +type tokenTransport struct { + token string +} + +func (t *tokenTransport) RoundTrip(req *http.Request) (*http.Response, error) { + req.Header.Set("Authorization", "Bearer "+t.token) + return http.DefaultTransport.RoundTrip(req) +} diff --git a/action/googleDriveNewFile.go b/action/googleDriveNewFile.go index c28e5aa..1262326 100644 --- a/action/googleDriveNewFile.go +++ b/action/googleDriveNewFile.go @@ -64,12 +64,3 @@ func execGoogleDriveNewFileAction(token string, actionID string, ctx context.Con } } } - -type tokenTransport struct { - token string -} - -func (t *tokenTransport) RoundTrip(req *http.Request) (*http.Response, error) { - req.Header.Set("Authorization", "Bearer "+t.token) - return http.DefaultTransport.RoundTrip(req) -} diff --git a/action/googleNewMail.go b/action/googleNewMail.go new file mode 100644 index 0000000..73108c3 --- /dev/null +++ b/action/googleNewMail.go @@ -0,0 +1,65 @@ +package action + +import ( + "context" + "fmt" + "log" + "net/http" + "time" + + "github.com/ValianceTekProject/AreaBack/db" + "github.com/ValianceTekProject/AreaBack/initializers" + "google.golang.org/api/gmail/v1" + "google.golang.org/api/option" +) + +func ExecGmailNewEmail(config map[string]any) error { + ctx := context.Background() + + actionID, googleToken, err := GetGoogleToken(ctx, config) + + if err != nil { + return err + } + + if googleToken != "" { + execGmailNewEmailAction(googleToken, actionID, ctx) + } + return nil +} + +func execGmailNewEmailAction(token string, actionID string, ctx context.Context) { + httpClient := &http.Client{ + Transport: &tokenTransport{token: token}, + } + + gmailService, err := gmail.NewService(ctx, option.WithHTTPClient(httpClient)) + if err != nil { + fmt.Printf("Failed to create Gmail service: %v\n", err) + return + } + + since := time.Now().Add(-1 * time.Minute).Unix() + query := fmt.Sprintf("after:%d", since) + + messageList, err := gmailService.Users.Messages.List("me"). + Q(query). + MaxResults(100). + Do() + + if err != nil { + fmt.Printf("Failed to list messages: %v\n", err) + return + } + + if len(messageList.Messages) > 0 { + _, err := initializers.DB.Actions.FindUnique( + db.Actions.ID.Equals(actionID), + ).Update( + db.Actions.Triggered.Set(true), + ).Exec(ctx) + if err != nil { + log.Printf("Error updating action trigger: %v", err) + } + } +} diff --git a/authentification/googleAuth.go b/authentification/googleAuth.go index fca2681..28b59fc 100644 --- a/authentification/googleAuth.go +++ b/authentification/googleAuth.go @@ -28,6 +28,7 @@ func getGoogleOAuthConfig() *oauth2.Config { Scopes: []string{ "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/gmail.send", + "https://www.googleapis.com/auth/gmail.readonly", "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/calendar", "https://www.googleapis.com/auth/drive", diff --git a/templates/config.go b/templates/config.go index a077b83..8858fa1 100644 --- a/templates/config.go +++ b/templates/config.go @@ -193,6 +193,20 @@ var Services = map[string]*Service{ }, Handler: action.ExecGoogleDriveNewFile, }, + "google_new_mail": { + Name: "google_new_mail", + Description: "New mail received", + Service: "Google", + Config: []ActionField{ + { + Name: "channel_id", + Type: "text", + Label: "Channel ID", + Required: false, + }, + }, + Handler: action.ExecGmailNewEmail, + }, }, Reactions: map[string]*ReactionDefinition{ "google_Send_Email": {