In this tutorial, we will learn how to create a simple bot using Python. A bot is a software application that performs automated tasks or actions, such as replying to messages, posting updates on social media, or even playing games.
Bots can be useful for various purposes such as automating repetitive tasks or gathering information from the web.
We will create a bot that can fetch current weather data and display it to users, using the OpenWeatherAPI service. The bot will take in the city name and output its current weather data.
Prerequisites
Before we get started, make sure that you have the following installed on your system:
- Python 3.x: You can download the latest version of Python from the official website.
- Requests: A Python library used for making HTTP requests. Install it by running:
pip install requests
Step 1: Get an API Key from OpenWeatherAPI
To fetch weather data from OpenWeatherAPI, we need an API key. Follow these steps to get your API key:
- Visit the OpenWeatherAPI website.
- Sign up for a free account.
- After signing up, go to the API section or visit the API keys page.
- Create a new key or copy the existing one.
Save your API Key in a text file for future use.
Step 2: Create a Python Script
Create a new Python script file named weather_bot.py.
Step 3: Import the Required Libraries
At the beginning of the script, import the required libraries:
1 2 |
import requests import json |
Step 4: Add the API Key and URL
Add a variable containing your API key and the API URL to your script:
1 2 |
API_KEY = "your_api_key" API_URL = "http://api.openweathermap.org/data/2.5/weather" |
Replace “your_api_key” with the API key you obtained in Step 1.
Step 5: Define a Function to Get Weather Data
Next, we will define a function called get_weather_data():
1 2 3 4 5 6 7 8 |
def get_weather_data(city): params = { 'q': city, 'appid': API_KEY, 'units': 'metric' } response = requests.get(API_URL, params=params) return response.json() |
This function takes a city name as a parameter and sends a GET request to the OpenWeatherAPI with the city name, your API key, and the desired unit system (metric, in this case). The response from the server is then returned in the JSON format.
Step 6: Create a Function to Display Weather Data
Now, let’s create a function named display_weather() to display the fetched weather data:
1 2 3 4 5 6 7 8 9 10 |
def display_weather(data): if data["cod"] == 200: main_data = data["main"] print(f"Current Temperature: {main_data['temp']}°C") print(f"Feels Like: {main_data['feels_like']}°C") print(f"Min Temperature: {main_data['temp_min']}°C") print(f"Max Temperature: {main_data['temp_max']}°C") print(f"Humidity: {main_data['humidity']}%") else: print("Error fetching data. Please enter a valid city name.") |
This function takes the JSON data fetched from OpenWeatherAPI and displays the relevant information. If the city name is invalid or an error occurs, an error message will be displayed.
Step 7: Add the Main Function
Finally, add a main function to take user input and call the above functions to display the weather data:
1 2 3 4 5 6 7 |
def main(): city = input("Enter the city name: ") data = get_weather_data(city) display_weather(data) if __name__ == "__main__": main() |
Now your weather bot is ready to function. Save the script and run it.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import requests import json API_KEY = "your_api_key" API_URL = "https://api.openweathermap.org/data/2.5/weather" def get_weather_data(city): params = { 'q': city, 'appid': API_KEY, 'units': 'metric' } response = requests.get(API_URL, params=params) return response.json() def display_weather(data): if data["cod"] == 200: main_data = data["main"] print(f"Current Temperature: {main_data['temp']}°C") print(f"Feels Like: {main_data['feels_like']}°C") print(f"Min Temperature: {main_data['temp_min']}°C") print(f"Max Temperature: {main_data['temp_max']}°C") print(f"Humidity: {main_data['humidity']}%") else: print("Error fetching data. Please enter a valid city name.") def main(): city = input("Enter the city name: ") data = get_weather_data(city) display_weather(data) if __name__ == "__main__": main() |
Output Example
Enter the city name: Warsaw Current Temperature: 23.85°C Feels Like: 23.2°C Min Temperature: 22.68°C Max Temperature: 24.95°C Humidity: 35%
Conclusion
In this tutorial, we created a simple bot that fetches current weather data and displays it to users using Python and OpenWeatherAPI. This concept can be expanded upon by creating bots for various services or applications such as chatbots, social media bots, and more.