How To Create A Bot In Python

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.


Before we get started, make sure that you have the following installed on your system:

  1. Python 3.x: You can download the latest version of Python from the official website.
  2. 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:

  1. Visit the OpenWeatherAPI website.
  2. Sign up for a free account.
  3. After signing up, go to the API section or visit the API keys page.
  4. 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

Step 3: Import the Required Libraries

At the beginning of the script, import the required libraries:

Step 4: Add the API Key and URL

Add a variable containing your API key and the API URL to your script:

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():

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:

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:

Now your weather bot is ready to function. Save the script and run it.

Full Code

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%


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.