Skip to content

arassp/daily-news-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📰 Daily News Email Bot

License Python Version Powered by NewsAPI

✔️ About

A totally automated GitHub actions workflow that emails you daily tech (or any other category) news summaries to your inbox using GitHub Actions, NewsAPI, and SMTP.

⚙️ Program Workflow

sequenceDiagram
    participant GitHub Actions
    participant NewsAPI
    participant SMTP Server
    participant Your Inbox

    GitHub Actions->>NewsAPI: 1. Request tech news
    NewsAPI-->>GitHub Actions: 2. Return news data
    GitHub Actions->>SMTP Server: 3. Send formatted email
    SMTP Server->>Your Inbox: 4. Deliver email
    Note right of GitHub Actions: Runs daily at 18:00 UTC
Loading

✨ Features

📩 Automated Daily Tech News Delivery

  • Fetches the top 5 trending tech news articles daily from NewsAPI.org.
  • Can be modified to fetch different news categories.
  • Sends you a daily email with article summaries and links.
  • Runs daily at 18:00 UTC via GitHub Actions.

🔐 Secure Email Handling

  • Uses SMTP authentication to send emails securely.
  • Supports Gmail, Outlook, Yahoo, and custom SMTP servers.
  • Email credentials are stored securely in GitHub Secrets.

⚡ Automated Integration with GitHub Actions

  • Runs automatically without manual actions.
  • But can also be manually triggered via GitHub Actions workflow dispatch.
  • Hosted on GitHub’s free CI/CD runners.

⚠️ Error Handling and Logging

  • Catches API failures (i.e., network issues, API rate limits).
  • Logs the error messages for debugging.
  • Provides warnings if no news articles are found.

🚀 Setup & Configuration

1️⃣ Add API Key & SMTP Credentials as GitHub Secrets

Go to Settings > Secrets and variables > Actions and add:

Secret Name Value
NEWSAPI_KEY Your NewsAPI key (https://newsapi.org).
SENDER_EMAIL Your email address for sending news.
RECEIVER_EMAIL The recipient’s email address.
SMTP_SERVER SMTP server (i.e., smtp.gmail.com).
SMTP_PORT SMTP port (465 for SSL or 587 for TLS).
SMTP_PASSWORD Your email password or app password.

2️⃣ Modify GitHub Actions Workflow (.github/workflows/schedule.yml)

  • Update the schedule.yml if needed (default is 18:00 UTC daily).
  • Modify environment variables if using a different/custom SMTP provider.

3️⃣ Test the Workflow

  • The workflow runs automatically at 18:00 UTC.
  • But you can also manually trigger it under GitHub Actions > Run Workflow.

📰 How to Change The News Category

You can change news category by modifying the NewsAPI query in the main.py script. Open main.py and locate this line:

category = "technology"

Possible categories with NewsAPI are: business, entertainment, general, health, science, sports, and technology.

FAQs❓

1. Can I change the frequency of the email updates? Yes, just open schedule.yml and modify the cron schedule to change the timing.
Example for hourly runs: - cron: '0 * * * *'
2. Can I use this bot with other news APIs? Yes, but you’ll need to modify the get_news() function in main.py to make API requests to the new service and parse their response format.
3. Can I customize the email content? Yes, just edit the get_tech_news() or send_email() functions in main.py. You can change the formatting, number of articles, or add new details to the email body.
4. Can I host this on a local machine instead of GitHub Actions? Yes, install the required dependencies and set up a cron job to run main.py on your local machine. Use a .env file to manage your credentials securely.
5. How do I set up my email credentials securely? Store your credentials (STMP_SERVER,NEWSAPI_KEY,etc...) in GitHub Secrets as shown in the setup and configuration steps above.
IMPORTANT: NEVER hard-code any sensitive information directly in your code.
6. How is SMTP security handled, and is it safe to store sensitive information in GitHub Secrets? SMTP credentials (such as email, password, server, and port number) are stored securely in GitHub Secrets, which are encrypted and accessible only as environment variables during workflow action. You can also use this bot with an email address that you don’t frequently use (SENDER_EMAIL) while adding your primary email (RECEIVER_EMAIL) address as the recipient in GitHub Secrets. Steps for this are shown in the setup and configuration steps above. And while it may not always be necessary, I recommend using App Passwords instead of your actual email password for added protection.
7. What are App Passwords? App Passwords are platform-specific passwords that allows third-party apps (like this bot) to access your email account without exposing your main account password.
Provider Steps to Generate App Password
Gmail
  1. Go to Google Account Security.
  2. Enable 2-Step Verification if not already enabled.
  3. Under "Signing in to Google," select App Passwords.
  4. Give it any name, but something that you recognize and click Generate
  5. Copy the password (delete the spaces) and add it to your SMTP_PASSWORD secret in GitHub.
Outlook/Hotmail
  1. Go to Microsoft Account Security.
  2. Enable Two-Step Verification if not already enabled.
  3. Select Create a New App Password.
  4. Copy the password and add it to your SMTP_PASSWORD secret in GitHub.
Yahoo Mail
  1. Go to Yahoo Account Security.
  2. Enable Two-Step Verification.
  3. Click Generate App Password, then select Other and give it a name.
  4. Copy the password and add it to your SMTP_PASSWORD secret in GitHub.
8. Is it possible to use this bot with multiple recipients? Yes, but with some modification. First, you need to add the recipients to RECEIVER_EMAIL in GitHub secrets, seperated by commas. Let's say you have: [email protected], [email protected]. Then you need to modify the send_email function in main.py so that it can handle multiple recipients. Now, the below line splits the receiver_email string (Stored in GitHub Secrets) into a list of email addresses, allowing you to handle multiple recipients.

recipients = email_config['receiver_email'].split(",") 

Now add a loop to send emails to each recipient individually.

    for recipient in recipients:
    recipient = recipient.strip()  # Remove spaces
    msg['To'] = recipient
    server.send_message(msg)
    logging.info(f"Email sent to {recipient}")

And a log message for each recipient for debugging

    logging.info(f"Email sent to {recipient}")

9. What if there are no articles available in the selected category? The bot will log a warning: No articles found for category: and skip sending the email.
10. Can I change the number of news articles in the email? Yes, modify the [:5] in the get_news() function to your desired number of articles.
Example: articles = response.json().get('articles', [])[:10] # Gets top 10 articles
11. Can I change the email format to HTML instead of plain text? Yes, modify the send_email() function and change the email body format from plain to html.

👏 Support

Feel free to contribute, suggest improvements, or submit PRs.
If you found the project helpful, I'd appreciate a follow :) Follow on GitHub

About

Fetches top news via NewsAPI and emails updates daily using SMTP & GitHub Actions

Topics

Resources

License

Stars

Watchers

Forks

Languages