Example #1
0
func (p PushToTwilio) Run() {
	if p.Message.IsBusiness {
		accountSid := os.Getenv("OAKED_TWILIO_ACCOUNT_SID")
		authToken := os.Getenv("OAKED_TWILIO_AUTH_TOKEN")
		urlStr := "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Messages.json"

		v := visit.Visit{ID: p.Message.VisitID}

		_, err := v.Get(p.Env)

		if err != nil {
			return
		}

		msg := url.Values{}
		msg.Set("To", v.PhoneNumber)
		msg.Set("From", "+15873174099")
		msg.Set("Body", p.Message.Text)
		rb := *strings.NewReader(msg.Encode())

		client := &http.Client{}

		req, _ := http.NewRequest("POST", urlStr, &rb)
		req.SetBasicAuth(accountSid, authToken)
		req.Header.Add("Accept", "application/json")
		req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

		client.Do(req)
	}
}
Example #2
0
// HandleVisitSurveysCreate -
func HandleVisitSurveysCreate(e *config.Env, w http.ResponseWriter, r *http.Request) (int, error) {
	v := visit.Visit{ID: mux.Vars(r)["id"]}

	sCode, err := v.Get(e)

	if err != nil {
		return sCode, err
	}

	// Push survey only once.
	if !v.Survey {
		statusCode, err := visit.AddSurveyByVisitID(e, v.ID)

		if err != nil {
			return statusCode, err
		}

		// Send precanned message to user...
		m := message.Message{
			VisitID:    v.ID,
			Text:       "Thanks for taking the time to visit us today! I just wanted to follow-up personally and see how your visit went. Thanks! -Samantha",
			IsBusiness: true,
		}

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

	return http.StatusOK, nil

}
Example #3
0
// HandleGet (GET)
//   params: id (id)
func HandleGet(e *config.Env, w http.ResponseWriter, r *http.Request) (int, error) {
	mID := mux.Vars(r)["id"]

	v := visit.Visit{ID: mID}

	sCode, err := v.Get(e)

	if err != nil {
		return sCode, err
	}

	writeJSON(w, &v)

	return http.StatusOK, nil
}
Example #4
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)
	}
}
Example #5
0
func HandleCreate(e *config.Env, w http.ResponseWriter, r *http.Request) (int, error) {
	decoder := json.NewDecoder(r.Body)
	var v visit.Visit
	err := decoder.Decode(&v)

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

	statusCode, err := v.Create(e)

	if err != nil {
		return statusCode, err
	}

	writeJSON(w, &v)

	return http.StatusOK, nil
}