Parce que Python c'est bien, en faire exécuter par les bots c'est bon esprit.

This commit is contained in:
Klafyvel 2018-08-04 15:17:40 +02:00
parent 53310ede4e
commit 6cc3993f3c
18 changed files with 72 additions and 8 deletions

View file

@ -45,8 +45,15 @@ bots:
- "Tu sais {user}, si j'suis toujours si bien accompagné, c'est pour mon argent hein, c'est pas pour mon odeur... Et ça ça fait mal"
- "Ou tu sors, ou j'te sors, hein, mais faudra prendre une décision."
- "Et à propos de vieille truie toi comment ça va ?"
Chuck:
on_ping:
- "No, thanks."
on_ping_python:
- 'klafirc.bots.chuck_norris.on_ping'
channels:
- server: irc.rezometz.org
port: 6667
channel: "#campus"
bots: [sel, Macron, Patou, Claudy]
bots: [sel, Macron, Patou, Claudy, Chuck]

View file

@ -1,5 +1,6 @@
import re
import random
import importlib
class Bot:
def __init__(self, nickname):
@ -31,11 +32,30 @@ class Bot:
}
self.reactions[re.compile(match.format(**context))] = reaction
def add_python_reaction(self, match, reaction):
""" Add a Python callback to the reactions.
Args:
match: The string which, if matched will trigger the answer.
reaction: The path to the callback
"""
self.add_reaction(match, self.fetch_callback(reaction))
def add_ping(self, reaction):
"""Add a reaction to a ping"""
self.pings.append(reaction)
def add_python_ping(self, reaction):
"""Fetch a Python callable and add it to the pings"""
self.add_ping(self.fetch_callback(reaction))
def fetch_callback(self, path):
"""Fetch a Python callable"""
s = path.split('.')
module, callback = '.'.join(s[:-1]), s[-1]
module = importlib.import_module(module)
return getattr(module, callback)
def get_reaction(self, user, channel, message):
"""Get a reaction to a message.
@ -62,11 +82,20 @@ class Bot:
result = []
for m in self.reactions.keys():
if m.search(message):
result.append(self.reactions[m].format(**context))
r = self.reactions[m]
if callable(r):
r = r(self, username, channel, message)
else:
r = r.format(**context)
result.append(r)
if not result and self.ping_match.search(message):
sentence = random.choice(self.pings).format(**context)
result.append(' : '.join([username, sentence]))
r = random.choice(self.pings)
if callable(r):
r = r(self, username, channel, message)
else:
r = r.format(**context)
result.append(' : '.join([username, r]))
return result

Binary file not shown.

Binary file not shown.

1
klafirc/bots/__init__.py Normal file
View file

@ -0,0 +1 @@
from . import chuck_norris

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,13 @@
"""
Fetch a random chuck norris fact and dislays it.
"""
import requests
import json
def get_content():
r = requests.get('https://api.chucknorris.io/jokes/random')
return json.loads(r.content)['value']
def on_ping(bot, user, channel, message):
return get_content()

View file

@ -19,10 +19,14 @@ class Loader:
b.port = port
for ping in template.get('on_ping', []):
b.add_ping(ping)
for ping in template.get('on_ping_python', []):
b.add_python_ping(ping)
matches = template.get('on_match', [])
for match in matches:
b.add_reaction(match, matches[match])
for match in template.get('on_match_python', []):
b.add_python_reaction(match, matches[match])
return b

View file

@ -1,9 +1,14 @@
import logging
from logging.handlers import RotatingFileHandler
DEBUG = True
BOT_FILE = '/etc/klafirc/bots.yaml'
LOG_FILE = '/var/log/klafirc/klafirc.log'
DEBUG = False
if not DEBUG:
BOT_FILE = '/etc/klafirc/bots.yaml'
LOG_FILE = '/var/log/klafirc/klafirc.log'
else:
BOT_FILE = './bots.yaml'
LOG_FILE = './klafirc.log'
logger = logging.getLogger()

View file

@ -1,11 +1,16 @@
attrs==18.1.0
Automat==0.7.0
certifi==2018.4.16
chardet==3.0.4
constantly==15.1.0
hyperlink==18.0.0
idna==2.7
incremental==17.5.0
Klafirc==0.1
PyHamcrest==1.9.0
PyYAML==3.13
requests==2.19.1
six==1.11.0
Twisted==18.7.0
urllib3==1.23
zope.interface==4.5.0