Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: send message in groupchat #7

Open
SpangleLabs opened this issue Feb 2, 2020 · 7 comments
Open

Feature request: send message in groupchat #7

SpangleLabs opened this issue Feb 2, 2020 · 7 comments
Labels

Comments

@SpangleLabs
Copy link
Contributor

  • 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

@JosXa JosXa added the enhancement New feature or request label Oct 19, 2020
@JosXa
Copy link
Owner

JosXa commented Oct 19, 2020

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:)?

@SpangleLabs
Copy link
Contributor Author

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!

@JosXa
Copy link
Owner

JosXa commented Oct 23, 2020

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

@JosXa
Copy link
Owner

JosXa commented Oct 23, 2020

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

@SpangleLabs
Copy link
Contributor Author

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.

Copy link
Owner

JosXa commented Oct 23, 2020

@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):
    ...

Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants