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

Added Weather Bot for GMAIL #183

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Gmail_Weather Bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Gmail Bot - Weather Report

This Python script allows you to send the weather report of a specified city via Gmail.

## Prerequisites

- Python 3.x
- `requests` library: You can install it using `pip install requests`.
- `secure-smtplib` library: You can install it using `pip install secure-smtplib`.
- Gmail account: You will need a Gmail account to send the email.
- OpenWeatherMap API key: You need to sign up and obtain an API key from OpenWeatherMap (https://openweathermap.org/) to fetch weather data.

## Setup

1. Clone this repository or download the script file `gmail_bot.py` to your local machine.

2. Install the required dependencies by running the following command:

3. Open the `gmail_bot.py` script in a text editor.

4. Replace the following placeholders in the script with your own values:
- `GMAIL_USERNAME`: Your Gmail email address.
- `GMAIL_PASSWORD`: Your Gmail app password (generated for this script).
- `API_KEY`: Your OpenWeatherMap API key.

5. Save the script.

## Usage

1. Open a terminal or command prompt.

2. Navigate to the directory where the `gmail_bot.py` script is located.

3. Run the script using the following command:

4. Enter the city name when prompted.

5. Enter the email address where you want to receive the weather report.

6. The script will fetch the weather report and send it via email to the specified email address.

Note: Make sure to enable "Less Secure Apps" access in your Gmail account settings, and generate an app password specifically for this script.

## License

This script is licensed under the [MIT License](LICENSE).
70 changes: 70 additions & 0 deletions Gmail_Weather Bot/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import smtplib
import requests

# Gmail credentials
GMAIL_USERNAME = "YOUR_GMAIL_ADDRESS"
GMAIL_PASSWORD = "YOUR_PASSWORD"

# OpenWeatherMap API key
API_KEY = "YOUR_API_KEY"

def send_email(subject, body, to_email):
try:
# Connect to Gmail's SMTP server
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(GMAIL_USERNAME, GMAIL_PASSWORD)

# Create the email message
subject = subject.encode("utf-8")
body = body.encode("utf-8")
message = f"Subject: {subject}\n\n{body}"

# Send the email
server.sendmail(GMAIL_USERNAME, to_email, message)

# Close the connection
server.close()

print("Email sent successfully!")
except Exception as e:
print(f"Error sending email: {str(e)}")

def get_weather(city):
try:
# Make a request to OpenWeatherMap API
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
response = requests.get(url)
data = response.json()

# Extract relevant weather information
weather_main = data["weather"][0]["main"]
weather_description = data["weather"][0]["description"]
temperature = data["main"]["temp"]
humidity = data["main"]["humidity"]

# Create the weather report message
report = f"Weather report for {city}:"
report += f"Weather: {weather_main} ({weather_description})"
report += f"Temperature: {temperature}°C"
report += f"Humidity: {humidity}%"

return report
except Exception as e:
print(f"Error fetching weather data: {str(e)}")

# Get the city from the user
city = input("Enter the city name: ")

# Fetch the weather report
weather_report = get_weather(city)

if weather_report:
# Send the weather report via email
subject = f"Weather Report for {city}"
to_email = input("Enter your email address: ")

send_email(subject, weather_report, to_email)
else:
print("Unable to fetch weather report for the enterd city.")