Skip to content

Commit

Permalink
Allow documents and photos to include captions, simply as any text fo…
Browse files Browse the repository at this point in the history
…llowing the document:DOCUMENT_ID / photo:PHOTO_ID text.

See nmlorg/metabot#74.
  • Loading branch information
nmlorg committed Aug 24, 2019
1 parent 2ea50df commit 863284a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
10 changes: 6 additions & 4 deletions ntelebot/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def __call__(self, bot, update): # pylint: disable=too-many-branches,too-many-s
ctx.user = payload['from']
ctx.chat = payload['message']['chat']
ctx.edit_id = payload['message']['message_id']
ctx.callback_id = payload['id']
text = ntelebot.keyboardutil.decode(payload['message'], payload['data'])
ctx.command, ctx.text = get_command(text, bot.username)
ctx.prefix = ctx.text.partition(' ')[0]
Expand Down Expand Up @@ -111,6 +112,7 @@ class Context(object):
forward_from = reply_from = None
document = photo = sticker = None
reply_id = edit_id = answer_id = None
callback_id = None

def __init__(self, conversations, bot):
self._conversations = conversations
Expand Down Expand Up @@ -157,12 +159,12 @@ def reply_text(self, text, *args, **kwargs): # pylint: disable=too-many-branche
text = text_prefix + text

if self.reply_id:
if text.startswith('document:') and ' ' not in text:
if text.startswith('document:'):
method = self.bot.send_document
kwargs['document'] = text[len('document:'):]
elif text.startswith('photo:') and ' ' not in text:
kwargs.update(zip(('document', 'caption'), text[len('document:'):].split(None, 1)))
elif text.startswith('photo:'):
method = self.bot.send_photo
kwargs['photo'] = text[len('photo:'):]
kwargs.update(zip(('photo', 'caption'), text[len('photo:'):].split(None, 1)))
elif text.startswith('sticker:') and ' ' not in text:
method = self.bot.send_sticker
kwargs['sticker'] = text[len('sticker:'):]
Expand Down
20 changes: 10 additions & 10 deletions ntelebot/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ def send_message( # pylint: disable=unused-argument,too-many-arguments
return reply_to_message_id and 'REPLY' or 'SEND'

@staticmethod
def send_document(chat_id=None, document=None): # pylint: disable=unused-argument
return 'DOCUMENT'
def send_document(chat_id=None, document=None, caption=None): # pylint: disable=unused-argument
return 'DOCUMENT=%s CAPTION=%s' % (document, caption)

@staticmethod
def send_photo(chat_id=None, photo=None): # pylint: disable=unused-argument
return 'PHOTO'
def send_photo(chat_id=None, photo=None, caption=None): # pylint: disable=unused-argument
return 'PHOTO=%s CAPTION=%s' % (photo, caption)

@staticmethod
def send_sticker(chat_id=None, sticker=None): # pylint: disable=unused-argument
return 'STICKER'
return 'STICKER=%s' % sticker


def test_unknown_update():
Expand Down Expand Up @@ -494,12 +494,12 @@ def test_media():
assert ctx.photo is None
assert ctx.sticker == 'AAA1234'

assert ctx.reply_text('document:BOGUS DATA') == 'SEND'
assert ctx.reply_text('document:DOCUMENT_ID') == 'DOCUMENT'
assert ctx.reply_text('photo:BOGUS DATA') == 'SEND'
assert ctx.reply_text('photo:PHOTO_ID') == 'PHOTO'
assert ctx.reply_text('document:DOCUMENT_ID MY TEXT') == 'DOCUMENT=DOCUMENT_ID CAPTION=MY TEXT'
assert ctx.reply_text('document:DOCUMENT_ID') == 'DOCUMENT=DOCUMENT_ID CAPTION=None'
assert ctx.reply_text('photo:PHOTO_ID MY TEXT') == 'PHOTO=PHOTO_ID CAPTION=MY TEXT'
assert ctx.reply_text('photo:PHOTO_ID') == 'PHOTO=PHOTO_ID CAPTION=None'
assert ctx.reply_text('sticker:BOGUS DATA') == 'SEND'
assert ctx.reply_text('sticker:STICKER_ID') == 'STICKER'
assert ctx.reply_text('sticker:STICKER_ID') == 'STICKER=STICKER_ID'


def test_new_chat_members():
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setuptools.setup(
name='ntelebot',
version='0.3.2',
version='0.3.3',
author='Daniel Reed',
author_email='[email protected]',
description='A simple implementation of https://core.telegram.org/bots/api.',
Expand Down

0 comments on commit 863284a

Please sign in to comment.