Esempio n. 1
0
func (o *oneSignalDrv) Send(ctx context.Context, message *notificationpb.Message, man template.Manager, ch chan<- drivers.DriverResult) {
	p := message.GetPush()
	m := o.notification()
	if uid, ok := message.Tags["user_id"]; ok {
		m.addTagFilter("userid", uid)
	} else if len(p.To) > 0 {
		m.addTagFilter("userid", p.To[0])
	}

	if env, ok := message.Tags["env"]; ok {
		m.addTagFilter("env", env)
	}
	if len(p.To) > 0 {
		m.IncludePlayerIDs = p.To
	}
	if message.ScheduleAt > 0 {
		m.SendAfter = time.Unix(message.ScheduleAt, 0).UTC().String()
	}
	if p.LaunchUrl > "" {
		m.Url = p.LaunchUrl
	}
	if err := putContent(&m, message, man); err != nil {
		ch <- drivers.DriverResult{Type: drivers.TypePush, Err: err}
		return
	}

	var out bytes.Buffer
	if err := json.NewEncoder(&out).Encode(&m); err != nil {
		ch <- drivers.DriverResult{Type: drivers.TypePush, Err: fmt.Errorf("encode message json error, %v", err)}
		return
	}
	req, err := http.NewRequest("POST", "https://onesignal.com/api/v1/notifications", &out)
	if err != nil {
		ch <- drivers.DriverResult{Type: drivers.TypePush, Err: err}
		return
	}
	req.Header.Set("Authorization", fmt.Sprintf("Basic %s", o.authorization))
	req.Header.Set("Content-Type", "application/json")
	select {
	case <-ctx.Done():
		ch <- drivers.DriverResult{Type: drivers.TypePush, Err: ctx.Err()}
		return
	default:
	}
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		ch <- drivers.DriverResult{Type: drivers.TypePush, Err: err}
		return
	}
	if resp.StatusCode != http.StatusOK {
		ch <- drivers.DriverResult{Type: drivers.TypePush, Err: fmt.Errorf("status code: %d", resp.StatusCode)}
		return
	}
	ch <- drivers.DriverResult{Type: drivers.TypePush, Err: nil}
}
Esempio n. 2
0
func (server *Server) SendMessage(ctx context.Context, in *pb.Message) (*pb.SendMessageResponse, error) {
	if in.Language == "" {
		in.Language = server.Config.DefaultLanguage
	}
	n := len(in.Targets)
	logrus.Debugf("SendMessage with event='%s' and language='%s' to #%d target(s)", in.Event, in.Language, n)
	results := make([]*pb.MessageTargetResponse, 0)
	ch := make(chan drivers.DriverResult, 1)
	go server.send(ctx, in, ch)

	for i := 0; i < n; i++ {
		r := <-ch
		resp := &pb.MessageTargetResponse{
			Target: string(r.Type),
			Output: "Success",
		}
		if r.Err != nil {
			resp.Output = r.Err.Error()
		}
		results = append(results, resp)
	}
	if logrus.GetLevel() >= logrus.DebugLevel {
		for _, t := range results {
			logrus.Debugf("SendMessage output[%s]= %s", t.Target, t.Output)
		}
	}
	return pb.NewMessageResponse(results), nil
}
Esempio n. 3
0
func putContent(n *notification, message *notificationpb.Message, man template.Manager) error {
	p := message.GetPush()
	langs := man.Languages(message.Event, "push")
	data := make(map[string]interface{})
	if err := json.Unmarshal(message.DataJson, &data); err != nil {
		return err
	}
	for k, v := range message.Tags {
		if _, ok := data[k]; !ok {
			data[k] = v
		}
	}
	for _, l := range langs {
		t, err := man.Template(message.Event, l, "push")
		if err != nil {
			return err
		}
		s, err := t.String(data)
		if err != nil {
			return err
		}
		n.Contents[l] = s
	}

	for _, l := range man.Languages(message.Event, "tit") {
		t, err := man.Template(message.Event, l, "tit")
		if err != nil {
			return err
		}
		s, err := t.String(data)
		if err != nil {
			return err
		}
		n.Headings[l] = s
	}

	if len(p.Template) > 0 {
		cts := make(map[string]string)
		if err := json.Unmarshal(p.Template, &cts); err != nil {
			if len(langs) == 0 {
				t, e := template.NewTemplate(string(p.Template), false)
				if e != nil {
					return e
				}
				s, _ := t.String(data)
				n.Contents["en"] = s
				n.Contents[message.Language] = s
				return nil
			}
		} else {
			for k, v := range cts {
				t, e := template.NewTemplate(v, false)
				if e != nil {
					continue
				}
				n.Contents[k], _ = t.String(data)
			}
			if _, ok := n.Contents["en"]; !ok {
				n.Contents["en"] = ""
			}
			return nil
		}
	}
	if len(p.Headings) > 0 {
		cts := make(map[string]string)
		if err := json.Unmarshal(p.Template, &cts); err != nil {
			if len(langs) == 0 {
				t, e := template.NewTemplate(string(p.Template), false)
				if e != nil {
					return e
				}
				s, _ := t.String(data)
				n.Headings["en"] = s
				n.Headings[message.Language] = s
				return nil
			}
		} else {
			for k, v := range cts {
				t, e := template.NewTemplate(v, false)
				if e != nil {
					continue
				}
				n.Headings[k], _ = t.String(data)
			}
			if _, ok := n.Headings["en"]; !ok {
				n.Headings["en"] = ""
			}
			return nil
		}
	}
	return nil
}
Esempio n. 4
0
func (d *SendGridDriver) Send(ctx context.Context, message *notificationpb.Message, man template.Manager, ch chan<- drivers.DriverResult) {
	m := message.GetEmail()
	email := new(mail.SGMailV3)
	p := mail.NewPersonalization()
	var fromName string
	if len(m.FromName) > 0 {
		fromName = m.FromName
	} else {
		fromName = d.DefaultFromName
	}
	if len(m.FromEmail) > 0 {
		email.SetFrom(mail.NewEmail(fromName, m.FromEmail))
	} else {
		email.SetFrom(mail.NewEmail(fromName, d.DefaultFromEmail))
	}
	addName := len(m.ToEmail) == len(m.ToName)
	for i, e := range m.ToEmail {
		if addName {
			p.AddTos(mail.NewEmail(m.ToName[i], e))
		} else {
			p.AddTos(mail.NewEmail("", e))
		}
	}
	for _, e := range m.Cc {
		p.AddCCs(mail.NewEmail("", e))
	}
	for _, e := range m.Bcc {
		p.AddBCCs(mail.NewEmail("", e))
	}
	if len(m.ReplyTo) > 0 {
		email.SetReplyTo(mail.NewEmail("", m.ReplyTo))
	}
	data := make(map[string]interface{})
	if len(message.DataJson) > 0 {
		if err := json.Unmarshal(message.DataJson, &data); err != nil {
			ch <- drivers.DriverResult{Type: drivers.TypeEmail, Err: err}
			return
		}
	}
	for k, v := range message.Tags {
		if _, ok := data[k]; !ok {
			data[k] = v
		}
	}
	var err error
	email.Subject, _, err = templateString(data, m.TemplateSub, message, man, "sub", false)
	if err != nil && m.Subject == "" {
		ch <- drivers.DriverResult{Type: drivers.TypeEmail, Err: errors.New("failed to create subject text")}
		return
	}
	if email.Subject == "" {
		email.Subject = m.Subject
	}
	var html string
	var set bool
	var text string
	text, set, err = templateString(data, m.TemplateTxt, message, man, "txt", false)
	if set {
		if err != nil {
			ch <- drivers.DriverResult{Type: drivers.TypeEmail, Err: errors.New("failed to create text content")}
			return
		}
		email.AddContent(mail.NewContent("text/plain", text))
	}
	html, set, err = templateString(data, m.TemplateHtml, message, man, "html", true)
	if set {
		if err != nil {
			ch <- drivers.DriverResult{Type: drivers.TypeEmail, Err: errors.New("failed to create html content")}
			return
		}
		email.AddContent(mail.NewContent("text/html", html))
	}
	if message.ScheduleAt > 0 {
		email.SendAt = int(message.ScheduleAt)
	}
	email.AddPersonalizations(p)
	request := sendgrid.GetRequest(d.ApiKey, "/v3/mail/send", "https://api.sendgrid.com")
	request.Method = "POST"
	request.Body = mail.GetRequestBody(email)
	select {
	case <-ctx.Done():
		ch <- drivers.DriverResult{Type: drivers.TypeEmail, Err: ctx.Err()}
	default:
		r, err := sendgrid.API(request)
		if err != nil {
			ch <- drivers.DriverResult{Type: drivers.TypeEmail, Err: err}
			return
		}
		if r.StatusCode >= 300 {
			b, _ := json.Marshal(email)
			logrus.Debugf("failed to send mail: body was %s", string(b))
			ch <- drivers.DriverResult{Type: drivers.TypeEmail, Err: fmt.Errorf(r.Body)}
		} else {
			ch <- drivers.DriverResult{Type: drivers.TypeEmail, Err: nil}
		}
	}
}