// NotifyUpcomingEvents sends one message to slack
// containing all of the events that are scheduled to happen
// in the next `delta` amount of time, starting at `t`
NotifyUpcomingEvents sends one message to slack
containing all of the events that are scheduled to happen
in the next `delta` amount of time, starting at `t`
// NotifyUpcomingEvents sends one message to slack // containing all of the events that are scheduled to happen // in the next ` delta ` amount of time starting at ` t `
func (b *Bot) NotifyUpcomingEvents(ctx context.Context, t time.Time, delta time.Duration) error {
	s, err := b.CalendarService(ctx)
	if err != nil {
		return errors.Wrap(err, "failed to create calendar service")
	}
	start := t.Format(time.RFC3339)
	end := t.Add(delta).Format(time.RFC3339)
	events, err := s.Events.
		List(b.CalendarName).
		TimeMin(start).
		TimeMax(end).
		SingleEvents(true).
		OrderBy("startTime").
		Do()
	if err != nil {
		return errors.Wrap(err, "failed to list events")
	}
	if len(events.Items) == 0 {
		return nil
	}
	buf := bytes.Buffer{}
	fields := make([]slack.AttachmentField, len(events.Items))
	for i, event := range events.Items {
		t1, err := time.Parse(time.RFC3339, event.Start.DateTime)
		if err != nil {
			return errors.Wrap(err, "failed to parse start date/time")
		}
		t2, err := time.Parse(time.RFC3339, event.End.DateTime)
		if err != nil {
			return errors.Wrap(err, "failed to parse end date/time")
		}
		buf.Reset()
		fmt.Fprintf(&buf, "%s-%s: <%s|%s>", t1.Format("15:04"), t2.Format("15:04"), event.HtmlLink, event.Summary)
		fields[i] = slack.AttachmentField{
			Value: buf.String(),
		}
	}
	buf.Reset()
	fmt.Fprintf(&buf, "Upcoming events between %s to %s", t.Format("2006 Jan 02 15:04"), t.Add(delta).Format("2006 Jan 02 15:04"))
	params := slack.NewPostMessageParameters()
	params.Username = b.SlackUsername
	params.Attachments = []slack.Attachment{
		slack.Attachment{
			Fallback: buf.String(),
			Fields:   fields,
			ThumbURL: b.SlackThumbURL,
			Title:    buf.String(),
		},
	}
	return errors.Wrap(postSlack(ctx, b.SlackToken, b.SlackChannel, "", &params), "failed to post message to slack")
}
func ( b * Bot ) NotifyUpcomingEvents ( ctx context . Context , t time . Time , delta time . Duration ) error { s , err := b . CalendarService ( ctx ) if err != nil { return errors . Wrap ( err , " " ) } start := t . Format ( time . RFC3339 ) end := t . Add ( delta ) . Format ( time . RFC3339 ) events , err := s . Events . List ( b . CalendarName ) . TimeMin ( start ) . TimeMax ( end ) . SingleEvents ( true ) . OrderBy ( " " ) . Do ( ) if err != nil { return errors . Wrap ( err , " " ) } if len ( events . Items ) == 0 { return nil } buf := bytes . Buffer { } fields := make ( [ ] slack . AttachmentField , len ( events . Items ) ) for i , event := range events . Items { t1 , err := time . Parse ( time . RFC3339 , event . Start . DateTime ) if err != nil { return errors . Wrap ( err , " " ) } t2 , err := time . Parse ( time . RFC3339 , event . End . DateTime ) if err != nil { return errors . Wrap ( err , " " ) } buf . Reset ( ) fmt . Fprintf ( & buf , " " , t1 . Format ( " " ) , t2 . Format ( " " ) , event . HtmlLink , event . Summary ) fields [ i ] = slack . AttachmentField { Value : buf . String ( ) , } } buf . Reset ( ) fmt . Fprintf ( & buf , " " , t . Format ( " " ) , t . Add ( delta ) . Format ( " " ) ) params := slack . NewPostMessageParameters ( ) params . Username = b . SlackUsername params . Attachments = [ ] slack . Attachment { slack . Attachment { Fallback : buf . String ( ) , Fields : fields , ThumbURL : b . SlackThumbURL , Title : buf . String ( ) , } , } return errors . Wrap ( postSlack ( ctx , b . SlackToken , b . SlackChannel , " " , & params ) , " " ) }
NotifyUpcomingEvents sends one message to slack
containing all of the events that are scheduled to happen
in the next `delta` amount of time, starting at `t`
NotifyUpcomingEvents sends one message to slack containing all of the events that are scheduled to happen in the next ` delta ` amount of time starting at ` t `
// Collect events that are due in the given time frame // Nothing to do // Create a message containing all events for the day