All Projects → JosXa → tgintegration

JosXa / tgintegration

Licence: MIT license
Integration test and automation library for Telegram Messenger bots based on Pyrogram.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to tgintegration

KAI
KAI is a distributed computing model written in modern C++ and is cross-plaftorm. Using custom language translators and an executor, KAI provides full reflection, persistence and cross-process communications without having to modify existing source code. KAI Comes with an automated, generational tricolor garbage collector, and Console- and Windo…
Stars: ✭ 13 (-88.5%)
Mutual labels:  tests
emaile2e-javascript-client
Test email integration with your app using MailSlurp
Stars: ✭ 14 (-87.61%)
Mutual labels:  integration
Blankly
🚀 💸 Easily build, backtest and deploy your algo in just a few lines of code. Trade stocks, cryptos, and forex across exchanges w/ one package.
Stars: ✭ 1,456 (+1188.5%)
Mutual labels:  bots
EntityFrameworkCore.AutoFixture
A library aimed to minimize the boilerplate required to unit-test Entity Framework Core code using AutoFixture and in-memory providers.
Stars: ✭ 31 (-72.57%)
Mutual labels:  tests
testable
QT/QML Test Runner and Utilities
Stars: ✭ 53 (-53.1%)
Mutual labels:  tests
xharness
C# command line tool for running tests on Android / iOS / tvOS devices and simulators
Stars: ✭ 123 (+8.85%)
Mutual labels:  tests
Daisy-OLD
“ Hey there 👋 I'm Daisy „ PTB Group management bot with some extra features
Stars: ✭ 43 (-61.95%)
Mutual labels:  pyrogram
jest-partial
A partial matcher for Jest to simplify validation of complex structures.
Stars: ✭ 21 (-81.42%)
Mutual labels:  tests
extensions
Code Generators and Extensions for vanilla-rtb stack
Stars: ✭ 16 (-85.84%)
Mutual labels:  integration
eg
eg delivers clojure.test function tests with conciseness
Stars: ✭ 24 (-78.76%)
Mutual labels:  tests
fem mesh matlab
MATLAB Toolbox for Handling 2D and 3D FEM Meshes
Stars: ✭ 23 (-79.65%)
Mutual labels:  integration
BroadcastBot
A simple Telegram bot that can broadcast messages and media to the bot subscribers. with mongo DB support
Stars: ✭ 73 (-35.4%)
Mutual labels:  pyrogram
OrionServer
An open-source, centralized HTTPS botnet
Stars: ✭ 58 (-48.67%)
Mutual labels:  bots
openjpeg-data
Test files for the OpenJPEG libraries and utilities
Stars: ✭ 37 (-67.26%)
Mutual labels:  tests
7182
Curso 7182 - Refatorando para testes de unidade
Stars: ✭ 21 (-81.42%)
Mutual labels:  tests
clonings
A project for learning Clojure, based on rustlings.
Stars: ✭ 32 (-71.68%)
Mutual labels:  tests
StringSessionBot
Pyrogram and Telethon String Session Generator
Stars: ✭ 63 (-44.25%)
Mutual labels:  pyrogram
Message-Manager-Bot
A Telegram Message Manager Bot by @AbirHasan2005
Stars: ✭ 32 (-71.68%)
Mutual labels:  pyrogram
MaratonaBots
Códigos em Node.js da Maratona Bots
Stars: ✭ 29 (-74.34%)
Mutual labels:  bots
telectron
Telegram desktop client [WIP]
Stars: ✭ 19 (-83.19%)
Mutual labels:  bots

TgIntegration

An integration test and automation library for Telegram Bots based on Pyrogram.
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 the official group!

PyPI - Python Version PyPI - Downloads PyPI GitHub top language GitHub Workflow Status (branch) GitHub Workflow Status

FeaturesRequirementsInstallationQuick Start GuideTest Frameworks

Features

▶️ See it in action! 🎬

  • 👤 Log into a Telegram user account and interact with bots or other users
  • Write realtime integration tests to ensure that your bot works as expected! ▶️ Pytest examples
  • ⚡️ Automate any interaction on Telegram! ▶️ Automatically play @IdleTownBot | More examples
  • 🛡 Fully typed for safety and autocompletion with your favorite IDE
  • 🐍 Built for modern Python (3.8+) with high test coverage

Prerequisites

Same as Pyrogram:

  • A Telegram API key.
  • A user session (seeing things happen in your own account is great for getting started)
  • But: Python 3.8 or higher!

A basic understanding of async/await and asynchronous context managers is assumed, as TgIntegration heavily relies on the latter to automate conversations.

Installation

All hail pip!

$ pip install tgintegration --upgrade


Feeling adventurous?

For bleeding edge, install the master branch:

$ pip install git+https://github.com/JosXa/tgintegration.git

Quick Start Guide

You can follow along by running the example (README)

Setup

Suppose we want to write integration tests for @BotListBot by sending it a couple of messages and checking that it responds the way it should.

After configuring a Pyrogram user client, let's start by creating a BotController:

from tgintegration import BotController

controller = BotController(
    peer="@BotListBot",      # The bot under test is https://t.me/BotListBot 🤖
    client=client,           # This assumes you already have a Pyrogram user client available
    max_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)
)

await controller.clear_chat()  # Start with a blank screen (⚠️)

Now, let's send /start to the bot and wait until exactly three messages have been received by using the asynchronous collect context manager:

async with controller.collect(count=3) as response:
    await controller.send_command("start")

assert response.num_messages == 3  # Three messages received, bundled under a `Response` object
assert response.messages[0].sticker  # The first message is a sticker

The result should look like this:

image

Examining the buttons in the response...

# Get first (and only) inline keyboard from the replies
inline_keyboard = response.inline_keyboards[0]

# Three buttons in the first row
assert len(inline_keyboard.rows[0]) == 3

We can also press the inline keyboard buttons, for example based on a regular expression:

examples = await inline_keyboard.click(pattern=r".*Examples")

As the bot edits the message, .click() automatically listens for "message edited" updates and returns the new state as another Response.

image

assert "Examples for contributing to the BotList" in examples.full_text

Error handling

So what happens when we send an invalid query or the peer fails to respond?

The following instruction will raise an InvalidResponseError after controller.max_wait seconds. This is because we passed raise_no_response=True during controller initialization.

try:
    async with controller.collect():
        await controller.send_command("ayylmao")
except InvalidResponseError:
    pass  # OK

Let's explicitly set raise_ to False so that no exception occurs:

async with controller.collect(raise_=False) as response:
    await client.send_message(controller.peer_id, "Henlo Fren")

In this case, tgintegration will simply emit a warning, but you can still assert that no response has been received by using the is_empty property:

assert response.is_empty

Integrating with Test Frameworks

pytest

Pytest is the recommended test framework for use with tgintegration. You can browse through several examples and tgintegration also uses pytest for its own test suite.

unittest

I haven't tried out the builtin unittest library in combination with tgintegration yet, 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/TgIntegration or in an issue.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].