29 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			29 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| from maubot import Plugin, MessageEvent
 | |
| from maubot.handlers import command
 | |
| import aiohttp
 | |
| 
 | |
| class WeatherBot(Plugin):
 | |
|     @command.new("pocasi", help="Zobrazí aktuální počasí pro dané město")
 | |
|     async def pocasi(self, evt: MessageEvent, args: str = None) -> None:
 | |
|         city = args if args else self.config.get("default_location", "Sokolov")
 | |
|         api_key = self.config.get("api_key")
 | |
| 
 | |
|         if not api_key:
 | |
|             await evt.reply("API klíč pro OpenWeatherMap není nastaven.")
 | |
|             return
 | |
| 
 | |
|         url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric&lang=cz"
 | |
|         async with aiohttp.ClientSession() as session:
 | |
|             async with session.get(url) as resp:
 | |
|                 if resp.status != 200:
 | |
|                     await evt.reply(f"Nepodařilo se načíst počasí pro {city}.")
 | |
|                     return
 | |
|                 data = await resp.json()
 | |
| 
 | |
|         try:
 | |
|             weather = data["weather"][0]["description"]
 | |
|             temp = data["main"]["temp"]
 | |
|             wind = data["wind"]["speed"]
 | |
|             await evt.reply(f"Počasí v {city}: {temp}°C, {weather}, vítr {wind} m/s")
 | |
|         except KeyError:
 | |
|             await evt.reply("Chyba při čtení dat z API.") |