Skip to content

Commit

Permalink
Merge pull request #8 from juanprado/master
Browse files Browse the repository at this point in the history
adding phone number prompt
  • Loading branch information
chrisdevwords committed Jan 14, 2017
2 parents f268c73 + ea87607 commit f89e169
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 57 deletions.
138 changes: 83 additions & 55 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ import {
const app = new alexa.app('who-are-my-congressmen');

const slots = {
ADDRESS: 'AMAZON.PostalAddress',
ADDRESS: 'AMAZON.PostalAddress'
};

const phoneSlots = {
PHONE: 'NUMBER'
}

const utterances = [
'{|my address|I live|It|It\'s} ' +
'{|is|at} {-|ADDRESS}'
];

const phoneUtterances = [
'{|my|the|} ' +
'{|phone|number|} ' +
'{|is|it is| it\'s} {-|PHONE}'
];

app.intent('FindByAddress', { slots, utterances },

(req, res) => {
Expand Down Expand Up @@ -69,69 +79,87 @@ app.intent('FindByAddress', { slots, utterances },
}
);

const exitFunction = (req, res) => {
res.say('Goodbye!');
};
app.intent('GetPhoneNumber', { slots: phoneSlots, utterances: phoneUtterances },
(req, res) => {

app.intent('AMAZON.YesIntent', (req, res) => {
const session = req.getSession();
const data = session.get('data');

if (data) {
const { senators, representative } = data;
const names = [
representative.name,
senators[0].name,
senators[1].name
];

getBulkContactMessage(names)
.then(parseBulkMessages)
.then((message) => {
const card = {
type: 'Simple',
title: 'Their Contact Info',
content: message
}
console.log('-- sending bulk message');
sendBulkMessage(message)
.then((resp) => {
console.log('-- bulk message sent');

res
.say(message)
.card(card)
.shouldEndSession(true)
.send();
})
.catch((err => {
console.log('-- error sending bulk message', err);
res
.say('There was an error sending the text')
.say(message)
.card(card)
.shouldEndSession(true)
.send();

}))
const phone = req.slot('PHONE');
const session = req.getSession();
const data = session.get('data');

console.log('phone number is', phone);

if (phone && data) {
const { senators, representative } = data;
const names = [
representative.name,
senators[0].name,
senators[1].name
];

getBulkContactMessage(names)
.then(parseBulkMessages)
.then((message) => {
const card = {
type: 'Simple',
title: 'Their Contact Info',
content: message
}
console.log('-- sending bulk message');
sendBulkMessage(phone, message)
.then((resp) => {
console.log('-- bulk message sent');

res
.say(`Ok, your information was texted to ${phone}`)
.card(card)
.shouldEndSession(true)
.send();
})
.catch((err) => {
console.log('-- error sending bulk message', err);
res
.say('There was an error sending the text')
.say(message)
.card(card)
.shouldEndSession(true)
.send();
})

})
.catch((err) => {
const { message } = parseErrorToMessage(err);
res
.say(message)
.shouldEndSession(true)
.send();
});
return false;
}

})
.catch((err) => {
const { message } = parseErrorToMessage(err);
res
.say(message)
.shouldEndSession(true)
.send();
});
const prompt = 'I didn\'t hear a phone number. Tell me a phone number.';
const reprompt = 'Tell me your phone number.';

return false;
res
.say(prompt)
.reprompt(reprompt)
.shouldEndSession(false);
return true;
}
);

exitFunction();

const exitFunction = (req, res) => {
res.say('Goodbye!');
};

app.intent('AMAZON.YesIntent', (req, res) => {
res.say('Ok, please give me your phone number')
.shouldEndSession(false)
.send();
return false;
});


app.intent('AMAZON.HelpIntent', (req, res) => {
res
.say(
Expand Down
4 changes: 2 additions & 2 deletions src/serivce-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export function parseContactMessage(name, result) {
return { message };
}

export function sendBulkMessage(message) {
export function sendBulkMessage(phone, message) {
const options = {
uri: smsEndpoint(encodeURI(2163347295), encodeURI(message)),
uri: smsEndpoint(encodeURI(phone), encodeURI(message)),
json:true
};
console.log(' - bulk message sent to endpoint:', options.uri);
Expand Down

0 comments on commit f89e169

Please sign in to comment.