From bf01cc183d24add07a14137d13cc4b833566b5e8 Mon Sep 17 00:00:00 2001 From: Kirill Pushkarev Date: Sun, 5 Nov 2023 17:31:24 +0200 Subject: [PATCH 1/5] chore: add to gitignore local telegram token file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6f10937..ad899f3 100644 --- a/.gitignore +++ b/.gitignore @@ -131,3 +131,4 @@ cython_debug/ # VSCode files .vscode/ +.token From 124a1a5a2061c21154b07164e64675bf52d3f686 Mon Sep 17 00:00:00 2001 From: Kirill Pushkarev Date: Sun, 5 Nov 2023 17:38:18 +0200 Subject: [PATCH 2/5] feature: connect to telegram bot --- src/chatbot/telegram_bot.py | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/chatbot/telegram_bot.py diff --git a/src/chatbot/telegram_bot.py b/src/chatbot/telegram_bot.py new file mode 100644 index 0000000..778c019 --- /dev/null +++ b/src/chatbot/telegram_bot.py @@ -0,0 +1,52 @@ +import os + +from model import BlenderChatbot +from telegram import Update +from telegram.ext import ( + CallbackContext, + CommandHandler, + Filters, + MessageHandler, + Updater, +) + +chatbot = BlenderChatbot() + + +def start(update: Update, context: CallbackContext) -> None: + first_message = "Hello! I am your chatbot. Ask me anything!" + update.message.reply_text(first_message) + chatbot.add_message(content=first_message) + + +def get_response(update: Update, context: CallbackContext) -> None: + user_input = update.message.text + response = chatbot.get_response(user_input) + update.message.reply_text(response) + + +def help(update: Update, context: CallbackContext) -> None: + update.message.reply_text("Help!") + + +def main() -> None: + try: + updater = Updater(str(os.environ.get("TELEGRAM_TOKEN")), use_context=True) + except Exception: + with open(".token", "r") as file: + token = file.readline().strip("\n") + updater = Updater(token, use_context=True) + + dp = updater.dispatcher # type: ignore + + dp.add_handler(CommandHandler("start", start)) + dp.add_handler(CommandHandler("help", help)) + dp.add_handler(MessageHandler(Filters.text & ~Filters.command, get_response)) + + updater.start_polling() + + updater.idle() + + +if __name__ == "__main__": + main() From ed00b9003b2b532600475f30c8b5f9f3a2234712 Mon Sep 17 00:00:00 2001 From: Kirill Pushkarev Date: Sun, 5 Nov 2023 20:05:15 +0200 Subject: [PATCH 3/5] chore: remove main func --- src/chatbot/telegram_bot.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/chatbot/telegram_bot.py b/src/chatbot/telegram_bot.py index 778c019..8da3d65 100644 --- a/src/chatbot/telegram_bot.py +++ b/src/chatbot/telegram_bot.py @@ -29,7 +29,7 @@ def help(update: Update, context: CallbackContext) -> None: update.message.reply_text("Help!") -def main() -> None: +if __name__ == "__main__": try: updater = Updater(str(os.environ.get("TELEGRAM_TOKEN")), use_context=True) except Exception: @@ -46,7 +46,3 @@ def main() -> None: updater.start_polling() updater.idle() - - -if __name__ == "__main__": - main() From 3e0f6510022907b342394a287b1d6b386c249f79 Mon Sep 17 00:00:00 2001 From: Kirill Pushkarev Date: Sun, 5 Nov 2023 20:32:13 +0200 Subject: [PATCH 4/5] test: add tests for telegram_bot.py --- tests/test_telegram_bot.py | 108 +++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 tests/test_telegram_bot.py diff --git a/tests/test_telegram_bot.py b/tests/test_telegram_bot.py new file mode 100644 index 0000000..345c032 --- /dev/null +++ b/tests/test_telegram_bot.py @@ -0,0 +1,108 @@ +# import datetime +# from unittest.mock import MagicMock, patch + +# import pytest +# from telegram import Chat, Message, Update, User, Bot +# from telegram.ext import CallbackContext +# from telegram_bot import get_response, help, start + + +# @pytest.fixture +# def fake_update() -> MagicMock: +# user = User(id=2, first_name="testuser", is_bot=False) +# chat = Chat(id=3, type="private") +# message = Message( +# message_id=1, +# date=datetime.datetime.now(), +# chat=chat, +# text="Test", +# from_user=user, +# bot=MagicMock(spec=Bot) +# ) + +# update = MagicMock(spec=Update) +# update.message = message +# update.update_id = 123 +# update._id_attrs = (update.update_id,) + +# # update.configure_mock(update_id=123, message=message) + +# return update + + +# @pytest.fixture +# def fake_context() -> MagicMock: +# context = MagicMock(spec=CallbackContext) +# return context + + +# def test_start(fake_update: MagicMock, fake_context: MagicMock) -> None: +# with patch("telegram_bot.chatbot") as mock_chatbot: +# start(fake_update, fake_context) +# fake_update.message.reply_text.assert_called_once_with( +# "Hello! I am your chatbot. Ask me anything!" +# ) +# mock_chatbot.add_message.assert_called_once_with( +# content="Hello! I am your chatbot. Ask me anything!" +# ) + + +# def test_get_response(fake_update: MagicMock, fake_context: MagicMock) -> None: +# with patch("telegram_bot.chatbot") as mock_chatbot: +# mock_chatbot.get_response.return_value = "This is a response" +# get_response(fake_update, fake_context) +# mock_chatbot.get_response.assert_called_once_with(fake_update.message.text) +# fake_update.message.reply_text.assert_called_once_with("This is a response") + + +# def test_help(fake_update: MagicMock, fake_context: MagicMock) -> None: +# help(fake_update, fake_context) +# fake_update.message.reply_text.assert_called_once_with("Help!") + +from unittest.mock import MagicMock, patch + +import pytest +from pytest_mock import MockerFixture +from telegram import Chat, Message, Update, User +from telegram_bot import get_response, help, start + + +@pytest.fixture +def mock_update(mocker: MockerFixture) -> MagicMock: + update = MagicMock(spec=Update) + update.message = MagicMock(spec=Message) + update.message.text = "test" + update.message.chat = MagicMock(spec=Chat) + update.message.chat.id = 123456 + update.message.from_user = MagicMock(spec=User) + update.message.from_user.id = 11111 + update.effective_chat = update.message.chat + update.message.reply_text = MagicMock() + return update + + +@pytest.fixture +def mock_context(mocker: MockerFixture) -> MagicMock: + context = MagicMock() + return context + + +def test_start(mock_update: MagicMock, mock_context: MagicMock) -> None: + start(mock_update, mock_context) + mock_update.message.reply_text.assert_called_with( + "Hello! I am your chatbot. Ask me anything!" + ) + + +def test_help(mock_update: MagicMock, mock_context: MagicMock) -> None: + help(mock_update, mock_context) + mock_update.message.reply_text.assert_called_with("Help!") + + +def test_get_response(mock_update: MagicMock, mock_context: MagicMock) -> None: + with patch("telegram_bot.chatbot.get_response", return_value="response"): + mock_update.message.text = "test" + mock_context.bot = MagicMock() + get_response(mock_update, mock_context) + expected_response = "response" + mock_update.message.reply_text.assert_called_with(expected_response) From 1b0b6fdc20b32ebe44b4b2b46cf56ea2997db030 Mon Sep 17 00:00:00 2001 From: Kirill Pushkarev Date: Sun, 5 Nov 2023 20:35:37 +0200 Subject: [PATCH 5/5] bug: test_telegram_bot comment code --- tests/test_telegram_bot.py | 61 -------------------------------------- 1 file changed, 61 deletions(-) diff --git a/tests/test_telegram_bot.py b/tests/test_telegram_bot.py index 345c032..b5092d3 100644 --- a/tests/test_telegram_bot.py +++ b/tests/test_telegram_bot.py @@ -1,64 +1,3 @@ -# import datetime -# from unittest.mock import MagicMock, patch - -# import pytest -# from telegram import Chat, Message, Update, User, Bot -# from telegram.ext import CallbackContext -# from telegram_bot import get_response, help, start - - -# @pytest.fixture -# def fake_update() -> MagicMock: -# user = User(id=2, first_name="testuser", is_bot=False) -# chat = Chat(id=3, type="private") -# message = Message( -# message_id=1, -# date=datetime.datetime.now(), -# chat=chat, -# text="Test", -# from_user=user, -# bot=MagicMock(spec=Bot) -# ) - -# update = MagicMock(spec=Update) -# update.message = message -# update.update_id = 123 -# update._id_attrs = (update.update_id,) - -# # update.configure_mock(update_id=123, message=message) - -# return update - - -# @pytest.fixture -# def fake_context() -> MagicMock: -# context = MagicMock(spec=CallbackContext) -# return context - - -# def test_start(fake_update: MagicMock, fake_context: MagicMock) -> None: -# with patch("telegram_bot.chatbot") as mock_chatbot: -# start(fake_update, fake_context) -# fake_update.message.reply_text.assert_called_once_with( -# "Hello! I am your chatbot. Ask me anything!" -# ) -# mock_chatbot.add_message.assert_called_once_with( -# content="Hello! I am your chatbot. Ask me anything!" -# ) - - -# def test_get_response(fake_update: MagicMock, fake_context: MagicMock) -> None: -# with patch("telegram_bot.chatbot") as mock_chatbot: -# mock_chatbot.get_response.return_value = "This is a response" -# get_response(fake_update, fake_context) -# mock_chatbot.get_response.assert_called_once_with(fake_update.message.text) -# fake_update.message.reply_text.assert_called_once_with("This is a response") - - -# def test_help(fake_update: MagicMock, fake_context: MagicMock) -> None: -# help(fake_update, fake_context) -# fake_update.message.reply_text.assert_called_once_with("Help!") - from unittest.mock import MagicMock, patch import pytest