Commit bd111eb1 authored by Otthorn's avatar Otthorn 🤔

added new stuff

parent dd9da7d9
......@@ -76,3 +76,4 @@ envs3
# Senible data storage
*.env
citations.txt
*.ini
......@@ -10,3 +10,6 @@
- [ ] fonction !music pour changer la musique à la med
- [ ] fonction qui renvoie les menus pour les commandes de nourritures à la med
- [ ] module qui intégarit avec la BDD de la Med
- [ ] ajouter une option modulaire : avoir accès seuelement certains modules par salle (peut se gérer avec un fichier de conf, qui pourrait être modifiable par une commande, qui metterais à jouer l'help, qui serait accessible uniquement aux moderateur)
- [ ] module qui prévient les gens pour la politesse, les capslock et le spam et les kicks temporairement en cas non respect répétter sur un temps trop court
api_key = api_key_goes_here
USERNAME = username_goes_here
PASSWORD = password_goes_here
SERVER = https://matrix.org
import string
from secrets import choice
from sys import argv
PL = int(argv[1])
alphabet = string.ascii_letters + string.digits
password = ''.join(choice(alphabet) for i in range(PL))
print(password)
......@@ -112,15 +112,6 @@ def gif_callback(room, event):
room.send_image(mxc_url, query, mimetype=mime)
def weather_callback(room, event):
args = event['content']['body'].split()
query = " ".join(args[1:])
print("WEATHER called with query={} in room={}".format(query, room.display_name))
summary = query_weather(query)
room.send_text(summary)
def quote_callback(room, event):
args = event['content']['body'].split()
......@@ -189,9 +180,6 @@ def main():
gif_handler = MCommandHandler("gif", gif_callback)
bot.add_handler(gif_handler)
weather_handler = MCommandHandler("weather", weather_callback)
bot.add_handler(weather_handler)
quote_handler = MCommandHandler("citation", quote_callback)
bot.add_handler(quote_handler)
......
import requests
import configparser
from time import strftime
from parse_env import parse_env
from matrix_bot_api.matrix_bot_api import MatrixBotAPI
from matrix_bot_api.mregex_handler import MRegexHandler
from matrix_bot_api.mcommand_handler import MCommandHandler
appid = parse_env("weather.env")['appid']
def query_weather(query, api_key=appid):
def query_weather(query):
url = "http://api.openweathermap.org/data/2.5/weather?appid={}&q={}".format(appid, query)
json_data = requests.get(url).json()
# check if we joined the api correctly
# check for potential errors in contacting API
code = json_data['cod']
if str(code) != "200": # str(code) par l'api renvoie parfois des int et parfois des str
return "Internal error " + code
if str(code) != "200": # fix strange API behaviour
return "API error " + str(code)
# import all of the variables
weather_main = json_data['weather'][0]['main']
......@@ -31,12 +34,56 @@ def query_weather(query, api_key=appid):
country = json_data['sys']['country']
name = json_data['name']
# create a summary for the weather
summary = """ {}, {} : {}
{:.1f}°C ({:.1f}°C - {:.1f}°C), wind {} m/s, clouds {}%, {} hPa""".format(name, country,
weather_description, temp, temp_min, temp_max, wind_speed, clouds, pressure)
{:.1f}°C ({:.1f}°C - {:.1f}°C), wind {} m/s, clouds {}%, {} hPa""".format(name,
country, weather_description, temp, temp_min, temp_max, wind_speed,
clouds, pressure)
return summary
def kelvin_to_celsius(T):
"""Convert Kelvin (K) to degree Celsius (°C)"""
return float(T) - 273
def weather_callback(room, event):
args = event['content']['body'].split()
query = " ".join(args[1:])
# Print monitoring information
print("[{}] WEATHER called with query={} in room={}".format(strftime("%d/%m/%Y %H:%M:%S"), query, room.display_name))
summary = query_weather(query)
room.send_text(summary)
def main():
# Load configuration
config = configparser.ConfigParser()
config.read("config.ini")
username = config.get("Weather", "username")
password = config.get("Weather", "password")
server = config.get("Weather", "server")
global appid
appid = config.get("Weather", "appid")
# Start bot
bot = MatrixBotAPI(username, password, server)
# Create the handlers
weather_handler = MCommandHandler("weather", weather_callback)
bot.add_handler(weather_handler)
# Start polling
bot.start_polling()
print("weather_bot started")
while True:
input()
if __name__ == "__main__":
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment