|
|
|
@ -28,6 +28,7 @@ class Bot: |
|
|
|
for k in self.config["match"].keys(): |
|
|
|
self.add_reaction(k, self.config["match"][k]) |
|
|
|
|
|
|
|
|
|
|
|
self.pings = [] |
|
|
|
for quote in self.config["ping"]["quotes"]: |
|
|
|
self.add_ping(quote) |
|
|
|
@ -40,6 +41,7 @@ class Bot: |
|
|
|
self.dispatcher = self.updater.dispatcher |
|
|
|
|
|
|
|
self.quote_handler = CommandHandler('ping', self.on_ping) |
|
|
|
self.match_handler = MessageHandler(Filters.text, self.on_message) |
|
|
|
self.dispatcher.add_handler(self.quote_handler) |
|
|
|
|
|
|
|
def start(self): |
|
|
|
@ -52,8 +54,26 @@ class Bot: |
|
|
|
pass |
|
|
|
|
|
|
|
def on_ping(self, update, context): |
|
|
|
quote = random.choice(self.config["ping"]["quotes"]) |
|
|
|
context.bot.send_message(chat_id=update.effective_chat.id, text=quote) |
|
|
|
c = { |
|
|
|
"channel": update.effective_chat.title, |
|
|
|
"name": self.name, |
|
|
|
"user": update.effective_user.name, |
|
|
|
"message": update.effective_message.text, |
|
|
|
} |
|
|
|
quote = random.choice(self.pings) |
|
|
|
if callable(quote): |
|
|
|
quote = quote(self, username, channel, message) |
|
|
|
else: |
|
|
|
quote = quote.format(**c) |
|
|
|
context.bot.send_message(chat_id=update.effective_chat.id, text=quote, reply_to_message_id=update.effective_message.message_id) |
|
|
|
|
|
|
|
def on_message(self, update, context): |
|
|
|
user = update.effective_user |
|
|
|
channel = update.effective_chat |
|
|
|
message = update.effective_message |
|
|
|
answer = self.get_reaction(user, channel, message) |
|
|
|
for ans in answer: |
|
|
|
context.bot.send_message(chat_id=update.effective_chat.id, text=ans) |
|
|
|
|
|
|
|
def add_reaction(self, match, reaction): |
|
|
|
"""Add a reaction to the bot. |
|
|
|
@ -88,7 +108,7 @@ class Bot: |
|
|
|
module = importlib.import_module(module) |
|
|
|
return getattr(module, callback) |
|
|
|
|
|
|
|
def get_reaction(self, username, channel, message): |
|
|
|
def get_reaction(self, user, channel, message): |
|
|
|
"""Get a reaction to a message. |
|
|
|
|
|
|
|
Args: |
|
|
|
@ -99,18 +119,19 @@ class Bot: |
|
|
|
Returns: |
|
|
|
Every matched reactions. |
|
|
|
""" |
|
|
|
if (datetime.datetime.now() - self.last_time).total_seconds() < self.min_time: |
|
|
|
if (datetime.datetime.now() - self.channels[channel.id]).total_seconds() < self.min_time: |
|
|
|
return [] |
|
|
|
|
|
|
|
context = { |
|
|
|
"channel": channel, |
|
|
|
"channel": channel.title, |
|
|
|
"name": self.name, |
|
|
|
"user": username, |
|
|
|
"message": message, |
|
|
|
"user": user.name, |
|
|
|
"message": message.text, |
|
|
|
} |
|
|
|
result = [] |
|
|
|
logging.debug("Looking for reactions.") |
|
|
|
for m in self.reactions.keys(): |
|
|
|
search = m.search(message) |
|
|
|
search = m.search(message.text) |
|
|
|
if search: |
|
|
|
r = self.reactions[m] |
|
|
|
if callable(r): |
|
|
|
@ -119,15 +140,7 @@ class Bot: |
|
|
|
r = r.format(**context) |
|
|
|
result.append(r) |
|
|
|
|
|
|
|
if not result and self.ping_match.search(message): |
|
|
|
r = random.choice(self.pings) |
|
|
|
if callable(r): |
|
|
|
r = r(self, username, channel, message) |
|
|
|
else: |
|
|
|
r = r.format(**context) |
|
|
|
result.append(" : ".join([username, r])) |
|
|
|
|
|
|
|
if len(result) > 0: |
|
|
|
self.last_time = datetime.datetime.now() |
|
|
|
self.channels[channel.id] = datetime.datetime.now() |
|
|
|
|
|
|
|
return result |
|
|
|
|