func setupCronIEPGDailyNotification(app *server.App) {
	var Cron = app.Cron()
	Cron.Define(
		"/records/notificatoin/daily/",
		"every day 08:00",
		"Notification for recordings today",
		func(req *wcg.Request) error {
			var hasError = false
			var email = configs.GetValue(req, "intern.pt.notification_email")
			messengerConfigs, err := getMessengerNotificationOptInUsers(req, "Summary")
			if err != nil {
				return err
			}

			content, err := createDailyNotificationContent(req)
			if err != nil {
				return err
			}

			if email == "" {
				req.Logger.Warnf(
					"No notification email address is specified (SiteConfig: %q)",
					"intern.pt.notification_email",
				)
			} else {
				err := notifications.NewEmail(
					email,
					content.title,
					strings.Join(content.lines, "\n"),
				).Deliver(req)
				if err != nil {
					req.Logger.Errorf("Failed to deliver an email notification: %v", err)
					hasError = true
				} else {
					req.Logger.Infof("Sent an email notification to %s", email)
				}
			}

			if len(messengerConfigs) == 0 {
				req.Logger.Infof("No messenger configuration is found. Skipped")
			}

			for _, cfg := range messengerConfigs {
				client := messenger.NewMessengerClient(req)
				client.UserID = cfg.UserID
				err := client.SendText(content.title + "\n" + strings.Join(content.lines, "\n"))
				if err != nil {
					req.Logger.Errorf("Failed to deliver a messenger notification: %v", err)
					hasError = true
				} else {
					req.Logger.Infof("Sent a messenger notification to %s", client.UserID)
				}
			}
			if hasError {
				return fmt.Errorf("Something wrong in daily notification cron. Check error logs.")
			}
			return nil
		},
	)
}
Example #2
0
func (email *EmailNotification) Deliver(req *wcg.Request) error {
	sender := configs.GetValue(req, "notifications.sender")
	if sender == "" {
		return fmt.Errorf("notifications.sender is empty.")
	}
	msg := &mail.Message{
		Sender:  sender,
		To:      email.To,
		Subject: email.Subject,
		Body:    email.Body,
	}
	if !lib.IsProduction() {
		msg.Subject = fmt.Sprintf("[Non-Prod]%s", msg.Subject)
	}
	if err := gae.SendMail(req, msg); err != nil {
		return err
	}
	if lib.IsOnLocalGAE() {
		req.Logger.Debugf("Email Content:\n%s", email.Body)
	}
	return nil
}
Example #3
0
package messenger

import (
	"fmt"
	"lib/server"
	"lib/server/configs"
	"lib/server/response"
	"lib/wcg"
)

// SetupHandler is a server.Handler to respond setup HTTP call from a Facebook server
var SetupHandler = server.Handler(func(req *wcg.Request) response.Response {
	token := configs.GetValue(req, "facebook_messenger_verify_token")
	if token == "" {
		return response.NotFound(req)
	}
	if req.Query("hub.verify_token") != token {
		return response.BadRequest(
			req,
			fmt.Errorf(
				"token mismatch: hub.verify_token(%q) != facebook_messenger_verify_token(%q)",
				req.Query("hub.verify_token"), token,
			),
		)
	}
	return response.NewTextResponse(req.Query("hub.challenge"))
})

// WebhookHandler is a server.Handler to respond setup HTTP call from a Facebook server
var WebHookHandler = server.Handler(func(req *wcg.Request) response.Response {
	messages, perr := parseMessage(req.HTTPRequest().Body)