Example #1
0
func Repeat(tw twilio.Context) {
	req := tw.Request()
	c := appengine.NewContext(req)
	parts := strings.Split(req.URL.Path, "/")
	from := parts[len(parts)-1]
	var lf LastFortune
	if err := datastore.Get(c, datastore.NewKey(c, "LastFortune", from, 0, nil), &lf); err != nil {
		c.Errorf("error getting last fortune for %q: %v", from, err)
		return
	}
	c.Debugf("LastFortune(%q): %v", from, lf)
	tw.Response(string(lf.Twiml))
}
func (ft Fortwilio) Handle(tw twilio.Context) {
	c := appengine.NewContext(tw.Request())
	twiml := ft.Say()
	c.Debugf(twiml)
	tw.Response(twiml)
	lf := &LastFortune{
		tw.Value("To"),
		[]byte(twiml),
	}
	if _, err := datastore.Put(c, datastore.NewKey(c, "LastFortune", tw.Value("From"), 0, nil), lf); err != nil {
		c.Errorf("error recording last fortune for %q: %v", tw.Value("From"), err)
		return
	}
}
Example #3
0
func handler(c twilio.Context) {
	o := c.IntValue("Offset")
	if o < 0 || o >= len(digits) {
		c.Hangup()
		return
	}
	d := ""
	if o == 0 {
		d = "3 point "
	}
	d += digits[o]
	c.Responsef(`
		<Say>%v</Say>
		<Redirect method="GET">http://pi-phone.appspot.com/?Offset=%v</Redirect>
	`, d, o+1)
}
Example #4
0
func Reshare(tw twilio.Context) {
	c := appengine.NewContext(tw.Request())
	from := tw.Value("From")
	var lf LastFortune
	if err := datastore.Get(c, datastore.NewKey(c, "LastFortune", from, 0, nil), &lf); err != nil {
		c.Errorf("error getting last fortune for %q: %v", from, err)
		return
	}
	c.Debugf("LastFortune(%q): %v", from, lf)
	client := urlfetch.Client(c)
	friend := strings.TrimSpace(tw.Value("Body"))
	data := url.Values{
		"From":   []string{lf.Number},
		"To":     []string{friend},
		"Url":    []string{fmt.Sprintf("%s/%s", repeatUrl, from)},
		"Method": []string{"GET"},
	}
	c.Debugf("POST(%s): %q", callsApiUrl, data.Encode())
	req, err := http.NewRequest("POST", callsApiUrl, strings.NewReader(data.Encode()))
	if err != nil {
		c.Errorf("error creating API request: %v", err)
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.SetBasicAuth(accountSid, authToken)
	resp, err := client.Do(req)
	if err != nil {
		c.Errorf("error posting outbound call: %v", err)
		return
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		c.Errorf("error reading API response body: %v", err)
		return
	}
	c.Debugf("API response body: %q", string(body))
}