Пример #1
0
func (cv *Conversation) handleUnknownUserMessage(msg sarif.Message) {
	pl := &MsgErrNatural{
		Original: msg.Text,
	}

	cv.SendToClient(msg.Reply(sarif.CreateMessage("err/natural", pl)))
}
Пример #2
0
func (cv *Conversation) HandleClientMessage(msg sarif.Message) {
	if msg.Text == ".full" || msg.Text == "/full" {
		text, err := json.MarshalIndent(cv.LastMessage, "", "    ")
		if err != nil {
			panic(err)
		}
		cv.service.Reply(msg, sarif.Message{
			Action: "natural/full",
			Text:   string(text),
		})
		return
	}

	// Check if client answers a conversation.
	if time.Now().Sub(cv.LastTime) < 5*time.Minute {
		if cv.LastMessageAction.IsAction() {
			parsed, ok := cv.answer(cv.LastMessageAction.Action, msg.Text)
			cv.LastTime = time.Time{}
			parsed.Destination = cv.LastMessage.Source
			if ok {
				cv.PublishForClient(parsed)
			}
			return
		}
	}

	// Otherwise parse message as normal request.
	ctx := &natural.Context{
		Text:      msg.Text,
		Sender:    "user",
		Recipient: "sarif",
	}
	res, err := cv.service.Parse(ctx)
	if err != nil || len(res.Intents) == 0 {
		cv.handleUnknownUserMessage(msg)
		return
	}
	pred := res.Intents[0]
	if pred.Type == "exclamatory" {
		cv.SendToClient(msg.Reply(sarif.Message{
			Action: "natural/phrase",
			Text:   cv.service.phrases.Answer(msg.Text),
		}))
		return
	}

	if pred.Message.Text == "" && pred.Type != "simple" {
		pred.Message.Text = msg.Text
	}
	cv.LastUserTime = time.Now()
	cv.LastUserText = msg.Text
	cv.LastUserMessage = pred.Message
	pred.Message.CorrId = msg.Id
	cv.PublishForClient(pred.Message)
}
Пример #3
0
func (s *Service) handleGeofenceCreate(msg sarif.Message) {
	var g Geofence
	if err := msg.DecodePayload(&g); err != nil {
		s.ReplyBadRequest(msg, err)
		return
	}

	if g.Address != "" {
		geo, err := Geocode(g.Address)
		if err != nil {
			s.ReplyBadRequest(msg, err)
			return
		}
		if len(geo) == 0 {
			s.Publish(msg.Reply(MsgAddressNotFound))
			return
		}
		g.BoundingBox = BoundingBox(geo[0].BoundingBox)
	}
	if g.Name == "" {
		g.Name = sarif.GenerateId()
	}
	g.GeohashMin = EncodeGeohash(g.BoundingBox.LatMin, g.BoundingBox.LngMin, 12)
	g.GeohashMax = EncodeGeohash(g.BoundingBox.LatMax, g.BoundingBox.LngMax, 12)

	if _, err := s.Store.Put(g.Key(), &g); err != nil {
		s.ReplyInternalError(msg, err)
	}

	reply := sarif.Message{Action: "location/fence/created"}
	if err := reply.EncodePayload(g); err != nil {
		s.ReplyInternalError(msg, err)
		return
	}
	reply.Text = "Geofence '" + g.Name + "' created."
	s.Publish(reply)
}