-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemails.go
107 lines (87 loc) · 2.62 KB
/
emails.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package gmail
import (
"context"
"encoding/base64"
"fmt"
"time"
"github.com/algo7/day-planner-gpt-data-portal/pkg/integrations"
"github.com/algo7/day-planner-gpt-data-portal/pkg/utils"
"google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
)
// GetEmails calls the Gmail API to get the user's emails.
func GetEmails() ([]integrations.Email, error) {
// Get the OAuth2 config
config, err := utils.GetOAuth2Config("google")
if err != nil {
return nil, err
}
// Get the token from redis
token, err := utils.RetrieveToken("google")
if err != nil {
return nil, err
}
// Create a new HTTP client and bind it to the token
client := config.Client(context.Background(), token)
// Create a new Gmail service client using the HTTP client
srv, err := gmail.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return nil, fmt.Errorf("Unable to retrieve Gmail client: %w", err)
}
// The current logged in user
user := "me"
// Get the current time
now := time.Now()
// Subtract 2 days from the current time
dateDiff := now.AddDate(0, 0, -2)
// Format the time in ISO 8601 format
dateDiffStr := dateDiff.Format("2006-01-02")
m, err := srv.Users.Messages.List(user).Q("is:unread").Q(fmt.Sprintf("after:%s", dateDiffStr)).Do()
if err != nil {
return nil, fmt.Errorf("Unable to retrieve messages: %w", err)
}
if len(m.Messages) == 0 {
fmt.Println("No messages found.")
return nil, err
}
// Get the content of each email
gmailEmails := []integrations.Email{}
for _, msg := range m.Messages {
c, err := srv.Users.Messages.Get(user, msg.Id).Do()
if err != nil {
return nil, fmt.Errorf("Unable to retrieve message: %w", err)
}
gmailEmails = append(gmailEmails, integrations.Email{
Subject: getHeader("Subject", c.Payload.Headers),
Body: getMessageBody(c.Payload),
Sender: getHeader("From", c.Payload.Headers),
RecievedDateTime: getHeader("Date", c.Payload.Headers),
})
}
return gmailEmails, nil
}
func getHeader(name string, headers []*gmail.MessagePartHeader) string {
for _, header := range headers {
if header.Name == name {
return header.Value
}
}
return ""
}
func getMessageBody(payload *gmail.MessagePart) string {
if payload.MimeType == "text/plain" {
data, _ := base64.URLEncoding.DecodeString(payload.Body.Data)
return string(data)
}
for _, part := range payload.Parts {
if part.MimeType == "text/plain" {
data, _ := base64.URLEncoding.DecodeString(part.Body.Data)
return string(data)
}
// Recursively check in nested parts
if len(part.Parts) > 0 {
return getMessageBody(part)
}
}
return ""
}