Ejemplo n.º 1
0
// HandleVisitMessagesCreate (GET)
//   params: id (id)
func HandleVisitMessagesCreate(e *config.Env, w http.ResponseWriter, r *http.Request) (int, error) {
	vID := mux.Vars(r)["id"]

	decoder := json.NewDecoder(r.Body)
	var m message.Message
	err := decoder.Decode(&m)

	if err != nil {
		return http.StatusBadRequest, err
	}

	m.VisitID = vID

	sCode, err := m.Create(e)

	if err != nil {
		return sCode, err
	}

	ms := make([]message.Message, 1)

	ms[0] = m

	writeJSONMessages(w, &ms)

	jobrunner.Now(jobs.PushToTwilio{Env: e, Message: m})

	return http.StatusOK, nil
}
Ejemplo n.º 2
0
func (m CreateMessageAndPushToDevice) Run() {
	v := visit.Visit{PhoneNumber: m.TwilioResponse.Get("From")}

	_, err := v.GetByPhoneNumber(m.Env)

	if err != nil {
		logError(m.Env, err)
	}

	// Handle survey right off the bat.
	if v.Survey {
		// Send precanned message to user...
		msg := message.Message{
			VisitID:    v.ID,
			Text:       "Thanks for getting back to me, I really appreciate it :) -Samantha",
			IsBusiness: true,
		}

		_, err = msg.Create(m.Env)

		if err != nil {
			logError(m.Env, err)
		}

		jobrunner.Now(jobs.PushToTwilio{Env: m.Env, Message: msg})
	} else {
		msg := message.Message{
			Text:       m.TwilioResponse.Get("Body"),
			IsBusiness: false,
			VisitID:    v.ID,
		}

		_, err = msg.Create(m.Env)

		if err != nil {
			logError(m.Env, err)
		}

		msgs := make([]message.Message, 1)

		msgs[0] = msg

		pushToDevice(msgs)
	}
}