Raspberry Pi project: Weather forecast bot
Get Telegram Bot and API Key
a. Create a Telegram bot by contacting the BotFather in Telegram.
b. Note down the bot token you get from the BotFather.
c. Register on the OpenWeatherMap website and get a free API key

Installing Python Libraries
Before you can run the code, you'll need to make sure you have the necessary Python libraries installed on your Raspberry Pi 5. You can install these libraries using pip, the Python package manager. Open a terminal on your Raspberry Pi 5 and run the following commands to install python and pip:
sudo apt-get update
sudo apt-get upgrade
sudo apt install python3
sudo apt install python3-pipNow install the python libraries
pip install requests
pip install python-telegram-botFinally, reboot your Raspberry Pi to complete the installation:
sudo rebootCode
Copy and paste the code into Thonny. Replace the placeholders in the code (such as 'JE_BOT_TOKEN' and 'JE_OPENWEATHERMAP_API_KEY') with the actual bot token and API key you obtained.
import requests
import datetime
from telegram import Bot
import time
import asyncio
# Vervang ‘JE_BOT_TOKEN’ door je eigen Telegram bot token
bot_token = ‘JE_BOT_TOKEN_HIER’
# Vervang ‘STAD’ door de naam van de stad waarvoor je het weer wilt ophalen
stad = ‘Amersfoort’
# Vervang ‘JE_OPENWEATHERMAP_API_KEY’ door je eigen gratis OpenWeatherMap API-sleutel
api_key = ‘JE_OPENWEATHERMAP_API_KEY_HIER’
# Functie om het weerbericht op te halen
async def get_weer():
base_url = ‘http://api.openweathermap.org/data/2.5/weather’
params = {
‘q’: stad,
‘appid’: api_key,
‘units’: ‘metric’ # Gebruik ‘imperial’ voor Fahrenheit in plaats van Celsius
}
response = requests.get(base_url, params=params)
weer_data = response.json()
if response.status_code == 200:
weer_beschrijving = weer_data[‘weather’][0][‘description’]
temperatuur = weer_data[‘main’][‘temp’]
return f’Het weer in {stad} vandaag: {weer_beschrijving}, Temperatuur: {temperatuur}°C’
else:
return ‘Het weerbericht kon niet worden opgehaald.’
# Functie om een Telegram-bericht te versturen
async def send_telegram_bericht(bericht):
bot = Bot(token=bot_token)
chat_id = ‘JE_CHAT_ID_HIER’ # Vervang dit door je eigen chat-ID
await bot.send_message(chat_id=chat_id, text=bericht)
# Code continu uitvoeren
async def main():
while True:
now = datetime.datetime.now()
if now.hour == 14 and now.minute == 5: # Vervang dit door je gewenste tijd
weer_rapport = await get_weer()
await send_telegram_bericht(weer_rapport)
# Wacht even om te voorkomen dat er snel achter elkaar meerdere berichten worden verstuurd
await asyncio.sleep(60) # Wacht 60 seconden (pas aan indien nodig)
else:
# Wacht even voordat de tijd opnieuw wordt gecontroleerd
await asyncio.sleep(60) # Wacht 60 seconden (pas aan indien nodig)
if __name__ == “__main__”:
asyncio.run(main())Execute Code
a. Save the Python script with a suitable name, for example weer_project.py .
b. Navigate to the folder where you saved the Python script.
c. Run the script with the following command:
python weer_project.pyThe script will start running and check every minute if it is time to get the weather forecast and send it to your Telegram chat. To test if it works fine you can change the time to a time that is 2 minutes from now and then run the code.
And that's it! Didn't work? Look back at the previous steps to see what went wrong. Did it work? Then take a look at our projects!
