Skip to content

Commit 6efe839

Browse files
committed
Add WhatsApp messages for quickstart
1 parent 2d47bdc commit 6efe839

9 files changed

+139
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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("/whatsapp", func(context *gin.Context) {
14+
message := &twiml.MessagingMessage{
15+
Body: "Message received! Hello again from Twilio Whatsapp.",
16+
}
17+
18+
twimlResult, err := twiml.Messages([]twiml.Element{message})
19+
if err != nil {
20+
context.String(http.StatusInternalServerError, err.Error())
21+
} else {
22+
context.Header("Content-Type", "text/xml")
23+
context.String(http.StatusOK, twimlResult)
24+
}
25+
})
26+
27+
router.Run(":3000")
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import com.twilio.twiml.MessagingResponse;
2+
import com.twilio.twiml.messaging.Body;
3+
import com.twilio.twiml.messaging.Message;
4+
5+
import static spark.Spark.*;
6+
7+
public class SmsApp {
8+
public static void main(String[] args) {
9+
get("/", (req, res) -> "Hello Web");
10+
11+
post("/whatsapp", (req, res) -> {
12+
res.type("application/xml");
13+
Body body = new Body
14+
.Builder("Message received! Hello again from Twilio Whatsapp.")
15+
.build();
16+
Message sms = new Message
17+
.Builder()
18+
.body(body)
19+
.build();
20+
MessagingResponse twiml = new MessagingResponse
21+
.Builder()
22+
.message(sms)
23+
.build();
24+
return twiml.toXml();
25+
});
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const express = require('express');
2+
const { MessagingResponse } = require('twilio').twiml;
3+
4+
const app = express();
5+
6+
app.post('/whatsapp', (req, res) => {
7+
const twiml = new MessagingResponse();
8+
9+
twiml.message('Message received! Hello again from Twilio Whatsapp.');
10+
11+
res.type('text/xml').send(twiml.toString());
12+
});
13+
14+
app.listen(3000, () => {
15+
console.log('Express server listening on port 3000');
16+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// In Package Manager, run:
2+
// Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor
3+
4+
using System.Web.Mvc;
5+
using Twilio.AspNet.Mvc;
6+
using Twilio.TwiML;
7+
8+
namespace YourNewWebProject.Controllers
9+
{
10+
public class WhatsappController : TwilioController
11+
{
12+
[HttpPost]
13+
public TwiMLResult Index()
14+
{
15+
var messagingResponse = new MessagingResponse();
16+
messagingResponse.Message("Message received! Hello again from Twilio Whatsapp.");
17+
18+
return TwiML(messagingResponse);
19+
}
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'twilio-ruby'
2+
require 'sinatra'
3+
4+
# Respond to incoming calls with a simple text message
5+
post '/whatsapp' do
6+
twiml = Twilio::TwiML::MessagingResponse.new do |r|
7+
r.message body: 'Message received! Hello again from Twilio Whatsapp.'
8+
end
9+
10+
content_type 'text/xml'
11+
12+
twiml.to_s
13+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
// Get the PHP helper library from https://twilio.com/docs/libraries/php
3+
4+
require_once 'vendor/autoload.php'; // Loads the library
5+
use Twilio\TwiML\MessagingResponse;
6+
7+
$response = new MessagingResponse();
8+
$response->message("Message received! Hello again from Twilio Whatsapp.");
9+
print $response;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from flask import Flask, request, redirect
2+
from twilio.twiml.messaging_response import MessagingResponse
3+
4+
app = Flask(__name__)
5+
6+
@app.route("/whatsapp", methods=['GET', 'POST'])
7+
def sms_reply():
8+
"""Respond to incoming calls with a simple text message."""
9+
# Start our TwiML response
10+
resp = MessagingResponse()
11+
12+
# Add a message
13+
resp.message("Message received! Hello again from Twilio Whatsapp.")
14+
15+
return str(resp)
16+
17+
if __name__ == "__main__":
18+
app.run(debug=True)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testable: false
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"testable": "false",
3+
"type": "server",
4+
"title": "Respond to an incoming WhatsApp message",
5+
"description": "When the Twilio Sandbox for WhatsApp receives an incoming message, Twilio will send an HTTP request to your server. This code shows how your server can reply with a WhatsApp message using the Twilio helper library."
6+
}

0 commit comments

Comments
 (0)