diff --git a/meteobot.py b/meteobot.py new file mode 100644 index 0000000..2daa21b --- /dev/null +++ b/meteobot.py @@ -0,0 +1,59 @@ +## MeteoBot v. 0.1 +## Autore Mario Sabatino +## e-mail: mario@sabatino.pro +## jid xmpp: mario@sabatino.pro +## Questo software viene rilasciato con la licenza GPL3 +## +## Ringraziamenti: +## Questo software è stato sviluppato con il supporto di ChatGPT, un modello di intelligenza artificiale sviluppato da OpenAI. +## +################################################################################################################################# + +import slixmpp +import requests +import logging + +class WeatherBot(slixmpp.ClientXMPP): + def __init__(self, jid, password, room, nick): + super().__init__(jid, password) + self.room = room + self.nick = nick + self.add_event_handler("session_start", self.start) + self.add_event_handler("groupchat_message", self.muc_message) + + def start(self, event): + self.get_roster() + self.send_presence() + self.plugin['xep_0045'].join_muc(self.room, self.nick) + + def muc_message(self, msg): + if msg['mucnick'] != self.nick and msg['body'].startswith('!'): + city_name = msg['body'][1:] + weather = self.get_weather(city_name) + self.send_message(mto=msg['from'].bare, mbody=weather, mtype='groupchat') + + def get_weather(self, city_name): + api_key = "apikey" + url = f"http://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={api_key}&units=metric" + response = requests.get(url).json() + +# Assicurati di gestire errori e dati mancanti qui + weather = response['weather'][0]['description'] + temperature = response['main']['temp'] + return f"Weather in {city_name}: {weather}, {temperature}°C" + +if __name__ == "__main__": + jid = "user@nameserver.tld" + password = "password" + room = "name@conference.nameserver.tld" + nick = "MeteoBot" + +# Creazione dell'istanza del bot e registrazione dei plugin necessari + xmpp = WeatherBot(jid, password, room, nick) + xmpp.register_plugin('xep_0030') # Service Discovery + xmpp.register_plugin('xep_0045') # Multi-User Chat + xmpp.register_plugin('xep_0199') # XMPP Ping + +# Connessione del bot al server XMPP e avvio del processo + xmpp.connect() + xmpp.process(forever=False)