-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(MicrosoftAPI): Action received email -> Reaction mark as read
- Loading branch information
Showing
5 changed files
with
212 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package microsoft | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"service/models" | ||
"service/utils" | ||
) | ||
|
||
type APIClient struct { | ||
Token string | ||
} | ||
|
||
func (c *APIClient) ValidateToken() error { | ||
// Add token validation logic here | ||
return nil | ||
} | ||
|
||
func (c *APIClient) ProcessWorkflowEvent(workflowID, eventID uint) error { | ||
var event models.Event | ||
if err := utils.DB.First(&event, eventID).Error; err != nil { | ||
return fmt.Errorf("event not found: %w", err) | ||
} | ||
|
||
log.Printf("Processing workflow event in Microsoft API: workflowID=%d, eventID=%d, eventName=%s, eventType=%s", | ||
workflowID, eventID, event.Name, event.Type) | ||
|
||
switch event.Type { | ||
case models.ActionEventType: | ||
|
||
switch event.Name { | ||
case "New Email Received": | ||
return c.HandleNewEmailReceived(workflowID) | ||
/*case "File uploaded": | ||
return c.HandleFileUploaded(workflowID)*/ | ||
} | ||
|
||
case models.ReactionEventType: | ||
/*if event.Name == "Send an Email" { | ||
return c.SendEmail(workflowID, eventID) | ||
} else if event.Name == "Upload a file" { | ||
return c.UploadFileToOneDrive(workflowID, eventID) | ||
}*/ | ||
} | ||
|
||
return fmt.Errorf("unhandled event type: %s (%s)", event.Type, event.Name) | ||
} | ||
|
||
func InitMicrosoftAPI(token string) (*APIClient, error) { | ||
client := &APIClient{ | ||
Token: token, | ||
} | ||
|
||
if err := client.ValidateToken(); err != nil { | ||
return nil, fmt.Errorf("invalid token: %w", err) | ||
} | ||
|
||
return client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package microsoft | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type MailMessage struct { | ||
ID string `json:"id"` | ||
Subject string `json:"subject"` | ||
ReceivedDateTime string `json:"receivedDateTime"` | ||
IsRead bool `json:"isRead"` | ||
From struct { | ||
EmailAddress struct { | ||
Name string `json:"name"` | ||
Address string `json:"address"` | ||
} `json:"emailAddress"` | ||
} `json:"from"` | ||
} | ||
|
||
type MailListResponse struct { | ||
Value []MailMessage `json:"value"` | ||
} | ||
|
||
func (c *APIClient) HandleNewEmailReceived(workflowID uint) error { | ||
endpoint := "https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages?$filter=isRead%20eq%20false&$top=10&$orderby=receivedDateTime%20desc" | ||
|
||
req, err := http.NewRequest("GET", endpoint, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to create request: %w", err) | ||
} | ||
req.Header.Set("Authorization", "Bearer "+c.Token) | ||
req.Header.Set("Accept", "application/json") | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to retrieve emails: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
bodyBytes, _ := ioutil.ReadAll(resp.Body) | ||
return fmt.Errorf("non-200 response: %d, body: %s", resp.StatusCode, string(bodyBytes)) | ||
} | ||
|
||
var mailList MailListResponse | ||
if err := json.NewDecoder(resp.Body).Decode(&mailList); err != nil { | ||
return fmt.Errorf("failed to decode mail response: %w", err) | ||
} | ||
|
||
if len(mailList.Value) == 0 { | ||
log.Println("No new unread emails found.") | ||
return nil | ||
} | ||
|
||
for _, message := range mailList.Value { | ||
log.Printf("New Email Received: Subject=%q, From=%q, ReceivedAt=%s", | ||
message.Subject, message.From.EmailAddress.Address, message.ReceivedDateTime) | ||
|
||
if err := c.MarkEmailAsRead(message.ID); err != nil { | ||
log.Printf("Failed to mark email %s as read: %v", message.ID, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *APIClient) MarkEmailAsRead(messageID string) error { | ||
endpoint := fmt.Sprintf("https://graph.microsoft.com/v1.0/me/messages/%s", messageID) | ||
reqBody := `{"isRead": true}` | ||
req, err := http.NewRequest("PATCH", endpoint, strings.NewReader(reqBody)) | ||
if err != nil { | ||
return fmt.Errorf("failed to create patch request: %w", err) | ||
} | ||
|
||
req.Header.Set("Authorization", "Bearer "+c.Token) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to send PATCH request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
bodyBytes, _ := ioutil.ReadAll(resp.Body) | ||
return fmt.Errorf("failed to mark email as read, status: %d, body: %s", resp.StatusCode, string(bodyBytes)) | ||
} | ||
|
||
log.Printf("Email %s marked as read.", messageID) | ||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.