Пример #1
0
func pushHandler(
	item *db.PendingItem,
	text string,
	settings map[string]interface{},
) {
	log.Println("Sending Push Notification...")

	application_id, ok := settings["parse_application_id"].(string)
	if !ok {
		log.Println("parse_application_id is a required parameter.")
		return
	}

	api_key, ok := settings["parse_rest_api_key"].(string)
	if !ok {
		log.Println("parse_rest_api_key is a required parameter.")
		return
	}

	subject, ok := item.Context["subject"].(string)
	if !ok || subject == "" {
		subject = item.Topic
	}

	body := map[string]interface{}{}
	body["alert"] = subject
	body["message"] = text
	body["entity"] = item.Entity
	body["organization"] = item.Organization
	body["app_name"] = item.AppName
	body["topic"] = item.Topic
	body["created_on"] = item.CreatedOn
	body["sound"] = PUSH_SOUND
	body["badge"] = "Increment"

	data := map[string]interface{}{}
	data["data"] = body
	data["channels"] = []string{"USER_" + item.User}

	var jsonStr = utils.Encoder(&data)

	req, err := http.NewRequest("POST", PARSE_URL, bytes.NewBuffer(jsonStr))

	req.Header.Set("X-Parse-Application-Id", application_id)
	req.Header.Set("X-Parse-REST-API-Key", api_key)
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	saved_item.Insert(db.SavedPushCollection, &db.SavedItem{Data: data, Details: *item})

	log.Println("Push notification status:", resp.Status)
}
Пример #2
0
func emailHandler(
	item *db.PendingItem, text string, settings map[string]interface{},
) {
	log.Println("Sending email...")

	if item.Context["email"] == nil {
		log.Println("Email field not found.")
		return
	}

	email, ok := item.Context["email"].(string)
	if !ok {
		log.Println("Email field is of invalid type")
		return
		//} else {
		//email = "*****@*****.**"
	}

	var sender string = ""
	var subject string = ""

	if item.Context["sender"] != nil {
		sender, ok = item.Context["sender"].(string)
	}

	if item.Context["subject"] != nil {
		subject, ok = item.Context["subject"].(string)
	}

	mailConn := utils.MailConn{
		HostName:   settings["smtp_hostname"].(string),
		UserName:   settings["smtp_username"].(string),
		Password:   settings["smtp_password"].(string),
		SenderName: sender,
		Port:       settings["smtp_port"].(string),
		Host: settings["smtp_hostname"].(string) + ":" +
			settings["smtp_port"].(string),
	}

	msg := utils.Message{
		From:    settings["smtp_from"].(string),
		To:      []string{email},
		Subject: subject,
		Body:    text,
	}

	mailConn.SendEmail(msg)

	saved_item.Insert(db.SavedEmailCollection, &db.SavedItem{Data: msg, Details: *item})
}