Example #1
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))
}
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
	}
}