Skip to content

Commit

Permalink
Fix Twilio sample (#1479)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyumic authored and fhinkel committed Oct 1, 2019
1 parent 6ab38b1 commit 42e9f34
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
19 changes: 9 additions & 10 deletions appengine/twilio/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,23 @@ if (!TWILIO_NUMBER) {
);
}

const Twilio = require('twilio');
const twilio = require('twilio');

const twilioClient = Twilio(
const twilioClient = new twilio(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);

const {TwimlResponse} = Twilio;

// [START gae_flex_twilio_receive_call]
app.post('/call/receive', (req, res) => {
const resp = new TwimlResponse();
resp.say('Hello from Google App Engine.');
const twiml = new twilio.twiml.VoiceResponse();

twiml.say('Hello from Google App Engine.');

res
.status(200)
.contentType('text/xml')
.send(resp.toString());
.send(twiml.toString());
});
// [END gae_flex_twilio_receive_call]

Expand Down Expand Up @@ -82,13 +81,13 @@ app.post('/sms/receive', bodyParser, (req, res) => {
const sender = req.body.From;
const body = req.body.Body;

const resp = new TwimlResponse();
resp.message(`Hello, ${sender}, you said: ${body}`);
const twiml = new twilio.twiml.MessagingResponse();
twiml.message(`Hello, ${sender}, you said: ${body}`);

res
.status(200)
.contentType('text/xml')
.send(resp.toString());
.send(twiml.toString());
});
// [END gae_flex_twilio_receive_sms]

Expand Down
2 changes: 1 addition & 1 deletion appengine/twilio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"twilio": "^3.30.3"
"twilio": "^3.34.0"
},
"devDependencies": {
"mocha": "^6.1.4",
Expand Down
28 changes: 22 additions & 6 deletions appengine/twilio/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ const getSample = () => {
messages: {
create: sinon.stub().resolves(),
},
message: sinon.stub(),
say: sinon.stub(),
toString: sinon.stub(),
};
const twilioVoiceRespMock = {
say: sinon.stub().returns(),
};
const twilioMessagingRespMock = {
message: sinon.stub().returns(),
};

const twilioStub = sinon.stub().returns(twilioMock);
twilioStub.TwimlResponse = sinon.stub().returns(twilioMock);
twilioStub.twiml = {
VoiceResponse: sinon.stub().returns(twilioVoiceRespMock),
MessagingResponse: sinon.stub().returns(twilioMessagingRespMock),
};

const {app} = proxyquire('../app', {twilio: twilioStub});

Expand All @@ -41,6 +47,8 @@ const getSample = () => {
}),
mocks: {
twilio: twilioMock,
twilioVoiceRespMock: twilioVoiceRespMock,
twilioMessagingRespMock: twilioMessagingRespMock,
},
};
};
Expand All @@ -66,7 +74,11 @@ it('should receive an SMS message', () => {
.type('form')
.expect(200)
.expect(() => {
assert(mocks.twilio.message.calledWith('Hello, Bob, you said: hi'));
assert(
mocks.twilioMessagingRespMock.message.calledWith(
'Hello, Bob, you said: hi'
)
);
});
});

Expand All @@ -78,6 +90,10 @@ it('should receive a call', () => {
.send()
.expect(200)
.expect(() => {
assert(mocks.twilio.say.calledWith('Hello from Google App Engine.'));
assert(
mocks.twilioVoiceRespMock.say.calledWith(
'Hello from Google App Engine.'
)
);
});
});

0 comments on commit 42e9f34

Please sign in to comment.