generated from Nemesis/Exemple
Compare commits
10 commits
d1d1c4ba60
...
7262d190ba
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7262d190ba | ||
![]() |
45f4e994cf | ||
2910f0afdc | |||
77920a94f2 | |||
![]() |
e665b335d8 | ||
c228b3feff | |||
9a81959f5a | |||
da8b02b027 | |||
ad0087e8cb | |||
917d4d9d88 |
2 changed files with 143 additions and 18 deletions
|
@ -5,8 +5,8 @@ server = https://instance.tld
|
|||
admin = @mxid:intance.tld
|
||||
|
||||
[MOD]
|
||||
# liste = roll,quote,biere,stats,liste,horloge,carte,card,cristal,welcome,salut,jdr,gens,va,salon,kick,mails,titre,invidious
|
||||
liste = roll,quote,biere,stats,liste,horloge,carte,card,cristal,welcome,salut,jdr,gens,va,salon,kick,mails,titre,invidious
|
||||
# liste = roll,quote,biere,stats,liste,horloge,carte,card,cristal,welcome,salut,jdr,gens,va,salon,kick,mails,titre,invidious,ecowatt,commune,eau
|
||||
liste = roll,quote,biere,stats,liste,horloge,carte,card,cristal,welcome,salut,jdr,gens,va,salon,kick,mails,titre,invidious,ecowatt,commune,eau
|
||||
|
||||
[MAIL]
|
||||
malt = 0
|
||||
|
@ -18,4 +18,3 @@ mport = port
|
|||
[RTE]
|
||||
client_id = id
|
||||
client_secret = secret
|
||||
call_url = https://digital.iservices.rte-france.com/open_api/ecowatt/v4/signals
|
||||
|
|
156
asmodee.py
156
asmodee.py
|
@ -25,9 +25,14 @@ import json
|
|||
bot = None
|
||||
PREFIX = '!'
|
||||
USERNAME = ""
|
||||
oauth = None #Appels RTE
|
||||
RTE_ID = None # Bot's RTE ID
|
||||
RTE_SECRET = None # Bot's RTE SECRET
|
||||
|
||||
#ecowatt_url = 'https://digital.iservices.rte-france.com/open_api/ecowatt/v4/sandbox/signals'
|
||||
ecowatt_url = 'https://digital.iservices.rte-france.com/open_api/ecowatt/v4/signals'
|
||||
commune_url = 'https://geo.api.gouv.fr/communes?<TYPE>=<NOM>&fields=nom,code,codesPostaux,population&format=json'
|
||||
eau_url = 'https://hubeau.eaufrance.fr/api/v1/qualite_eau_potable/resultats_dis?<TYPE>=<NOM>&size=20&code_parametre=1301,1302,1312,1313,1305,1335,1339,1340,1350,1433,1377,1338,6276,1295,1382,1383,1388,1387,1392'
|
||||
|
||||
wattobj = None
|
||||
|
||||
# Listes et dictionnaires
|
||||
|
@ -222,7 +227,7 @@ class Userstats: #Pour garder des stats sur les users
|
|||
def __init__(self,room,user,nick):
|
||||
self.room = room
|
||||
self.user = user
|
||||
self.nick = nick.strip()
|
||||
self.nick = nick.strip() if nick and nick != None else ""
|
||||
self.date = 0
|
||||
self.mess = 0
|
||||
self.char = 0
|
||||
|
@ -692,7 +697,10 @@ def getMessage(message):
|
|||
|
||||
def getUser(message):
|
||||
match = re.search(r"^(@\w+:[^ :]+)", str(message))
|
||||
return str(match.group())
|
||||
if match:
|
||||
return str(match.group())
|
||||
else:
|
||||
return "personne"
|
||||
|
||||
def getNick(room, message): # Obtenir le DisplayName à partir du mxid.
|
||||
sender = getUser(message)
|
||||
|
@ -1254,7 +1262,10 @@ async def bienvenue(room,message): # Affichage d'un message d'accueil aux nouvea
|
|||
elif (membership == "leave" and prev_member != "leave"):
|
||||
await msg(room,"Au revoir " + nick + "!",sender)
|
||||
else:
|
||||
print("current: " + membership + " prev: " + prev_member)
|
||||
try:
|
||||
print("current: " + membership + " prev: " + prev_member)
|
||||
except:
|
||||
print(message)
|
||||
|
||||
async def titre(room, message): # cherche le titre d'une page
|
||||
global modules
|
||||
|
@ -1627,8 +1638,14 @@ async def ecowatt(room, message):
|
|||
signals = None
|
||||
global wattobj
|
||||
if (wattobj.jour < jn or (wattobj.jour == jn and wattobj.heure + 1 < hn)):
|
||||
result = oauth.get(ecowatt_url)
|
||||
watt = json.loads(result.text)
|
||||
try:
|
||||
oauth = rte_token()
|
||||
result = oauth.get(ecowatt_url)
|
||||
watt = json.loads(result.text)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
await msg(room, "Veuillez attendre 15 minutes", sender)
|
||||
return
|
||||
sign = watt["signals"]
|
||||
signals = sorted(sign, key=lambda signal: signal["jour"][0:10])
|
||||
wattobj.jour = jn
|
||||
|
@ -1664,7 +1681,101 @@ async def ecowatt(room, message):
|
|||
alerte = getNiveau(jour["dvalue"])
|
||||
mess = mess + "La météo RTE du " + jour["jour"][0:10] + " est de niveau " + alerte + " (" + jour["message"] + ")\n"
|
||||
await msg(room, mess, sender)
|
||||
|
||||
async def commune(room, message):
|
||||
global modules
|
||||
try:
|
||||
rmod = modules[room.room_id, 'commune']
|
||||
except:
|
||||
modules[room.room_id, 'commune'] = False
|
||||
if (modules[room.room_id, 'commune']):
|
||||
args = getMessage(message).split()
|
||||
sender = getUser(message)
|
||||
args.pop(0)
|
||||
mess = ""
|
||||
if (len(args) > 0):
|
||||
ville = " ".join(args)
|
||||
cette_commune = None
|
||||
if (ville.isnumeric()):
|
||||
cette_commune = commune_url.replace("<NOM>", ville).replace("<TYPE>", "codePostal")
|
||||
else:
|
||||
cette_commune = commune_url.replace("<NOM>", ville).replace("<TYPE>", "nom")
|
||||
oauth = rte_token()
|
||||
result = oauth.get(cette_commune)
|
||||
villes = json.loads(result.text)
|
||||
if (len(villes) > 0):
|
||||
try:
|
||||
cites = sorted(villes, key=lambda v: str(v["population"]).zfill(10), reverse=True)
|
||||
cite = cites[0]
|
||||
codePoste = cite["codesPostaux"][0]
|
||||
pop = cite["population"]
|
||||
insee = cite["code"]
|
||||
nom = cite["nom"]
|
||||
await msg(room, nom + ". CP : " + codePoste + ". Code INSEE : " + insee + ". Population : " + str(pop), sender)
|
||||
except:
|
||||
await msg(room, str(villes), sender)
|
||||
else:
|
||||
await msg(room, "Commune non trouvée", sender)
|
||||
else:
|
||||
await msg(room, "!commune <nom de la commune OU code postal de la commune>", sender)
|
||||
|
||||
async def eau(room, message):
|
||||
global modules
|
||||
try:
|
||||
rmod = modules[room.room_id, 'eau']
|
||||
except:
|
||||
modules[room.room_id, 'eau'] = False
|
||||
if (modules[room.room_id, 'eau']):
|
||||
args = getMessage(message).split()
|
||||
sender = getUser(message)
|
||||
args.pop(0)
|
||||
mess = ""
|
||||
if (len(args) > 0):
|
||||
ville = " ".join(args)
|
||||
cette_commune = None
|
||||
if (ville.isnumeric()):
|
||||
cette_commune = eau_url.replace("<NOM>", ville).replace("<TYPE>", "code_commune")
|
||||
else:
|
||||
cette_commune = eau_url.replace("<NOM>", ville).replace("<TYPE>", "nom_commune")
|
||||
oauth = rte_token()
|
||||
result = oauth.get(cette_commune)
|
||||
eaux = None
|
||||
try:
|
||||
eaux = json.loads(result.text)
|
||||
except:
|
||||
eaux = None
|
||||
if (eaux is not None):
|
||||
if (len(eaux["data"]) > 0):
|
||||
try:
|
||||
f = True
|
||||
comp_msg=""
|
||||
for eau in eaux["data"]:
|
||||
if (f):
|
||||
nom = eau["nom_commune"]
|
||||
distrib = eau["nom_distributeur"]
|
||||
concl = eau["conclusion_conformite_prelevement"]
|
||||
await msg(room, "Prélèvement à " + nom + " réseau " + distrib + ". " + concl, sender)
|
||||
f = False
|
||||
date = eau["date_prelevement"][0:10]
|
||||
res = eau["resultat_alphanumerique"]
|
||||
unite = str(eau["libelle_unite"])
|
||||
if (unite == "SANS OBJET"):
|
||||
unite = ""
|
||||
limite = "(" + str(eau["limite_qualite_parametre"]) + ")"
|
||||
if (limite == "(None)"):
|
||||
limite = ""
|
||||
param = eau["libelle_parametre"] + " " + limite
|
||||
comp_msg = comp_msg + "Résultat du " + date + " : " + res + unite + " / " + param + ".\n"
|
||||
await msg(room, comp_msg, sender)
|
||||
except:
|
||||
await msg(room, str(eaux), sender)
|
||||
else:
|
||||
await msg(room, "Commune non trouvée", sender)
|
||||
else:
|
||||
await msg(room, "Rapport d'eaux non trouvé", sender)
|
||||
else:
|
||||
await msg(room, "!eau <nom de la commune OU code INSEE de la commune>", sender)
|
||||
|
||||
async def help(room, message): # Aide en message privé
|
||||
await help_center(room,message,"help",True)
|
||||
|
||||
|
@ -1763,6 +1874,18 @@ async def help_center(room,message,name,private): # Aide sur les commandes dispo
|
|||
message += "\n- !modo : Gère les modérateurs du salon\n- !module : Gère les modules actifs sur le salon\n\n- Pour plus de details, tapez !" + name + " <commande>\n\nRetrouvez Asmodee sur https://git.ombreport.info/nemesis/asmodee_matrix"
|
||||
await msg(salon,message,sender, False)
|
||||
|
||||
@bot.listener.on_message_event
|
||||
async def callCommune(room, message):
|
||||
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
||||
if match.is_not_from_this_bot() and match.prefix() and match.command("commune"):
|
||||
await commune(room, message)
|
||||
|
||||
@bot.listener.on_message_event
|
||||
async def callEau(room, message):
|
||||
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
||||
if match.is_not_from_this_bot() and match.prefix() and match.command("eau"):
|
||||
await eau(room, message)
|
||||
|
||||
@bot.listener.on_message_event
|
||||
async def callEcowatt(room, message):
|
||||
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
||||
|
@ -1903,13 +2026,20 @@ async def callStats(room, message):
|
|||
if match.is_not_from_this_bot():
|
||||
statistiques(room, message)
|
||||
|
||||
def rte_token():
|
||||
auth = HTTPBasicAuth(RTE_ID, RTE_SECRET)
|
||||
client = BackendApplicationClient(client_id=RTE_ID)
|
||||
oauth = OAuth2Session(client=client)
|
||||
token = oauth.fetch_token(token_url='https://digital.iservices.rte-france.com/token/oauth/', auth=auth)
|
||||
return(oauth)
|
||||
|
||||
def main():
|
||||
config = configparser.ConfigParser()
|
||||
config.read('asmodee.ini')
|
||||
USERNAME = "" # Bot's username
|
||||
PASSWORD = "" # Bot's password
|
||||
SERVER = "" # Matrix server URL
|
||||
mods = ["roll", "quote", "biere", "stats", "liste", "horloge", "carte", "card", "cristal", "welcome", "salut", "jdr", "gens", "va", "salon", "kick", "mails", "titre", "invididious", "ecowatt"]
|
||||
mods = ["roll", "quote", "biere", "stats", "liste", "horloge", "carte", "card", "cristal", "welcome", "salut", "jdr", "gens", "va", "salon", "kick", "mails", "titre", "invididious", "ecowatt", "commune"]
|
||||
global admins
|
||||
if ('AUTH' in config):
|
||||
USERNAME = config['AUTH']['username'] # Bot's username
|
||||
|
@ -1919,20 +2049,16 @@ def main():
|
|||
else:
|
||||
print("Probleme de lecture de configuration asmodee.ini (AUTH)")
|
||||
|
||||
RTE_ID = None # Bot's RTE ID
|
||||
RTE_SECRET = None # Bot's RTE SECRET
|
||||
if ('RTE' in config):
|
||||
global oauth
|
||||
global RTE_ID
|
||||
global RTE_SECRET
|
||||
global wattobj
|
||||
RTE_ID = config['RTE']['client_id'] # Bot's username
|
||||
RTE_SECRET = config['RTE']['client_secret'] # Bot's password
|
||||
auth = HTTPBasicAuth(RTE_ID, RTE_SECRET)
|
||||
client = BackendApplicationClient(client_id=RTE_ID)
|
||||
oauth = OAuth2Session(client=client)
|
||||
token = oauth.fetch_token(token_url='https://digital.iservices.rte-france.com/token/oauth/', auth=auth)
|
||||
wattobj = Wattobject()
|
||||
|
||||
else:
|
||||
RTE_ID = None
|
||||
RTE_SECRET = None
|
||||
print("Probleme de lecture de configuration asmodee.ini (RTE)")
|
||||
|
||||
global liste_mod
|
||||
|
|
Loading…
Reference in a new issue