I wrote a bot using Python Telegram Bot. Then, i wanted to write some automatic tests for it. And that’s the problem: i needed a way to send messages, wait for my bot to reply, and then react to those messages (either sending more text or clicking on buttons).
Well, i didn’t found a good answer for it online.
2
Answers
So i had to crack my head and think on something on my own. Maybe it is not the better, or the most performatic, but solved ALL my problems, and in a clean way.
So my idea was to use a Producer-Consumer schema where i set somehwere to store all my updates and the tests were the consumer. Further, i decided to use an Asyncio Queue. It has methods to hang up and wait for items to be stored on queue. Exactly what i needed.
Now, some code. First of all, i created a CustomQueue class, extending from Asyncio.Queue where i could implement some functions like
skip_n_messages
ordiscard_n_messages
, to keep my code DRY.CustomQueue:
Now, i need to set somewhere that i want all the incoming messages to be put on CustomQueue. Because i writing tests, i wrote it on a fixture:
Now, some use cases:
Now, to the downside: I really really don't know if this schema supports concurrent updates. An workaround would be setting a message_queue for each command, by setting up some update filters, but i never tested this (since my bot is written with another library)
If I understand your question correctly, you want to test a bot from the user-side?
There it tgintegration out there, which seems to be doing exactly what you want:
https://github.com/JosXa/tgintegration