func handleCancel(db *bolt.DB, from, idStr string) string { id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { log.Printf("Error parsing Reminder ID: %v\n", err) err2 := twilhelp.SendSMS(from, "Error parsing the Reminder ID. Sorry!") if err2 != nil { log.Printf(`Error sending "sorry we couldn't parse" msg: %v\n`, err) } return twilioResponse("") } if err := runningReminders.Cancel(db, id); err != nil { log.Printf("Error cancelling Reminder %v: %v\n", id, err) reply := fmt.Sprintf("Error stopping Reminder %v. Sorry!", id) err2 := twilhelp.SendSMS(from, reply) if err2 != nil { log.Printf(`Error sending "sorry we couldn't cancel" msg: %v\n`, err) } return twilioResponse("") } reply := fmt.Sprintf("Reminder %v successfully stopped. Have an epic day!", id) err = twilhelp.SendSMS(from, reply) if err != nil { log.Printf(`Error sending "reminder %v successfully stopped": %v\n`, id, err) } return twilioResponse("") }
func incomingSMS(db *bolt.DB, req *http.Request, log *log.Logger) string { from := req.FormValue("From") body := req.FormValue("Body") log.Printf("Incoming SMS: `%v: %v`", from, body) parts := regexStopReminder.FindStringSubmatch(body) if len(parts) > 0 { return handleCancel(db, from, parts[1]) } // Remind me to _ @ _ reminder, err := parseReminder(from, body) if err != nil { log.Printf("Error parsing incoming message body: %v\n", err) return twilioResponse("") } err = reminder.Save(db) if err != nil { if err != nil { log.Printf("Error saving reminder %#v: %v\n", reminder, err) } err2 := twilhelp.SendSMS(from, "Error saving your reminder. Sorry!") if err2 != nil { log.Printf(`Error sending "sorry we couldn't save" msg: %v\n`, err) } return twilioResponse("") } err = runningReminders.ScheduleNew(db, reminder) if err != nil { log.Printf("Error scheduling reminder %#v: %v\n", reminder, err) err2 := twilhelp.SendSMS(from, "Error scheduling your reminder. Sorry!") if err2 != nil { log.Printf("Error after successful parse but failed scheduling: %v\n", err2) } return twilioResponse("") } reply := fmt.Sprintf("Reminder %v successfully scheduled! "+ "Have a great day :-)", reminder.ID) err = twilhelp.SendSMS(from, reply) if err != nil { log.Printf("Error from post-successful scheduling send: %v\n", err) } return twilioResponse("") }
func (r *Reminder) SendSMS() error { prefix := "" if r.ID != 0 { prefix = fmt.Sprintf("Reminder %v: ", r.ID) } return twilhelp.SendSMS(r.Recipient, prefix+r.Description) }
func main() { if len(os.Args) < 3 { log.Fatalf("Usage: %s <10-digit phone> <message>", filepath.Base(os.Args[0])) } toPhone := os.Args[1] msg := os.Args[2] if err := twilhelp.SendSMS(toPhone, msg); err != nil { log.Fatalf("Error sending text to %s: %v\n", toPhone, err) } }