Skip to main content

Mocked Telegram Bot API elements for unit tests of a bot based on the pyTelegramBotAPI library.

Project description

Library telemulator3

GitHub Workflow Status GitHub Workflow Status Codacy Badge Codacy Badge

The free, open-source telemulator3 library designed to simplify automatic testing of Telegram bots created using the pyTelegramBotAPI library.

The telemulator3 library partially emulates the Telegram Bot API in unit tests and allows you to create typical scenarios for the interaction of your bot with the Telegram Bot API.

Installation

pip install telemulator3

Usage

Create TeleBot instance and start emulate Telegram API for bot.

from telebot import TeleBot
from telemulator3 import Telemulator

bot = TeleBot('xxx-yyy-zzz', threaded=False)

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
    bot.reply_to(message, "Howdy, how are you doing?")

telemul = Telemulator()
telemul.set_tested_bot(bot, username='my_bot', name='My Bot')

At start, there are no registered users in emulated API.

assert not telemul.api.users

Make API user, that represent our bot. It's a first registered user.

mybot = telemul.api.get_me()
assert mybot.is_bot
assert mybot.username == 'my_bot'
assert len(telemul.api.users) == 1

New user open private chat with bot and send /start command. Bot must answer as defined and his answer must be in chat history.

from telemulator3 import send_command

user = telemul.api.create_user('User')
chat = user.private()
send_command(chat, '/start', user)
assert chat.history.contain('Howdy, how are you doing?')

User create group and add bot as member.

group = user.create_group('My group')
group.add_members(user, [mybot])
assert group.history.contain('invite new members:')

And so on.

mybot.leave(group)
assert group.history.contain('My Bot (ID 1) left chat')
# group.history.dump()

Development

git clone git@github.com:vb64/telemulator3
cd telemulator3
make setup PYTHON_BIN=/path/to/python3
make tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telemulator3-1.5.tar.gz (20.3 kB view hashes)

Uploaded Source

Built Distribution

telemulator3-1.5-py3-none-any.whl (30.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page