Skip to content

Instantly share code, notes, and snippets.

@adrianhajdin
Created August 4, 2020 09:56
Show Gist options
  • Save adrianhajdin/bab49eac7dcf9c095fce37ae1167dd87 to your computer and use it in GitHub Desktop.
Save adrianhajdin/bab49eac7dcf9c095fce37ae1167dd87 to your computer and use it in GitHub Desktop.
Alan Studio Code Materials
intent('What does this app do?', 'What can I do here?',
reply('This is a news project.'));
const API_KEY = '7bdfb1b10aca41c6becea47611b7c35a';
let savedArticles = [];
// News by Source
intent('Give me the news from $(source* (.*))', (p) => {
let NEWS_API_URL = `https://newsapi.org/v2/top-headlines?apiKey=${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 for news from a different source');
return;
}
savedArticles = articles;
p.play({ command: 'newHeadlines', articles });
p.play(`Here are the (latest|recent) ${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 = `https://newsapi.org/v2/everything?apiKey=${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 for something else.');
return;
}
savedArticles = 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 = `https://newsapi.org/v2/top-headlines?apiKey=${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 for a different category.');
return;
}
savedArticles = articles;
p.play({ command: 'newHeadlines', articles });
if(p.C.value) {
p.play(`Here are the (latest|recent) articles on ${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);
});
});
const confirmation = context(() => {
intent('yes', async (p) => {
for(let i = 0; i < savedArticles.length; i++){
p.play({ command: 'highlight', article: savedArticles[i]});
p.play(`${savedArticles[i].title}`);
}
})
intent('no', (p) => {
p.play('Sure, sounds good to me.')
})
})
intent('open (the|) (article|) (number|) $(number* (.*))', (p) => {
if(p.number.value) {
p.play({ command:'open', number: p.number.value, articles: savedArticles})
}
})
intent('(go|) back', (p) => {
p.play('Sure, going back');
p.play({ command: 'newHeadlines', articles: []})
})
@Aanya9693
Copy link

@anshika42 Can you please send the complete code please? it's not working for me, I tried a lot of times.

@moabz49
Copy link

moabz49 commented Aug 18, 2023

mohitksoni

@mohitksoni thanks friend

@GoutamSachdev
Copy link

Use this

api.axios.get(NEWS_API_URL).then((response)=>{
let {articles} = response.data;
instead of this>>
api.request(NEWS_API_URL, {headers: {"user-agent": 'user agent' }}, (error, response, body) => {
const { articles } = JSON.parse(body);

@GoutamSachdev
Copy link

Use this

api.axios.get(NEWS_API_URL).then((response)=>{
let {articles} = response.data;

instead of this>>
api.request(NEWS_API_URL, {headers: {"user-agent": 'user agent' }}, (error, response, body) => {
const { articles } = JSON.parse(body);

@Satwik1900
Copy link

@GoutamSachdev Could you provide the full code please. I am getting stuck repeatedly.

@ilija-mihajlovic
Copy link

ilija-mihajlovic commented Aug 31, 2023

Here is the fully working code. The only thing I changed is one intent from "Give me the latest news" to "Show me the latest news", don't forget to change that in the front end. The previous code did not work because the newsapi changed their endpoints.

Here is the link to repository where the code is https://github.com/ilija-mihajlovic/alan-ai-working-code

@otabek0302
Copy link

If request is not working try with axios or read document from Alan AI.
There some example from Alan AI with axios :

const SERVICE_URL = "http://api.openweathermap.org/data/2.5/weather";
const appid = "4acdb6432d18884ebc890c13a9c3cc85";

intent('What is the weather in $(LOC)', p => {
const request_url = ${SERVICE_URL}?appid=${appid}&q=${p.LOC}&units=imperial;
api.request(request_url, (error, res, body) => {
if (error || res && res.statusCode !== 200) {
p.play('Could not get weather information');
} else {
let data = JSON.parse(body);
console.log({data});
p.play(The temperature in ${p.LOC} is ${Math.floor(data.main.temp)} degrees in Fahrenheit);
}
});
});

@nuelheran
Copy link

nuelheran commented Sep 16, 2023

I want to direct the news on my project to national news in my country (Indonesia), but why can't my program capture images related to the news? does anyone know how to fix this?
ask question

@Sheri78
Copy link

Sheri78 commented Dec 26, 2023

Here is the fully working code. The only thing I changed is one intent from "Give me the latest news" to "Show me the latest news", don't forget to change that in the front end. The previous code did not work because the newsapi changed their endpoints.

Here is the link to repository where the code is https://github.com/ilija-mihajlovic/alan-ai-working-code

thanks this is working.but still have some issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment