Improved #21:4
This commit is contained in:
parent
7caec5ea2c
commit
b25ebd14e3
|
@ -13,11 +13,14 @@ all=.!?;()\-—"[]{}«»/*&^#$
|
|||
[logging]
|
||||
level=INFO
|
||||
|
||||
[links]
|
||||
lifetime=28800.0
|
||||
stickers=BQADAgADGwEAAjbsGwVVGLVNyOWfuwI
|
||||
|
||||
[redis]
|
||||
host=
|
||||
port=
|
||||
db=
|
||||
links_lifetime=28800.0
|
||||
|
||||
[db]
|
||||
driver=
|
||||
|
|
|
@ -1,4 +1,26 @@
|
|||
import configparser
|
||||
|
||||
sections = {
|
||||
'bot': ['token', 'name', 'anchors', 'messages', 'purge_interval', 'default_chance'],
|
||||
'grammar': ['end_sentence', 'all'],
|
||||
'logging': ['level'],
|
||||
'links': ['lifetime', 'stickers'],
|
||||
'redis': ['host', 'port', 'db'],
|
||||
'db': []
|
||||
}
|
||||
|
||||
|
||||
def getlist(self, section, option, type=str):
|
||||
return list(map(lambda o: type(o), config.get(section, option).split(',')))
|
||||
|
||||
configparser.ConfigParser.getlist = getlist
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
config.read('./main.cfg', encoding='utf-8')
|
||||
|
||||
for section, options in sections.items():
|
||||
if not config.has_section(section):
|
||||
raise ValueError("Config is not valid! Section '{}' is missing!".format(section))
|
||||
for option in options:
|
||||
if not config.has_option(section, option):
|
||||
raise ValueError("Config is not valid! Option '{}' in section '{}' is missing!".format(option, section))
|
|
@ -45,7 +45,7 @@ class Message(AbstractEntity):
|
|||
def has_anchors(self):
|
||||
"""Returns True if the message contains at least one anchor from anchors config.
|
||||
"""
|
||||
anchors = config['bot']['anchors'].split(',')
|
||||
anchors = config.getlist('bot', 'anchors')
|
||||
return self.has_text() and any(a in self.message.text.split(' ') for a in anchors)
|
||||
|
||||
def is_private(self):
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import logging
|
||||
|
||||
from random import choice
|
||||
from src.config import config
|
||||
from telegram.ext import MessageHandler as ParentHandler, Filters
|
||||
from telegram import ChatAction
|
||||
|
||||
|
@ -45,7 +47,7 @@ class MessageHandler(ParentHandler):
|
|||
self.data_learner.learn(message)
|
||||
|
||||
if message.has_links() and self.links_checker.check(message.chat.telegram_id, message.links):
|
||||
self.message_sender.send_sticker(message, "BQADAgAD8gADpOENAAEbAtjuNM5sygI")
|
||||
self.message_sender.send_sticker(message, choice(config.getlist('links', 'stickers')))
|
||||
|
||||
if should_answer:
|
||||
text = self.reply_generator.generate(message)
|
||||
|
|
|
@ -4,7 +4,7 @@ import redis
|
|||
class Redis:
|
||||
def __init__(self, config):
|
||||
self.pool = redis.ConnectionPool(host=config['redis']['host'],
|
||||
port=config['redis']['port'],
|
||||
port=config.getint('redis', 'port'),
|
||||
db=config['redis']['db'])
|
||||
|
||||
def instance(self):
|
||||
|
|
|
@ -11,7 +11,7 @@ from src.config import config
|
|||
class ChatPurgeQueue:
|
||||
queue = None
|
||||
jobs = {}
|
||||
default_interval = float(config['bot']['purge_interval'])
|
||||
default_interval = config.getfloat('bot', 'purge_interval')
|
||||
job_type = 'purge'
|
||||
|
||||
# TODO. Должно взять все задачи из таблицы и проинициализировать их
|
||||
|
|
|
@ -13,7 +13,7 @@ class LinksChecker:
|
|||
redis = self.redis.instance()
|
||||
key = "links:{}".format(chat_id)
|
||||
now = datetime.now()
|
||||
delete_at = (now + timedelta(seconds=float(config['redis']['links_lifetime']))).timestamp()
|
||||
delete_at = (now + timedelta(seconds=config.getfloat('links', 'lifetime'))).timestamp()
|
||||
|
||||
# Delete stale links
|
||||
redis.zremrangebyscore(key, 0, now.timestamp())
|
||||
|
|
Loading…
Reference in New Issue