-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
version 0.3 send and receive document support
- Loading branch information
1 parent
3052be8
commit 48e0f4c
Showing
24 changed files
with
728 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello world! | ||
Hello world! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.