func (server *Server) SendMessage(ctx context.Context, in *pb.Message) (*pb.SendMessageResponse, error) {
	log.Infof("server.go: SendMessage with template='%s' and language='%s' to #%d target(s)", in.Template, in.Language, len(in.Targets))

	temp := server.GetTemplateGroup(in.Template)
	results := make([]*pb.MessageTargetResponse, 0)

	if temp == nil {
		return pb.NewCustomMessageResponse(pb.ResultTemplateGroupNotFound,
			fmt.Sprintf("template=%s not found", in.Template), results), nil
	}

	for _, t := range in.Targets {
		ty := getTargetType(t)
		if len(ty) > 0 {
			driver := server.Drivers[ty]
			if driver != nil {
				err := driver.Send(in.Language, server.Config.DefaultLanguage, t, temp)
				if err != nil {
					results = append(results, &pb.MessageTargetResponse{Type: pb.ResultInternalDriverError, Data: fmt.Sprint(err)})
				} else {
					results = append(results, pb.NewMessageTargetResponse(pb.ResultSuccess, driver.Type(), driver.Name()))
				}
			} else {
				log.Warningf("server.go: %s driver not found", ty)
				results = append(results, pb.NewMessageTargetResponse(pb.ResultDriverNotFound, ty, ""))
			}
		} else {
			log.Warningf("server.go: there is no suitable driver")
			results = append(results, pb.NewMessageTargetResponse(pb.ResultDriverNotFound, "", ""))
		}
	}
	return pb.NewMessageResponse(pb.ResultSuccess, results), nil
}
Example #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
}