Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Twilio sample #1479

Merged
merged 5 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.'
)
);
});
});