Created
August 31, 2020 13:15
-
-
Save Ref-Bit/4242956a21388aaf7e36a44a1f8c625a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use this sample to create your own voice commands | |
intent('What does this app do?','What can I do here', | |
reply('This is a news project.')); | |
const NEWS_API_KEY = [YOUR_API_KEY]; | |
let saved_articles = []; | |
/* NEWS by SOURCE*/ | |
intent('Give me the news from $(source* (.*))', (p) => { | |
let NEWS_API_URL= `http://newsapi.org/v2/top-headlines?apiKey=${NEWS_API_KEY}`; | |
if(p.source.value){ | |
NEWS_API_URL = `${NEWS_API_URL}&sources=${p.source.value.toLowerCase().split(' ').join('-')}`; | |
} | |
api.request(NEWS_API_URL, (error, response, body) =>{ | |
const {articles} = JSON.parse(body); | |
if(!articles.length){ | |
p.play('Sorry, please try searching from different source.'); | |
return; | |
} | |
saved_articles = articles; | |
p.play({command: 'newHeadlines', articles}); | |
p.play(`Here are the (latest|recent) news from ${p.source.value}.`); | |
p.play('Would you like me to read the headlines?'); | |
p.then(confirmation); | |
}); | |
}); | |
/* NEWS by TERM*/ | |
intent('What\'\s up with $(term* (.*))', (p) => { | |
let NEWS_API_URL= `http://newsapi.org/v2/everything?apiKey=${NEWS_API_KEY}`; | |
if(p.term.value){ | |
NEWS_API_URL = `${NEWS_API_URL}&q=${p.term.value}`; | |
} | |
api.request(NEWS_API_URL, (error, response, body) =>{ | |
const {articles} = JSON.parse(body); | |
if(!articles.length){ | |
p.play('Sorry, please try searching from something else.'); | |
return; | |
} | |
saved_articles = articles; | |
p.play({command: 'newHeadlines', articles}); | |
p.play(`Here are the (latest|recent) articles on ${p.term.value}.`); | |
p.play('Would you like me to read the headlines?'); | |
p.then(confirmation); | |
}); | |
}); | |
/* NEWS by CATEGORIES*/ | |
const CATEGORIES = ['business', 'entertainment', 'general', 'health', 'science', 'sports', 'technology']; | |
const CATEGORIES_INTENT = `${CATEGORIES.map((category) => `${category}~${category}`).join('|')}|`; | |
intent(`(show|what is|tell me|what's|what are|what're|read) (the|) (recent|latest|) $(N news|headlines) (in|about|on|) $(C~ ${CATEGORIES_INTENT})`, | |
`(read|show|get|bring me|give me) (the|) (recent|latest) $(C~ ${CATEGORIES_INTENT}) $(N news|headlines)`, | |
(p) => { | |
let NEWS_API_URL= `http://newsapi.org/v2/top-headlines?apiKey=${NEWS_API_KEY}&country=us`; | |
if(p.C.value){ | |
NEWS_API_URL = `${NEWS_API_URL}&category=${p.C.value}`; | |
} | |
api.request(NEWS_API_URL, (error, response, body) =>{ | |
const {articles} = JSON.parse(body); | |
if(!articles.length){ | |
p.play('Sorry, please try searching from the existing categories.'); | |
return; | |
} | |
saved_articles = articles; | |
p.play({command: 'newHeadlines', articles}); | |
if(p.C.value){ | |
p.play(`Here are the (latest|recent) articles for ${p.C.value}.`); | |
} | |
else{ | |
p.play(`Here are the (latest|recent) news.`); | |
} | |
p.play('Would you like me to read the headlines?'); | |
p.then(confirmation); | |
}); | |
}); | |
/* CUSTOM DIALOGUE*/ | |
const confirmation = context(() => { | |
intent('yes', async (p)=>{ | |
for(let i=0;i<saved_articles.length;i++){ | |
p.play({command: 'highlight', article:saved_articles[i]}); | |
p.play(`${saved_articles[i].title}`); | |
} | |
}); | |
intent('no', (p)=>{ | |
p.play('Sounds good to me.'); | |
}); | |
}) | |
/* Read NUMBERED Article*/ | |
intent('open (the|) (article|) (number|) $(number* (.*))', (p) => { | |
if(p.number.value){ | |
p.play({command: 'open', number:p.number.value, articles: saved_articles}) | |
} | |
}); | |
/* Go Back */ | |
intent('(go|) back', (p) => { | |
p.play('Sure, going back.'); | |
p.play({command:'newHeadlines', articles:[]}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment