Package: tgintegration
TgIntegrationAn integration test and automation library forTelegram Botsbased onPyrogram.Test your bot in realtime scenarios!Are you a user of TgIntegration?I'm actively looking for feedback and ways to improve the library, come and let me know in theofficial group!Features•Requirements•Installation•Quick Start Guide•Test Frameworks📖Documentation👥Telegram Chat📄 Free software:MIT LicenseBuilt withPyCharmFeatures▶️See it in action!🎬👤 Log into a Telegram user account and interact with bots or other users✅ Writerealtime integration teststo ensure that your bot works as expected! ▶️Pytest examples⚡️Automate any interactionon Telegram! ▶️Automatically play @IdleTownBot|More examples🛡 Fully typed for safety andautocompletionwith your favorite IDE🐍 Built for modern Python (3.8+) with high test coveragePrerequisitesSame as Pyrogram:ATelegram API key.A user session (seeing things happen in your own account is great for getting started)But:Python 3.8or higher!A basic understanding of async/await andasynchronous context managersis assumed, as TgIntegration heavily relies on the latter to automate conversations.InstallationAll hail pip!$pip install tgintegration --upgradeFeeling adventurous?For bleeding edge, install the master branch:$pip install git+https://github.com/JosXa/tgintegration.gitQuick Start GuideYou canfollow along by running the example(README)SetupSuppose we want to write integration tests for@BotListBotby sending it a couple of
messages and checking that it responds the way it should.Afterconfiguring a Pyrogramuser client,
let's start by creating aBotController:fromtgintegrationimportBotControllercontroller=BotController(peer="@BotListBot",# The bot under test is https://t.me/BotListBot 🤖client=client,# This assumes you already have a Pyrogram user client availablemax_wait=8,# Maximum timeout for responses (optional)wait_consecutive=2,# Minimum time to wait for more/consecutive messages (optional)raise_no_response=True,# Raise `InvalidResponseError` when no response is received (defaults to True)global_action_delay=2.5# Choosing a rather high delay so we can observe what's happening (optional))awaitcontroller.clear_chat()# Start with a blank screen (⚠️)Now, let's send/startto the bot and wait until exactly three messages have been received by using the asynchronouscollectcontext manager:asyncwithcontroller.collect(count=3)asresponse:awaitcontroller.send_command("start")assertresponse.num_messages==3# Three messages received, bundled under a `Response` objectassertresponse.messages[0].sticker# The first message is a stickerThe result should look like this:Examining the buttons in the response...# Get first (and only) inline keyboard from the repliesinline_keyboard=response.inline_keyboards[0]# Three buttons in the first rowassertlen(inline_keyboard.rows[0])==3We can also press the inline keyboard buttons, for example based on a regular expression:examples=awaitinline_keyboard.click(pattern=r".*Examples")As the bot edits the message,.click()automatically listens for "message edited" updates and returns
the new state as anotherResponse.assert"Examples for contributing to the BotList"inexamples.full_textError handlingSo what happens when we send an invalid query or the peer fails to respond?The following instruction will raise anInvalidResponseErroraftercontroller.max_waitseconds.
This is because we passedraise_no_response=Trueduring controller initialization.try:asyncwithcontroller.collect():awaitcontroller.send_command("ayylmao")exceptInvalidResponseError:pass# OKLet's explicitly setraise_toFalseso that no exception occurs:asyncwithcontroller.collect(raise_=False)asresponse:awaitclient.send_message(controller.peer_id,"Henlo Fren")In this case,tgintegrationwill simply emit a warning, but you can still assert
that no response has been received by using theis_emptyproperty:assertresponse.is_emptyIntegrating with Test FrameworkspytestPytest is the recommended test framework for use withtgintegration. You canbrowse through several examplesandtgintegrationalso uses pytest for its own test suite.unittestI haven't tried out the builtinunittestlibrary in combination withtgintegrationyet,
but theoretically I don't see any problems with it.
If you do decide to try it, it would be awesome if you could tell me about your
experience and whether anything could be improved 🙂
Let us know at 👉https://t.me/TgIntegrationor in an issue.