Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
248 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ Development Lead | |
Contributors | ||
------------ | ||
|
||
None yet. Why not be the first? | ||
* Frédéric Massart (FMCorz) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,59 @@ | ||
from telegrambot.bot_views.generic.responses import TextResponse, KeyboardResponse | ||
from telegram import ParseMode | ||
import sys | ||
import traceback | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
PY3 = sys.version_info > (3,) | ||
|
||
class TemplateCommandView(object): | ||
template_text = None | ||
template_keyboard = None | ||
|
||
def get_context(self, bot, update, **kwargs): | ||
return None | ||
|
||
def handle(self, bot, update, **kwargs): | ||
try: | ||
ctx = self.get_context(bot, update, **kwargs) | ||
text = TextResponse(self.template_text, ctx).render() | ||
keyboard = KeyboardResponse(self.template_keyboard, ctx).render() | ||
# logger.debug("Text:" + str(text.encode('utf-8'))) | ||
# logger.debug("Keyboard:" + str(keyboard)) | ||
if text: | ||
if not PY3: | ||
text = text.encode('utf-8') | ||
bot.send_message(chat_id=update.message.chat_id, text=text, reply_markup=keyboard, parse_mode=ParseMode.MARKDOWN) | ||
else: | ||
logger.info("No text response for update %s" % str(update)) | ||
except: | ||
exc_info = sys.exc_info() | ||
traceback.print_exception(*exc_info) | ||
raise | ||
|
||
|
||
class BaseCommandView(object): | ||
"""Base Command View. | ||
This base class defines the handle method which you should implement | ||
in sub classes. The parameters you care about are accessible through | ||
the properties bot, update and kwargs. Note that the latter are not | ||
available in the constructor. | ||
""" | ||
|
||
_bot = None | ||
_update = None | ||
_kwargs = None | ||
|
||
def handle(self, *args, **kwargs): | ||
pass | ||
|
||
def init(self, bot, update, **kwargs): | ||
"""Init the view with the handling arguments. | ||
We could have done this in the constructor, but to maintain backwards compatibility with classes | ||
which did not call super in the constructor, we do this separately. This also simplifies the | ||
implementation of a subclass as a super call to the parent constructor is not required. | ||
""" | ||
|
||
self._bot = bot | ||
self._update = update | ||
self._kwargs = kwargs | ||
|
||
@property | ||
def bot(self): | ||
return self._bot | ||
|
||
@property | ||
def update(self): | ||
return self._update | ||
|
||
@property | ||
def kwargs(self): | ||
return self._kwargs | ||
|
||
@classmethod | ||
def as_command_view(cls, **initkwargs): | ||
def as_command_view(cls, *initargs, **initkwargs): | ||
def view(bot, update, **kwargs): | ||
self = cls(**initkwargs) | ||
return self.handle(bot, update, **kwargs) | ||
try: | ||
self = cls(*initargs, **initkwargs) | ||
self.init(bot, update, **kwargs) | ||
return self.handle() | ||
except: | ||
exc_info = sys.exc_info() | ||
traceback.print_exception(*exc_info) | ||
raise | ||
return view |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
from telegrambot.bot_views.generic.base import TemplateCommandView | ||
from telegrambot.bot_views.generic.base import BaseCommandView | ||
|
||
class ListDetailCommandView(TemplateCommandView): | ||
class ListDetailCommandView(BaseCommandView): | ||
list_view_class = None | ||
detail_view_class = None | ||
|
||
@classmethod | ||
def as_command_view(cls, **initkwargs): | ||
def as_command_view(cls, *initargs, **initkwargs): | ||
def view(bot, update, **kwargs): | ||
command_args = update.message.text.split(' ') | ||
args = [] | ||
|
||
if len(command_args) > 1: | ||
self = cls.detail_view_class(command_args[1]) | ||
class_ = cls.detail_view_class | ||
args.append(command_args[1]) | ||
else: | ||
self = cls.list_view_class() | ||
return self.handle(bot, update, **kwargs) | ||
return view | ||
class_ = cls.list_view_class | ||
|
||
return class_.as_command_view(*args)(bot, update, **kwargs) | ||
return view |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from telegrambot.bot_views.generic.responses import TemplateResponse, KeyboardResponse | ||
from telegrambot.bot_views.generic.base import BaseCommandView | ||
from telegram import ParseMode, ReplyKeyboardRemove | ||
import sys | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
PY3 = sys.version_info > (3,) | ||
|
||
|
||
class SendMessageCommandView(BaseCommandView): | ||
message = None | ||
keyboard = ReplyKeyboardRemove() | ||
parse_mode = ParseMode.MARKDOWN | ||
|
||
def __init__(self, **initkwargs): | ||
if initkwargs.get('message', None) is not None: | ||
self.message = initkwargs.get('message') | ||
|
||
def get_chat_id(self): | ||
return self.update.message.chat_id | ||
|
||
def get_keyboard(self): | ||
return self.keyboard | ||
|
||
def get_message(self): | ||
return self.message | ||
|
||
def get_parse_mode(self): | ||
return self.parse_mode | ||
|
||
def handle(self): | ||
self.send_message() | ||
|
||
def send_message(self): | ||
chat_id = self.get_chat_id() | ||
text = self.get_message() | ||
keyboard = self.get_keyboard() | ||
parse_mode = self.get_parse_mode() | ||
|
||
if not text: | ||
logger.info('No text response for update %s' % str(self.update)) | ||
return | ||
|
||
if not PY3: | ||
text = text.encode('utf-8') | ||
|
||
self.bot.send_message(chat_id=chat_id, text=text, reply_markup=keyboard, parse_mode=parse_mode) | ||
|
||
|
||
class HelloWorldCommandView(SendMessageCommandView): | ||
message = 'Hello World!' | ||
|
||
|
||
class EchoCommandView(SendMessageCommandView): | ||
"""Command which responds with the message received.""" | ||
def get_message(self): | ||
return self.update.message.text | ||
|
||
|
||
class TemplateCommandView(SendMessageCommandView): | ||
"""Send a message from a template | ||
Use the properties 'template_text' and 'template_keyboard' to define | ||
which template to use for rendering the message and the keyboard. | ||
And override the method 'get_context' to return the context variables | ||
for both templates. | ||
""" | ||
template_text = None | ||
template_keyboard = None | ||
|
||
def get_context(self, bot, update, **kwargs): | ||
return None | ||
|
||
def get_keyboard(self): | ||
ctx = self.get_context(self.bot, self.update, **self.kwargs) | ||
return KeyboardResponse(self.template_keyboard, ctx).render() | ||
|
||
def get_message(self): | ||
ctx = self.get_context(self.bot, self.update, **self.kwargs) | ||
text = TemplateResponse(self.template_text, ctx).render() | ||
return text | ||
|
||
def handle(self, *args, **kwargs): | ||
# To maintain backwards compatibility we re-implement part of what is done in BaseCommandView::init so | ||
# that the logic in this class can work fine even if the method init wasn't called as it should. | ||
if len(args) > 0 or 'kwargs' in kwargs: | ||
logger.warning("The arguments bot, update and kwargs should not be passed to handle(), " | ||
" they are now accessible as properties. Support for this will be removed in the future. " | ||
" Were you trying to trigger the view manually? In which case," | ||
" use View::as_command_view()(bot, update, **kwargs) instead.") | ||
self._bot = args[0] | ||
self._update = args[1] | ||
self._kwargs = kwargs.get('kwargs', {}) | ||
|
||
super(TemplateCommandView, self).handle() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.