Skip to content

Commit 34d92f8

Browse files
committed
feat: add Go samples for messaging twiml
1 parent 6599c64 commit 34d92f8

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
7+
"github.com/gin-gonic/gin"
8+
"github.com/twilio/twilio-go/twiml"
9+
)
10+
11+
func main() {
12+
router := gin.Default()
13+
14+
router.POST("/handle-sms", func(context *gin.Context) {
15+
incomingBody := strings.ToLower(strings.TrimSpace(context.PostForm("Body")))
16+
17+
var body string
18+
if incomingBody == "hello" {
19+
body = "Hi!"
20+
} else if incomingBody == "bye" {
21+
body = "Goodbye"
22+
} else {
23+
body = "No Body param match, Twilio sends this in the request to your server."
24+
}
25+
26+
message := &twiml.MessagingMessage{
27+
Body: body,
28+
}
29+
twimlResult, _ := twiml.Messages([]twiml.Element{message})
30+
31+
context.Header("Content-Type", "text/xml")
32+
context.String(http.StatusOK, twimlResult)
33+
})
34+
35+
router.Run(":3000")
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/twilio/twilio-go/twiml"
8+
)
9+
10+
func main() {
11+
router := gin.Default()
12+
13+
router.POST("/sms", func(context *gin.Context) {
14+
message := &twiml.MessagingMessage{
15+
InnerElements: []twiml.Element{
16+
twiml.MessagingBody{
17+
Message: "The Robots are coming! Head for the hills!",
18+
},
19+
twiml.MessagingMedia{
20+
Url: "https://farm8.staticflickr.com/7090/6941316406_80b4d6d50e_z_d.jpg",
21+
},
22+
},
23+
}
24+
25+
twimlResult, _ := twiml.Messages([]twiml.Element{message})
26+
27+
context.Header("Content-Type", "text/xml; charset=\"utf-8\"")
28+
context.String(http.StatusOK, twimlResult)
29+
})
30+
31+
router.Run(":3000")
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/twilio/twilio-go/twiml"
8+
)
9+
10+
func main() {
11+
router := gin.Default()
12+
13+
router.POST("/handle-sms", func(context *gin.Context) {
14+
message := &twiml.MessagingMessage{
15+
Body: "The Robots are coming! Head for the hills!",
16+
}
17+
twimlResult, _ := twiml.Messages([]twiml.Element{message})
18+
19+
context.Header("Content-Type", "text/xml")
20+
context.String(http.StatusOK, twimlResult)
21+
})
22+
23+
router.Run(":3000")
24+
}

0 commit comments

Comments
 (0)