-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgcalendar.go
61 lines (48 loc) · 1.48 KB
/
gcalendar.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
package gcalendar
import (
"context"
"fmt"
"log"
"github.com/cpanato/mattermost-away-reminder/model"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
func AddEventToGCal(userName, text, fromDate, toDate, calendarId, calendarAPIKey string) (string, error) {
calendarService, err := calendar.NewService(context.Background(), option.WithAPIKey(calendarAPIKey))
if err != nil {
log.Fatalf("Unable to create Calendar service: %v", err)
}
fmt.Println("Adding one Event")
msg := fmt.Sprintf("%v: %v", userName, text)
eventToSave := &calendar.Event{
Summary: msg,
Description: msg,
Start: &calendar.EventDateTime{
Date: fromDate,
},
End: &calendar.EventDateTime{
Date: toDate,
},
}
event, err := calendarService.Events.Insert(calendarId, eventToSave).Do()
if err != nil {
log.Fatalf("Unable to create event. %v\n", err)
}
fmt.Printf("Event created: %s\n", event.Id)
return event.Id, nil
}
func RemoveEventFromGCal(id, calendarId, calendarAPIKey string) error {
calendarService, err := calendar.NewService(context.Background(), option.WithAPIKey(calendarAPIKey))
if err != nil {
log.Fatalf("Unable to create Calendar service: %v", err)
}
fmt.Println("Removing Event")
event := calendarService.Events.Delete(calendarId, id).Do()
if event != nil {
msg := fmt.Sprintf("Unable to delete event. %v", event)
fmt.Println(msg)
return model.NewLocAppError("Mattermost Time Away", msg, nil, "")
}
fmt.Printf("Event Deleted\n")
return nil
}