-
Notifications
You must be signed in to change notification settings - Fork 1
/
abstract_bot.py
54 lines (44 loc) · 1.73 KB
/
abstract_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""AbstractBot"""
import random
import re
# encoding=utf8
class AbstractBot(object):
"""Generic bot"""
def __init__(self, trigger_word, provider, auto_feed, meme_provider, join_every):
#Setup class props
self.chat_count = {}
self.participation_frequency = 5
self.trigger_word = trigger_word
self.provider = provider
self.auto_feed = auto_feed
self.meme_provider = meme_provider
self.join_every = join_every
def process_incoming_message(self, chat_id, text):
"""Incoming message handlerr"""
regex_trigger = re.compile("(" + self.trigger_word + ")", re.IGNORECASE)
#Reload bot
if text == "bot reload":
self.provider.load()
return "Reloaded", None
#Bot match
elif regex_trigger.findall(text):
self.chat_count[chat_id] = 0
text = regex_trigger.sub('', text)
text = self.provider.get_message(text)
#Meme check
meme = self.meme_provider.get_random_meme(text)
return text, meme
#Save new data
else:
chat_count_dic = self.chat_count
if not chat_id in chat_count_dic:
chat_count_dic[chat_id] = 0
chat_count_dic[chat_id] = chat_count_dic[chat_id] + 1
##He'll join after a random number of messages between 5 and 15
frequency = random.randint(self.join_every, self.join_every * 2)
if chat_count_dic[chat_id] > frequency:
chat_count_dic[chat_id] = 0
text = self.provider.get_message(text)
return text, None
if self.auto_feed:
self.provider.text_provider.add_text(text)