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

errors in Alan studio script #14

Open
harshithvh opened this issue Jun 1, 2022 · 11 comments
Open

errors in Alan studio script #14

harshithvh opened this issue Jun 1, 2022 · 11 comments

Comments

@harshithvh
Copy link

I guess they have changed the whole syntax in alan studio
neither p.source.value or p.source works, hence no request is being made to the news api and there are many more errors
+1 to the previous issue
please update the script

@harshithvh
Copy link
Author

Got this error
{
"articles": {
"code": "userAgentMissing",
"message": "Please set your User-Agent header to identify your application. Anonymous requests are not allowed.",
"status": "error"
},
"command": "newHeadlines"
}
any help would be appreciated

@ElieMirza
Copy link

ElieMirza commented Jun 10, 2022

Got this error { "articles": { "code": "userAgentMissing", "message": "Please set your User-Agent header to identify your application. Anonymous requests are not allowed.", "status": "error" }, "command": "newHeadlines" } any help would be appreciated

This happened to me, so I switched to axios and that solved the problem. Turns out that newsapi doesn't like to receive request without headers, especially the User-Agent. So an async await function got the job done, here's how I solved it:

              try {
                 const response = await api.axios.get(url, {
                 headers: {
                     'User-Agent': "PostmanRuntime/7.29.0"; // You can use any User-Agent, I used Postman, Chrome or Firefox should work aswell
                      }
             });

             if(!response.data.articles.length) {
                p.play("Sorry, please try searching from a different source");
                return;
             } else {
                const articles = response.data.articles;
                savedArticles = articles;
                p.play({ command: "newHeadlines", articles });
                p.play(`Here are the news from (latest|recent) ${p.SOURCE.value}`);
             }
         } catch(error) {
             p.play("Sorry, an error has occurred");
             console.log(error);
         }

@TEMPTUN
Copy link

TEMPTUN commented Jun 20, 2022

Got this error { "articles": { "code": "userAgentMissing", "message": "Please set your User-Agent header to identify your application. Anonymous requests are not allowed.", "status": "error" }, "command": "newHeadlines" } any help would be appreciated

This happened to me, so I switched to axios and that solved the problem. Turns out that newsapi doesn't like to receive request without headers, especially the User-Agent. So an async await function got the job done, here's how I solved it:

              try {
                 const response = await api.axios.get(url, {
                 headers: {
                     'User-Agent': "PostmanRuntime/7.29.0"; // You can use any User-Agent, I used Postman, Chrome or Firefox should work aswell
                      }
             });

             if(!response.data.articles.length) {
                p.play("Sorry, please try searching from a different source");
                return;
             } else {
                const articles = response.data.articles;
                savedArticles = articles;
                p.play({ command: "newHeadlines", articles });
                p.play(`Here are the news from (latest|recent) ${p.SOURCE.value}`);
             }
         } catch(error) {
             p.play("Sorry, an error has occurred");
             console.log(error);
         }

can you please give a detailed code beacuse still im not able to get api call properly

@ElieMirza
Copy link

Got this error { "articles": { "code": "userAgentMissing", "message": "Please set your User-Agent header to identify your application. Anonymous requests are not allowed.", "status": "error" }, "command": "newHeadlines" } any help would be appreciated

This happened to me, so I switched to axios and that solved the problem. Turns out that newsapi doesn't like to receive request without headers, especially the User-Agent. So an async await function got the job done, here's how I solved it:

              try {
                 const response = await api.axios.get(url, {
                 headers: {
                     'User-Agent': "PostmanRuntime/7.29.0"; // You can use any User-Agent, I used Postman, Chrome or Firefox should work aswell
                      }
             });

             if(!response.data.articles.length) {
                p.play("Sorry, please try searching from a different source");
                return;
             } else {
                const articles = response.data.articles;
                savedArticles = articles;
                p.play({ command: "newHeadlines", articles });
                p.play(`Here are the news from (latest|recent) ${p.SOURCE.value}`);
             }
         } catch(error) {
             p.play("Sorry, an error has occurred");
             console.log(error);
         }

can you please give a detailed code beacuse still im not able to get api call properly

This is the code that gets the first command, you can update it for the rest of the commands:

intent(Give me the news from $(SOURCE* (.+)), (p) => { let NEWS_API_URL = https://newsapi.org/v2/top-headlines?apiKey=${API_KEY}&language=en`;
let query = p.SOURCE.value;
if(typeof(query) === "string") {
NEWS_API_URL = ${NEWS_API_URL}&sources=${p.SOURCE.value.toLowerCase().split(" ").join('-')};
}

getArticles(NEWS_API_URL);

async function getArticles(url) {
     try {
         const response = await api.axios.get(url, {
             headers: {
                 'User-Agent': "PostmanRuntime/7.29.0"
             }
         });

         if(!response.data.articles.length) {
            p.play("Sorry, please try searching from a different source");
            return;
         } else {
            const articles = response.data.articles;
            savedArticles = articles;
            p.play({ command: "newHeadlines", articles });
            p.play(`Here are the news from (latest|recent) ${p.SOURCE.value}`);
         }
     } catch(error) {
         p.play("Sorry, an error has occured");
         console.log(error);
     }
    p.play("Would you like me to read the headlines? Please answer by yes or no");
    p.then(confirmation);
}
});`

@mohit-1311
Copy link

It still gives me error. Can you please share your code ?

@TEMPTUN
Copy link

TEMPTUN commented Jul 7, 2022 via email

@mohit-1311
Copy link

Sure! Just remove that try and it brackets it will work for sure right now i cant send you the code but this will work and one more suggestion you should leave this project becoz it will give error after while further in video as libraries are now updated you should try to do latest videos project by JSM

On Thu, 7 Jul 2022, 12:40 pm Mohit Srivastava, @.> wrote: It still gives me error. Can you please share your code ? — Reply to this email directly, view it on GitHub <#14 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXIQ2NUOVYR6LMIGSJRLYU3VSZ7FZANCNFSM5XRU6J5A . You are receiving this because you commented.Message ID: @.>

let savedArticles = [];

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('-')}`
}

getArticles(NEWS_API_URL);

async function getArticles(url) {
try {
const response = await api.axios.get(url, {
headers: {
'User-Agent': "Mozilla/5.0"
}
});

     if(!response.data.articles.length) {
        p.play("Sorry, please try searching from a different source");
        return;
     } else {
        const articles = response.data.articles;
        savedArticles = articles;
        p.play({ command: "newHeadlines", articles });
        p.play(`Here are the news from (latest|recent) ${p.SOURCE.value}`);
     }
 } catch(error) {
     p.play("Sorry, an error has occured");
     console.log(error);
 }
p.play("Would you like me to read the headlines? Please answer by yes or no");
p.then(confirmation);

}

})

Please suggest what should be done differently

@TEMPTUN
Copy link

TEMPTUN commented Jul 7, 2022 via email

@thathva
Copy link

thathva commented Sep 12, 2022

There is another fix that can be done without using axios.

 const options = {
      url: NEWS_API_URL,
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0'
      }
    };

This snippet can be used in places where the api.request is used. Replace NEWS_API_URL with options parameter in api.request.

@enigmacoder-bot
Copy link

enigmacoder-bot commented Oct 2, 2022

The problem is due to api.request requiring additional parameters.
Try this code. be sure to add api keys.

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, {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 the (latest|recent) ${p.source.value}.`);
   
    });
});

@carlyjames
Copy link

The problem is due to api.request requiring additional parameters. Try this code. be sure to add api keys.

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, {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 the (latest|recent) ${p.source.value}.`);
   
    });
});

thanks for these,really worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants