tgintegration
tgintegration copied to clipboard
Feature request: send message in groupchat
- 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
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,
- "context" is maybe too generic?
- I'm debating if it should be a dataclass or just a bunch of kwargs passed to the initializer..?
- 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:
)?
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!
Oh yes, that's an angle I haven't thought about, you're right!
Only drawback would be that tests could potentially grow really wide with all those contextanagers..? Like you are always at least two levels deep
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.
@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 Ahh, yeah, that could work also. Might make things a bit neater