tgintegration icon indicating copy to clipboard operation
tgintegration copied to clipboard

Feature request: send message in groupchat

Open SpangleLabs opened this issue 5 years ago • 7 comments

  • tgintegration version: 0.16.0
  • Python version: 3.6.3
  • Operating System: Windows

Description

It would be very handy to be able to test that the bot responds correctly to a message in a groupchat. Currently this library only tests against the bot in privmsg

What I Did

Tried feeding chat_id parameter to send_message_await(), but it does not like that, as it's overridden to the bot id

SpangleLabs avatar Feb 02 '20 22:02 SpangleLabs

While this is technically already possible, I agree that it is a good idea to implement this directly in the library.

Since v1.0 you can do the following:

# Controller reacts to messages from @deerspangle
controller = BotController(peer="@deerspangle", client=client)

# This internally merges the controller's `filters.user("@deerspangle")` 
# with `filters.chat("@TgIntegration")` as an AND relation
async with controller.collect(filters.chat("@TgIntegration")) as response:
    await client.send_message("@TgIntegration", "Hello there!")

assert response.messages[0].chat.username == "TgIntegration"
assert response.messages[0].user.username == "deerspangle"

But I do see the need for making tests in groups and channels a high-level concept and would love some ideas on what a good user-facing API for that would be. I'm thinking something like:

controller = BotController(
    peer="@mybot",  # bot under test
    client=client,
    context=InteractionContext(
        username="@somechat",
        type_="group",
        title="Yo I'm just testing man",
        create_if_not_exists=True  # user clients ftw 💪 
    )
)

However,

  1. "context" is maybe too generic?
  2. I'm debating if it should be a dataclass or just a bunch of kwargs passed to the initializer..?
  3. Should you...
  • be able to assign a context to a controller,
  • be forced to always create a new controller,
  • or maybe even set the current context with a @contextmanager (like with controller.in_context(...) as ctx:)?

JosXa avatar Oct 19 '20 02:10 JosXa

I like being able to set the context with a context manager (It's in the name, right? As it means you can test things like: Set something in private message with the bot, then test it is applied in chat. Which seems potentially very useful!

SpangleLabs avatar Oct 23 '20 21:10 SpangleLabs

Oh yes, that's an angle I haven't thought about, you're right!

JosXa avatar Oct 23 '20 21:10 JosXa

Only drawback would be that tests could potentially grow really wide with all those contextanagers..? Like you are always at least two levels deep

JosXa avatar Oct 23 '20 21:10 JosXa

That is true, but feels like it's a rare enough case that people can cope with being tabbed in a few times at that point. Or even extract out parts of their test.

SpangleLabs avatar Oct 23 '20 21:10 SpangleLabs

@joshcoales Hmm, another idea that came to mind was to also stuff that into the central controller.collect ctm 🤔

in_the_group = InteractionContext(...)
in_private = InteractionContext(...)

async with controller.collect(count=3, context=in_the_group):
    ...

JosXa avatar Oct 23 '20 21:10 JosXa

@JosXa Ahh, yeah, that could work also. Might make things a bit neater

SpangleLabs avatar Oct 23 '20 21:10 SpangleLabs