Skip to content

Commit

Permalink
Add a message when running the bot (Finally!)
Browse files Browse the repository at this point in the history
  • Loading branch information
rougeth committed Oct 31, 2017
1 parent 1fa3644 commit 131740a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
16 changes: 13 additions & 3 deletions bottery/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import importlib
import logging
import logging.config
from datetime import datetime

import aiohttp
import click

import bottery
from bottery.conf import settings

logger = logging.getLogger('bottery')
Expand All @@ -30,7 +33,7 @@ def loop(self):
return self._loop

def configure_platforms(self):
platforms = settings.PLATFORMS.values()
platforms = settings.PLATFORMS.items()

# Raise Exception if no platform was configured.
if not platforms:
Expand All @@ -40,10 +43,11 @@ def configure_platforms(self):
# of its engine, run its `configure` method and add its tasks
# to the App's tasks list. Once it's configured, create
# a route for its handler.
for platform in platforms:
for platform_name, platform in platforms:
logger.debug('Configuring engine %s', platform['ENGINE'])

mod = importlib.import_module(platform['ENGINE'])
platform['OPTIONS']['platform'] = platform_name
platform['OPTIONS']['session'] = self.session
engine = mod.engine(**platform['OPTIONS'])
engine.configure()
Expand All @@ -55,13 +59,19 @@ def configure_platforms(self):
logger.debug('[%s] Ready', engine.platform)

def run(self):
click.echo('Configuring platforms...\n')
self.configure_platforms()

# Add Platforms tasks to the App loop.
for task in self.tasks:
self.loop.create_task(task(session=self.session))
logger.debug('Tasks created')

now = datetime.now().strftime('%B %d, %Y - %X')
msg = ('{now}\n'
'Bottery version {version}\n'
'Starting development server\n'
'Quit the server with CONTROL-C\n')
click.echo(msg.format(now=now, version=bottery.__version__))
self.loop.run_forever()

def stop(self):
Expand Down
7 changes: 3 additions & 4 deletions bottery/platform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def discover_view(message):
patterns = importlib.import_module('patterns').patterns
for pattern in patterns:
if pattern.check(message):
logger.debug('[%s] Pattern found', message.platform)
if isinstance(pattern.view, str):
return importlib.import_module(pattern.view)
return pattern.view
Expand All @@ -37,9 +36,9 @@ def __init__(self, **kwargs):
setattr(self, item, value)

@property
def platform(self):
"""Platform name"""
raise NotImplementedError('platform attribute not implemented')
def engine(self):
"""Engine name"""
raise NotImplementedError('engine attribute not implemented')

@property
def tasks(self):
Expand Down
3 changes: 2 additions & 1 deletion bottery/platform/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __str__(self):


class TelegramEngine(platform.BaseEngine):
platform = 'telegram'
engine = 'telegram'

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -135,6 +135,7 @@ def build_message(self, data):

async def message_handler(self, data):
message = self.build_message(data)
logger.info('[%s] Message from %s' % (self.platform, message.user))

# Try to find a view (best name?) to response the message
view = discover_view(message)
Expand Down

0 comments on commit 131740a

Please sign in to comment.