Prompt Title: Email News Extraction

Created 1 year ago
Votes 0
Views 88 times
0
Human: I am a google spread user. I want to extract 10 news articles related to the keyword I want and send them to my email using the Apps Script function. Please tell me how to do it. Answer in English.
ChatGPT:

To extract news articles related to a specific keyword and send them to your email using Apps Script in Google Sheets, you can follow these steps:

  1. Open your Google Sheets document.
  2. Click on "Extensions" in the menu bar, then select "Apps Script."
  3. A new tab will open with the Apps Script editor.
  4. In the editor, delete any existing code and replace it with the following code:
javascript
function sendNewsToEmail() { var keyword = "your_keyword"; // Replace "your_keyword" with the keyword you want to search // Fetch news articles using Google News API var url = "https://newsapi.org/v2/everything?q=" + encodeURIComponent(keyword) + "&apiKey=YOUR_API_KEY"; var response = UrlFetchApp.fetch(url); var articles = JSON.parse(response.getContentText()).articles; // Select the first 10 articles articles = articles.slice(0, 10); // Compose email body with articles var emailBody = ""; for (var i = 0; i < articles.length; i++) { var article = articles[i]; emailBody += "<h3>" + article.title + "</h3>"; emailBody += "<p>" + article.description + "</p>"; emailBody += "<a href='" + article.url + "'>Read more</a><br><br>"; } // Send email var recipient = "[email protected]"; // Replace with your email address var subject = "Top 10 news articles for keyword: " + keyword; MailApp.sendEmail({ to: recipient, subject: subject, htmlBody: emailBody }); }
  1. Replace "your_keyword" in the code with the keyword you want to search for news articles.
  2. Obtain an API key from newsapi.org by signing up for an account and create a new API key.
  3. Replace "YOUR_API_KEY" in the code with the API key you obtained from News API.
  4. Replace "[email protected]" in the code with your email address where you want to receive the articles.
  5. Save the script by clicking on the floppy disk icon or pressing Ctrl + S.
  6. Click on the "Run" icon (play button) to execute the sendNewsToEmail function for the first time.
  7. A dialog box will appear asking for authorization. Click "Review Permissions" and follow the prompts to grant the necessary permissions for the script.
  8. After authorizing, click on the "Run" icon again to execute the function.
  9. The function will fetch the news articles, compose an email with the top 10 articles, and send it to the specified email address.

Note: Make sure to enable the "Apps Script API" in your Google Cloud Platform project associated with the Google account you're using.

That's it! You should receive an email with the top 10 news articles related to your keyword. You can run this script manually whenever you want to get the latest articles, or you can set up a time-driven trigger to automatically send emails at specific intervals.

Human: Thanks for the reply. But please rewrite the code in a way to fetch news articles from newsapi without importing api. Enter 'Artificial Intelligence' in Korean as the keyword. My email address is [email protected]. Answer in English. Please write in Korean language.
ChatGPT:

Sure! Here's the modified code to fetch news articles from News API without using an external library:

javascript
function sendNewsToEmail() { var keyword = "인공지능"; // Enter your keyword in Korean // Fetch news articles using News API var url = "https://newsapi.org/v2/everything?q=" + encodeURIComponent(keyword) + "&apiKey=YOUR_API_KEY"; var response = UrlFetchApp.fetch(url); var data = JSON.parse(response.getContentText()); var articles = data.articles; // Select the first 10 articles articles = articles.slice(0, 10); // Compose email body with articles var emailBody = ""; for (var i = 0; i < articles.length; i++) { var article = articles[i]; emailBody += "<h3>" + article.title + "</h3>"; emailBody += "<p>" + article.description + "</p>"; emailBody += "<a href='" + article.url + "'>자세히 보기</a><br><br>"; } // Send email var recipient = "[email protected]"; // Replace with your email address var subject = "인공지능과 관련된 상위 10개 뉴스 기사"; MailApp.sendEmail({ to: recipient, subject: subject, htmlBody: emailBody }); }

위의 코드를 사용하여 News API에서 뉴스 기사를 가져온 후, 해당 기사를 이메일로 보낼 수 있습니다. 코드 내의 주석을 따라 진행하시면 됩니다.

주의사항: 코드 내의 "YOUR_API_KEY" 부분을 News API에서 발급받은 API 키로 대체해야 합니다.

이메일 주소를 "[email protected]"로 수정하였습니다. 필요에 따라 코드에서 해당 부분을 변경하십시오.

이제 코드를 실행하면 "인공지능"과 관련된 상위 10개의 뉴스 기사가 이메일로 전송됩니다.

avatar
Jong-Ki Lee
Shared 8 prompts
Created 1 year ago

Leave a Comment