Twilio Message Bot π
So you want a personal chatbot to handle your messages. Let’s build a easy twilio bot to handle and filter your incoming messages and calls - your personal virtual assistant π. Let’s start with a simple user case. I want to have a π to recieve the SMS messages and send me a summary about who are contacting me, what messages have them sent and what are their contact numbers.
1 - Get a twilio number
You can register a twilio number here.
2 - Create your own flow in twilio flow studio
It is a simple drag and click tools for doing something rather simple. For details you can check user guide. For this usecase, we can set our flow like this:
3 - Start your own backend server to handle requests from twilio
However, you also need your twilio bot to message you about the message summaries. Then how to do it? Similar to the guide here. You will need a small backend function to handle the POST request. So now it is clear - we need a tiny little backend server to handle the POST request.
// PostHandler converts post request body to string
func PostHandler(w http.ResponseWriter, r *http.Request) {
// ... setting up the accountSid and authToken
if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
}
// Build out the data for our message
v := url.Values{}
v.Set("To","your_number")
v.Set("From","your_twilio_number")
v.Set("Body", string(body))
rb := *strings.NewReader(v.Encode())
// Create client
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")
// Make request
resp, _ := client.Do(req)
fmt.Println(resp.Status)
}
}
That’s pretty much it π. ππ±message my twilio bot (+447479275693) to check out this twilio flow. Message me or leave a comment to let me know more interesting user cases. And you can find the source code here