Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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: []})
})
@ljunior23
Copy link

I'm also having the same problem any help? @rustam-maqsood, I got undefined for the !data.length

@magovansh9
Copy link

I have spend 2 days on this problem and just found the solution and get news data, Just give replace this code and your all problems will be solved Inshallah

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

    if(!data.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}.`);
});

This worked, thanks a lot!🥳 Basically you have to change the api.request and include the following {headers: {"user-agent": 'user agent' }}.
So api.request(NEWS_API_URL, (error, response, body) => becomes api.request(NEWS_API_URL, {headers: {"user-agent": 'user agent' }}, (error, response, body) => everywhere in the Alan Script

@mohitksoni
Copy link

api.axios.get(NEWS_API_URL).then((response)=>{
        let {articles} = response.data;
        if(!articles.length){
            p.play('Sorry, please try searching for news from a different source');
            return;
        }
        savedArticles = articles;
        p.play({command: 'newsHeadlines', articles});
        p.play(`Here are the (latest|recent) ${p.source.value}`);
        
        p.play('Would you like me to read the headlines?');
        p.then(confirmation);
    })

Try to fetch the requests in the way mentioned above. It will resolve all the errors related to length and others as well.

@harsharma3107
Copy link

can you share your code please? @mohitksoni

@JakeTylerSimon
Copy link

This is the working code for this problem as of August 26th 22

intent('What does this app do?', 'What can I do here?',
reply('This is a news project.'));

const API_KEY = 'YOUR KEY';
let savedArticles = [];

// News by Source
intent((Give me | tell me | show me) the news (from | by | on | in) $(source* .+), (p) => {
let NEWS_API_URL = https://newsapi.org/v2/top-headlines;

if(p.source.value) {
    p.source.value=p.source.value.toLowerCase().split(" ").join("-");
    NEWS_API_URL = `${NEWS_API_URL}?sources=${p.source.value}&apiKey=${API_KEY}`
}

api.request(NEWS_API_URL, {headers: {"user-agent": 'user agent' }}, (error, response, body) => {
    const { totalResults, articles } = JSON.parse(body);
    
    if(totalResults == 0) {
        p.play('Sorry, please try searching for news from a different source');
        return;
    }
    
    savedArticles = articles;
    
    p.play({ command: 'newHeadlines', articles });
    p.play(`Here are (latest|recent) ${p.source.value} news.`);

    p.play('Would you like me to read the headlines?');

});

})

@Abdul100-han
Copy link

I'm also having the same problem any help? @rustam-maqsood, I got undefined for the !data)

I am getting undefined as i call data from api. Help anyone image

I also faced same error(getting undefined after calling the data from the api), but i was able to solve it by adding this(country=us&) to the line of the code
i changed
let NEWS_API_URL = https://newsapi.org/v2/top-headlines?apiKey=${API_KEY};
to
let NEWS_API_URL = https://newsapi.org/v2/top-headlines?country=us&apiKey=${API_KEY};
And it worked!!

@mostafa-habib
Copy link

This code is working very well.
`intent('What does this app do?', 'What can I do here?',
reply('This is a news project.'));

const API_KEY = '4021f8f086174735b3c487d29e7644ac';
let savedArticles = [];
// News by Source
intent('(Give me | tell me | show me) the latest news', (p) => {
let NEWS_API_URL = https://newsapi.org/v2/everything?domains=wsj.com&apiKey=4021f8f086174735b3c487d29e7644ac;

api.request(NEWS_API_URL, {headers: {"user-agent": 'user agent' }}, (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 (latest|recent) news.`);

p.play('Would you like me to read the headlines?');
p.then(confirmation);

});
})
intent('(Give me | tell me | show me) the news (from | by | on | in) $(source* .+)', (p) => {
let NEWS_API_URL = https://newsapi.org/v2/top-headlines;
if(p.source.value) {
p.source.value=p.source.value.toLowerCase().split(" ").join("-");
NEWS_API_URL = ${NEWS_API_URL}?sources=${p.source.value}&apiKey=${API_KEY}
}

api.request(NEWS_API_URL, {headers: {"user-agent": 'user agent' }}, (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 (latest|recent) ${p.source.value} news.`);

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;
if(p.term.value) {
p.term.value=p.term.value.toLowerCase().split(" ").join("-");
NEWS_API_URL = ${NEWS_API_URL}?q=${p.term.value}&apiKey=${API_KEY}
}

api.request(NEWS_API_URL, {headers: {"user-agent": 'user agent' }}, (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) 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}).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* .+) $(N news|headlines), (p) => {
let NEWS_API_URL = https://newsapi.org/v2/top-headlines;

if(p.C.value) {
console.log(p.C.value)
NEWS_API_URL = ${NEWS_API_URL}?category=${p.C.value}&country=us&apikey=${API_KEY}
}

api.request(NEWS_API_URL, (error, response, body) => {
const { articles} = JSON.parse(body);

if(!articles) {
    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: []})
})`

@Hacking-NASSA-with-HTML
Copy link

Jeeez, people can someone upload fully working code for this gizmo, please 🤔?
It doesn't work on win11 chrome in any variants 👋
I've tried a Postman to check GET request from news API, it works perfectly.
Something wrong with Alan.
Help, please 🤷‍♂️

@Hacking-NASSA-with-HTML
Copy link

@JakeTylerSimon thank you, sir! 👍
It works now! 🤝

@iMakkHD
Copy link

iMakkHD commented Sep 19, 2022

Fully working code block!

This is from the code blocks above. This is for copy and paste purposes. I am not the author of these solutions.

Fully working as of 9/19/2022

// News by Source
intent('Give me the news from $(source* (.*))', (p) => {
    let NEWS_API_URL = `https://newsapi.org/v2/top-headlines?country=us&apiKey=${API_KEY}`;
    
    if(p.source.value) {
        p.source.value=p.source.value.toLowerCase().split(" ").join("-");
        NEWS_API_URL = `${NEWS_API_URL}?sources=${p.source.value}&apiKey=${API_KEY}`
    }

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

        if(totalResults == 0) {
            p.play('Sorry, please try searching for news from a different source');
            return;
        }

        savedArticles = articles;

        p.play({ command: 'newHeadlines', articles });
        p.play(`Here are (latest|recent) ${p.source.value} news.`);

        p.play('Would you like me to read the headlines?');

    });
})

@sekhar153
Copy link

intent('Give me the news from $(source* (.*))', (p) => {
let NEWS_API_URL = https://newsapi.org/v2/top-headlines?country=us&apiKey=${API_KEY};

if(p.source.value) {
    p.source.value=p.source.value.toLowerCase().split(" ").join("-");
    NEWS_API_URL = `${NEWS_API_URL}?sources=${p.source.value}&apiKey=${API_KEY}`
}

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

    if(totalResults == 0) {
        p.play('Sorry, please try searching for news from a different source');
        return;
    }

    savedArticles = articles;

    p.play({ command: 'newHeadlines', articles });
    p.play(`Here are (latest|recent) ${p.source.value} news.`);

    p.play('Would you like me to read the headlines?');

});

})

yes this one working ,thnks

@arpita-24
Copy link

{headers: {"user-agent": 'user agent' }}

This worked. Thanks

@zafair122
Copy link

why my newscard not showing on screen and also its give me these errors? any solution? erro2

is your issue resolved? same problem with me

i was getting the same problems and what did come to my mind was that our source is not working to get the data i think soooo

@SushantKawale
Copy link

I am able oh hear the news but the news are not displaying on screen What should I do?

@carlyjames
Copy link

hello guys,am having issues getting the latest news 😭😭
latest news error

@SushantKawale
Copy link

Try this code
// 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'))
//intent('Start a command',(p) =>{
//p.play({command:'testCommand'})
//})

const API_KEY='4d90c24ea6f44bf780ab37656e458b79';
let savedArticles = [];
//News By Source

intent('Give me the news from $(source* (.*))',(p) => {

let NEWS_API_URL = 'https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=4d90c24ea6f44bf780ab37656e458b79';

if(p.source.value) {
    NEWS_API_URL = `${NEWS_API_URL}&sources=${p.source.value.toLowerCase().split(" ").join('-')}`
}

api.axios.get(NEWS_API_URL).then((response)=>{
    let {articles} = response.data;
    if(!articles.length){
        p.play('Sorry, please try searching for news from a different source');
        return;
    }
    savedArticles = articles;
    p.play({command: 'newsHeadlines', 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('give information about $(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.axios.get(NEWS_API_URL).then((response)=>{
    let {articles} = response.data;
    
    if(!articles.length) {
        p.play('Sorry, please try searching for something else.');
        return;
    }
    
    savedArticles = articles;
    
    p.play({ command: 'newsHeadlines', 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.axios.get(NEWS_API_URL).then((response)=>{
    let {articles} = response.data;
    
    if(!articles.length) {
        p.play('Sorry, please try searching for a different category.');
        return;
    }
    
    savedArticles = articles;
    
    p.play({ command: 'newsHeadlines', 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: 'newsHeadlines', articles: []})
})

@Fazman-s
Copy link

hello guys,am having issues getting the latest news 😭😭 latest news error

use this code!!!
//latest news
intent('(give |show |tell) me the latest news', (p) => {
let NEWS_API_URL = https://newsapi.org/v2/top-headlines?apiKey=${API_KEY}&country=in;

if(p) {
    NEWS_API_URL = `${NEWS_API_URL}`;
}

api.request(NEWS_API_URL, {headers: {"user-agent": 'user agent' }}, (error, response, body) => {
const { articles } = JSON.parse(body);
   
    savedArticles = articles;
    
    p.play({ command: 'newHeadlines', articles });
    p.play(`Here are the (latest|recent) articles `);
    
    p.play('Would you like me to read the headlines?');
    p.then(confirmation);
});

})

@ayush9009
Copy link

// News by Categories
const CATEGORIES = ['business', 'entertainment', 'general', 'health', 'science', 'sports', 'technology'];
const CATEGORIES_INTENT = ${CATEGORIES.map((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.axios.get(NEWS_API_URL).then((response)=>{
let {articles} = response.data;

if(!articles.length) {
    p.play('Sorry, please try searching for a different category.');
    return;
}

savedArticles = articles;

p.play({ command: 'newsHeadlines', 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);

});

///i dont get the news from categories ,please anyone help me,i try to resolve it from 5 days but still not able to solve it

@ayush9009
Copy link

${CATEGORIES.map((category) => category {category}).join('|')}

can you please share the code for categories

@ayush9009
Copy link

hello guys,am having issues getting the latest news 😭😭 latest news error

use this code!!! //latest news intent('(give |show |tell) me the latest news', (p) => { let NEWS_API_URL = https://newsapi.org/v2/top-headlines?apiKey=${API_KEY}&country=in;

if(p) {
    NEWS_API_URL = `${NEWS_API_URL}`;
}

api.request(NEWS_API_URL, {headers: {"user-agent": 'user agent' }}, (error, response, body) => {
const { articles } = JSON.parse(body);
   
    savedArticles = articles;
    
    p.play({ command: 'newHeadlines', articles });
    p.play(`Here are the (latest|recent) articles `);
    
    p.play('Would you like me to read the headlines?');
    p.then(confirmation);
});

})

brother can you please share your code for categories section,because i tried lot but still not able to get the different categories news,when i search give me the latest technology news it responds me ,please try searching news from different source
PLEASE HELP

@Fazman-s
Copy link

hello guys,am having issues getting the latest news 😭😭 latest news error

use this code!!! //latest news intent('(give |show |tell) me the latest news', (p) => { let NEWS_API_URL = https://newsapi.org/v2/top-headlines?apiKey=${API_KEY}&country=in;

if(p) {
    NEWS_API_URL = `${NEWS_API_URL}`;
}

api.request(NEWS_API_URL, {headers: {"user-agent": 'user agent' }}, (error, response, body) => {
const { articles } = JSON.parse(body);
   
    savedArticles = articles;
    
    p.play({ command: 'newHeadlines', articles });
    p.play(`Here are the (latest|recent) articles `);
    
    p.play('Would you like me to read the headlines?');
    p.then(confirmation);
});

})

brother can you please share your code for categories section,because i tried lot but still not able to get the different categories news,when i search give me the latest technology news it responds me ,please try searching news from different source PLEASE HELP

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|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=in`;

if(p.C.value) {
    NEWS_API_URL = `${NEWS_API_URL}&category=${p.C.value}`
}

api.request(NEWS_API_URL, {headers: {"user-agent": 'user agent' }}, (error, response, body) => {
const { totalResults, 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}.`);
    }
    p.play('Would you like me to read the headlines?');
    p.then(confirmation);
    
});

})

@ayush9009
Copy link

ayush9009 commented Nov 20, 2022 via email

@Kajal-Abacus
Copy link

anyone please give fully working code .

@HackX-IN
Copy link

Thank You All Guys For Helping Me Out With This !

@Indradeep-Pal
Copy link

why is it not fetching any data from news api . I copied code fromone of these comments and it worked but when i console.log(articles ) it is showing undefined. Any help from peers would be appreciated .

@Mudassir55
Copy link

still facing same undefined error

@Mudassir55
Copy link

why is it not fetching any data from news api . I copied code from one of these comments and it worked but when i console.log(articles ) it is showing undefined. Any help from peers would be appreciated .

facing same issue when use conssole.log they data type is undefined

@Mudassir55
Copy link

can anyone share fully functional code

@yaninyzwitty
Copy link

I have spend 2 days on this problem and just found the solution and get news data, Just give replace this code and your all problems will be solved Inshallah

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

    if(!data.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}.`);
});

@yaninyzwitty
Copy link

this is great!!

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