Skip to content

Commit

Permalink
Include the method name in transcripts (for all methods except `send_…
Browse files Browse the repository at this point in the history
…message`).

Preparing for #97.
  • Loading branch information
nmlorg committed Jun 25, 2024
1 parent 21cdd85 commit cf12ea5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
20 changes: 13 additions & 7 deletions metabot/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test environment defaults."""

import functools
import json

import ntelebot
Expand All @@ -22,10 +23,15 @@ def _disable_geoutil(monkeypatch):


def _format_message(response):
method = response.pop('method')
text = response.pop('text', None) or response.pop('caption', '(EMPTY MESSAGE)')
reply_markup = response.pop('reply_markup', None)
header = ' '.join('%s=%s' % (field, value) for field, value in sorted(response.items()))
text = '[%s]\n%s\n' % (header, text)
if method != 'send_message':
method = f'{method} '
else:
method = ''
text = f'[{method}{header}]\n{text}\n'
if reply_markup:
for row in reply_markup.pop('inline_keyboard', ()):
text = '%s%s\n' % (text, ' '.join(
Expand Down Expand Up @@ -98,21 +104,21 @@ def raw_message(self,
if forward_from:
message['forward_from'] = {'id': forward_from}
update = {'message': message}

responses = []

def _handler(request, unused_context):
def _handler(method, request, unused_context):
response = json.loads(request.body.decode('ascii'))
response['method'] = method
responses.append(response)
message = {'message_id': 12345}
if response.get('caption'):
message['caption'] = 'CAPTION'
return {'ok': True, 'result': message}

self.bot.edit_message_caption.respond(json=_handler)
self.bot.edit_message_text.respond(json=_handler)
self.bot.forward_message.respond(json=_handler)
self.bot.send_message.respond(json=_handler)
self.bot.send_photo.respond(json=_handler)
for method in ('edit_message_caption', 'edit_message_text', 'forward_message',
'send_message', 'send_photo'):
getattr(self.bot, method).respond(json=functools.partial(_handler, method))
self.multibot.dispatcher(self.bot, update)
return responses

Expand Down
10 changes: 5 additions & 5 deletions metabot/modules/reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ def _daily_messages(multibot, records): # pylint: disable=too-many-branches,too
logging.exception('While sending to %s:\n%s', groupid, updtext)
continue

updated = 'Updated ' + humanize.time(nowdt)
suffix = 'Updated ' + humanize.time(nowdt)
groupidnum = int(groupid)
if -1002147483647 <= groupidnum < -1000000000000:
updated = '<a href="https://t.me/c/%s/%s">%s</a>' % (
-1000000000000 - groupidnum, updmessage['message_id'], updated)
suffix = '<a href="https://t.me/c/%s/%s">%s</a>' % (
-1000000000000 - groupidnum, updmessage['message_id'], suffix)
else:
updated = '%s (%s)' % (updated, updmessage['message_id'])
newtext = '%s\n\n[%s]' % (text, updated)
suffix = '%s (%s)' % (suffix, updmessage['message_id'])
newtext = '%s\n\n[%s]' % (text, suffix)
message = reminder_edit(bot, groupid, lastmessage['message_id'], newtext,
lastmessage.get('caption'))

Expand Down
5 changes: 4 additions & 1 deletion metabot/modules/test_moderator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_forward(conversation): # pylint: disable=redefined-outer-name
'from'] = -1001000001000

assert conversation.message('Test Forward', chat_type='channel') == """\
[chat_id=-1002000002000 disable_notification=True from_chat_id=-1001000001000 message_id=2000]
[forward_message chat_id=-1002000002000 disable_notification=True from_chat_id=-1001000001000 message_id=2000]
(EMPTY MESSAGE)
"""

Expand Down Expand Up @@ -172,6 +172,7 @@ def test_admin(conversation): # pylint: disable=redefined-outer-name
'chat_id': -1001000001000,
'disable_notification': True,
'disable_web_page_preview': True,
'method': 'send_message',
'parse_mode': 'HTML',
'reply_to_message_id': 5000,
'text': 'Welcome to My Group, <a href="tg://user?id=3000">User 3000</a>! <b>Initial</b> pinned message.',
Expand All @@ -197,6 +198,7 @@ def test_admin(conversation): # pylint: disable=redefined-outer-name
'chat_id': -1001000001000,
'disable_notification': True,
'disable_web_page_preview': True,
'method': 'send_message',
'parse_mode': 'HTML',
'reply_to_message_id': 5000,
'text': 'Welcome to My Group, <a href="tg://user?id=3000">User 3000</a>! <b>Initial</b> <a href="https://t.me/c/1000001000/6000">pinned message</a>.',
Expand All @@ -214,6 +216,7 @@ def test_admin(conversation): # pylint: disable=redefined-outer-name
'chat_id': -1001000001000,
'disable_notification': True,
'disable_web_page_preview': True,
'method': 'send_message',
'parse_mode': 'HTML',
'reply_to_message_id': 5000,
'text': 'Welcome to My Group, <a href="tg://user?id=3000">User 3000</a>! <b>Initial</b> <a href="https://t.me/mygroup/6000">pinned message</a>.',
Expand Down
20 changes: 10 additions & 10 deletions metabot/modules/test_reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def test_daily_messages_updated(daily_messages): # pylint: disable=redefined-ou
…2:16–12:34ᵃᵐ
[chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
[edit_message_text chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
There are a couple events coming up:
<b>Edited Summary</b>
Expand Down Expand Up @@ -284,7 +284,7 @@ def test_daily_messages_future(daily_messages, monkeypatch): # pylint: disable=
Now Summary
[chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
[edit_message_text chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
There are a couple events coming up:
<b>Now Summary</b>
Expand Down Expand Up @@ -330,7 +330,7 @@ def test_daily_messages_multiline(daily_messages): # pylint: disable=redefined-
Multi Line Description
[chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
[edit_message_text chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
There are a couple events coming up:
<b>Alpha Summary</b>
Expand Down Expand Up @@ -389,7 +389,7 @@ def test_daily_messages_add_remove_event(conversation, daily_messages): # pylin
◦ Removed.
[chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
[edit_message_text chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
There are a couple events coming up:
<b>Alpha Summary</b>
Expand Down Expand Up @@ -434,7 +434,7 @@ def test_daily_messages_icons(conversation, daily_messages): # pylint: disable=
Board Games!
[chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
[edit_message_text chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
There are a couple events coming up:
<b>Alpha Summary</b>
Expand All @@ -447,7 +447,7 @@ def test_daily_messages_icons(conversation, daily_messages): # pylint: disable=

# But an initial announcement does (photo=...).
assert daily_messages(True)[1] == """\
[chat_id=-1002000002000 disable_notification=True parse_mode=HTML photo=https://ssl.gstatic.com/calendar/images/eventillustrations/v1/img_gamenight_2x.jpg]
[send_photo chat_id=-1002000002000 disable_notification=True parse_mode=HTML photo=https://ssl.gstatic.com/calendar/images/eventillustrations/v1/img_gamenight_2x.jpg]
There are a couple events coming up:
<b>Alpha Summary</b>
Expand All @@ -459,7 +459,7 @@ def test_daily_messages_icons(conversation, daily_messages): # pylint: disable=
conversation.bot.config['issue37']['events']['series']['ha sum'] = 'SERIES ICON'

assert daily_messages(True)[1] == """\
[chat_id=-1002000002000 disable_notification=True parse_mode=HTML photo=SERIES ICON]
[send_photo chat_id=-1002000002000 disable_notification=True parse_mode=HTML photo=SERIES ICON]
There are a couple events coming up:
<b>Alpha Summary</b>
Expand All @@ -471,7 +471,7 @@ def test_daily_messages_icons(conversation, daily_messages): # pylint: disable=
conversation.bot.config['issue37']['events']['events']['6fc2c510:alpha'] = 'EVENT ICON'

assert daily_messages(True)[1] == """\
[chat_id=-1002000002000 disable_notification=True parse_mode=HTML photo=EVENT ICON]
[send_photo chat_id=-1002000002000 disable_notification=True parse_mode=HTML photo=EVENT ICON]
There are a couple events coming up:
<b>Alpha Summary</b>
Expand All @@ -491,7 +491,7 @@ def test_daily_messages_icons(conversation, daily_messages): # pylint: disable=
Fun Games!
[chat_id=-1002000002000 message_id=12345 parse_mode=HTML]
[edit_message_caption chat_id=-1002000002000 message_id=12345 parse_mode=HTML]
There are a couple events coming up:
<b>Alpha Summary</b>
Expand Down Expand Up @@ -531,7 +531,7 @@ def test_daily_messages_geometry(conversation, daily_messages): # pylint: disab
Trigger
[chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
[edit_message_text chat_id=-1002000002000 disable_web_page_preview=True message_id=12345 parse_mode=HTML]
There's an event coming up:
<b>Alpha Summary</b>
Expand Down

0 comments on commit cf12ea5

Please sign in to comment.