Skip to content

Commit

Permalink
version 0.3 send and receive document support
Browse files Browse the repository at this point in the history
  • Loading branch information
dontsovcmc committed May 24, 2022
1 parent 3052be8 commit 48e0f4c
Show file tree
Hide file tree
Showing 24 changed files with 728 additions and 124 deletions.
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ How it works

**This is a first version of library**

**Only long polling mode supported!**

Library starts your python-telegram-bot object with custom url (our unit-test server on Flask running under waitress).
Now you can communicate in unit-tests with your bot as you do in Telegram.

Expand All @@ -15,6 +17,8 @@ Features

1. send text message
2. send command
3. send file
4. receive file


Fixtures
Expand Down Expand Up @@ -53,6 +57,27 @@ Check /start command of Echo Bot
message = user.get_message()
assert message['text'] == 'Hi [FN LN](tg://user?id=1)\!'
File Bot example
---------------------------

Bot renames file you send to him.

.. code::
def test_echobot_file(bot, user):
current_dir = os.path.dirname(os.path.abspath(__file__))
user.send_document(current_dir, 'test.txt')
document = user.get_document()
file_io = document.path_or_bytes
assert file_io.name == 'echo_test.txt'
content = io.TextIOWrapper(file_io, encoding='utf-8').read()
assert content == 'Hello world!\nHello world!'
==========
Installing
==========
Expand Down
7 changes: 6 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
import pytest
from _pytest.main import Session


@pytest.hookimpl()
def pytest_sessionstart(session: Session) -> None:

print("Start pytest testing")


# Load fixtures
pytest_plugins = [
'telegram_bot_unittest.fixtures',
'echobot.fixtures'
'telegram_bot_unittest.pytest.fixtures'

#'examples.echobot.fixtures',
#'examples.filebot.fixtures'
]
File renamed without changes.
Empty file added examples/echobot/__init__.py
Empty file.
11 changes: 1 addition & 10 deletions echobot/echobot.py → examples/echobot/echobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
BOT_TOKEN = os.getenv("BOT_TOKEN")


# Define a few command handlers. These usually take the two arguments update and
# context.
def start(update: Update, context: CallbackContext) -> None:
"""Send a message when the command /start is issued."""
user = update.effective_user
Expand All @@ -30,20 +28,16 @@ def echo(update: Update, context: CallbackContext) -> None:

def setup_bot(bot_token: str, base_url: str = None) -> Updater:
"""Start the bot."""
# Create the Updater and pass it your bot's token.

updater = Updater(bot_token, base_url=base_url)

# Get the dispatcher to register handlers
dispatcher = updater.dispatcher

# on different commands - answer in Telegram
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help_command))

# on non command i.e message - echo the message on Telegram
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))

# Start the Bot
updater.start_polling()

return updater
Expand All @@ -53,9 +47,6 @@ def main(base_url: str = None) -> None:

updater = setup_bot(BOT_TOKEN, base_url)

# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()


Expand Down
6 changes: 4 additions & 2 deletions echobot/fixtures.py → examples/echobot/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

import pytest
from echobot.echobot import setup_bot

from examples.echobot.echobot import setup_bot

from telegram_bot_unittest.routes import TELEGRAM_URL
from telegram_bot_unittest.user import BOT_TOKEN, CHAT_ID

Expand All @@ -18,7 +20,7 @@ def bot(telegram_server):
user2_id = CHAT_ID+1

u2 = UserBase(user2_id)
chat2 = ChatBase(user2_id)
chat2 = ChatBase(u2)


@pytest.fixture(scope='session')
Expand Down
18 changes: 14 additions & 4 deletions echobot/pytest_echobot.py → examples/echobot/pytest_echobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_echobot_start(bot, user):
message = user.get_message()

assert message
assert message['text'] == 'Hi [FN LN](tg://user?id=1)\!'
assert message['text'] == 'Hi [FirstName LastName](tg://user?id=1)\!'


def test_echobot_help(bot, user):
Expand All @@ -20,7 +20,7 @@ def test_echobot_help(bot, user):
assert message['text'] == 'Help!'


def test_echobot_message(bot, user, user2):
def test_echobot_message(bot, user):

user.send_message('testing message')

Expand All @@ -29,9 +29,19 @@ def test_echobot_message(bot, user, user2):
assert message
assert message['text'] == 'testing message'

user2.send_message('testing message')

def test_echobot_multiple_users(bot, user, user2):

user.send_message('my name user1')

message = user.get_message()

assert message
assert message['text'] == 'my name user1'

user2.send_message('my name user2')

message = user2.get_message()

assert message
assert message['text'] == 'testing message'
assert message['text'] == 'my name user2'
Empty file added examples/filebot/__init__.py
Empty file.
56 changes: 56 additions & 0 deletions examples/filebot/filebot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

import os
import io

from telegram import Update, ForceReply
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext


BOT_TOKEN = os.getenv("BOT_TOKEN")


def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Hi!')


def echo_document(update: Update, context: CallbackContext) -> None:
"""Echo the user message."""
chat_id = update.message.chat_id
user_id = update.message.from_user.id

attachment = io.BytesIO()
context.bot.get_file(update.message.document).download(out=attachment)
attachment.seek(0)

content = attachment.read().decode('utf-8')

f = io.BytesIO(bytes(content, "utf-8"))
f.name = 'echo_' + update.message.document.file_name
f.seek(0)

context.bot.send_document(chat_id, f)


def setup_bot(bot_token: str, base_url: str = None) -> Updater:
"""Start the bot."""
updater = Updater(bot_token, base_url=base_url, base_file_url=base_url)

dispatcher = updater.dispatcher

dispatcher.add_handler(CommandHandler("start", start))

dispatcher.add_handler(MessageHandler(Filters.attachment, echo_document))

updater.start_polling()

return updater


def main(base_url: str = None) -> None:

updater = setup_bot(BOT_TOKEN, base_url)
updater.idle()


if __name__ == '__main__':
main()
12 changes: 12 additions & 0 deletions examples/filebot/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

import pytest
from examples.filebot.filebot import setup_bot
from telegram_bot_unittest.routes import TELEGRAM_URL
from telegram_bot_unittest.user import BOT_TOKEN


@pytest.fixture(scope='session')
def bot(telegram_server):
updater = setup_bot(BOT_TOKEN, TELEGRAM_URL)
yield updater.bot
updater.stop()
27 changes: 27 additions & 0 deletions examples/filebot/pytest_filebot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import io


def test_echobot_start(bot, user):

user.send_command('/start')

message = user.get_message()

assert message
assert message['text'] == 'Hi!'


def test_echobot_file(bot, user):

current_dir = os.path.dirname(os.path.abspath(__file__))

user.send_document(current_dir, 'test.txt')

document = user.get_document()

file_io = document.path_or_bytes
assert file_io.name == 'echo_test.txt'

content = io.TextIOWrapper(file_io, encoding='utf-8').read()
assert content == 'Hello world!\nHello world!'
2 changes: 2 additions & 0 deletions examples/filebot/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello world!
Hello world!
3 changes: 2 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[pytest]
FAIL_INVALID_TEMPLATE_VARS = True
norecursedirs = venv
python_files = pytest_*.py
#python_files = pytest_*.py
python_files = test_*.py
addopts = -p no:warnings --strict-markers --log-cli-level=INFO

3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
python-telegram-bot
pytest
pytest-cov
flask
waitress

pytest-cov
codecov
10 changes: 7 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from setuptools import setup

VERSION_MAJOR = 0
VERSION_MINOR = 2
VERSION_MINOR = 3

ver = '%d.%d' % (VERSION_MAJOR, VERSION_MINOR)

backlog = """
0.3 - 22.05.24 - send & receive documents
0.2 - 22.04.13 - add multiple users
Expand Down Expand Up @@ -34,8 +35,11 @@
url='https://github.com/dontsovcmc/telegram_bot_unittest',
include_package_data=True,
packages=[
'echobot',
'telegram_bot_unittest'
'examples',
'examples.echobot',
'examples.filebot',
'telegram_bot_unittest',
'telegram_bot_unittest.pytest'
],
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
Loading

0 comments on commit 48e0f4c

Please sign in to comment.