-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (114 loc) · 4.29 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"log"
"net/smtp"
"regexp"
"strings"
"github.com/openai/openai-go/v2"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
)
func main() {
// Authenticate with the email server
auth := smtp.PlainAuth("", "user@example.com", "password", "mail.example.com")
// Connect to the email server
imapClient, err := client.DialTLS("mail.example.com:993", nil)
if err != nil {
log.Fatal(err)
}
defer imapClient.Logout()
// Login to the email server
if err := imapClient.Login("user@example.com", "password"); err != nil {
log.Fatal(err)
}
// Select the inbox mailbox
inbox, err := imapClient.Select("INBOX", false)
if err != nil {
log.Fatal(err)
}
// Define the search criteria for new messages
sinceDate := "01-Jan-2022" // Replace with the desired date
searchCriteria := imap.NewSearchCriteria().Since(sinceDate)
// Search for new messages in the inbox
messageSeqNums, err := inbox.Search(searchCriteria)
if err != nil {
log.Fatal(err)
}
// Iterate over the new messages and generate email replies
for _, seqNum := range messageSeqNums {
// Fetch the message
msg, err := inbox.Fetch(seqNum, imap.FetchEnvelope|imap.FetchBody)
if err != nil {
log.Fatal(err)
}
// Extract relevant information from the message
fromEmail := msg.Envelope.From[0].Address
fromName := msg.Envelope.From[0].PersonalName
subject := msg.Envelope.Subject
body := string(msg.Body)
// Check if the sender is in the user's contacts and generate the email reply using ChatGPT4
if senderInContacts(fromEmail) {
label := getLabelForSender(fromEmail)
if label != "" {
// Generate email reply using ChatGPT4
reply := generateReply(body, label, fromName)
// Send the email reply
to := []string{fromEmail}
subject := "Re: " + subject
body := reply
err := smtp.SendMail("mail.example.com:587", auth, "user@example.com", to, []byte("Subject: "+subject+"\r\n\r\n"+body))
if err != nil {
log.Fatal(err)
}
// Mark the message as answered
err = inbox.Store(seqNum, imap.FormatFlagsOp(imap.AddFlags, true), []interface{}{imap.AnsweredFlag})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Replied to message from %s <%s>\n", fromName, fromEmail)
}
}
}
}
// Helper function to check if the sender's email address is in the user's contacts
func senderInContacts(senderEmail string) bool {
// TODO: Implement a database or a file to store the user's contacts and their labels
return true
}
// Helper function to get the label or tag associated with the sender's email address
func getLabelForSender(senderEmail string) string {
// TODO: Implement a database or a // file to store the user's contacts and their labels
return "Friend"
}
// Helper function to generate the email reply using ChatGPT4
func generateReply(emailBody string, label string, senderName string) string {
client, err := openai.NewClient("YOUR_API_KEY_HERE")
if err != nil {
log.Fatal(err)
}
prompt := fmt.Sprintf("Dear %s,\n\nThank you for your email. ", senderName)
if label == "Friend" {
prompt += "How's your day going? "
} else if label == "Coworker" {
prompt += "Regarding your question, "
} else if label == "Family" {
prompt += "I hope everything is well with you and your family. "
}
prompt += "Here's my reply:\n\n"
// Remove quoted text and signature from the email body
re := regexp.MustCompile(`(?s)On .* wrote:(.*)$|--\n.*`)
emailBody = re.ReplaceAllString(emailBody, "")
// Generate email reply using ChatGPT4
response, err := client.Completions.Create(
&openai.CompletionRequest{
Model: "text-davinci-002",
Prompt: prompt + emailBody,
MaxTokens: 1024,
},
)
if err != nil {
log.Fatal(err)
}
return strings.TrimSpace(response.Choices[0].Text)
}