diff --git a/.gitignore b/.gitignore index 6f10937..ad899f3 100644 --- a/.gitignore +++ b/.gitignore @@ -131,3 +131,4 @@ cython_debug/ # VSCode files .vscode/ +.token diff --git a/src/chatbot/telegram_bot.py b/src/chatbot/telegram_bot.py new file mode 100644 index 0000000..8da3d65 --- /dev/null +++ b/src/chatbot/telegram_bot.py @@ -0,0 +1,48 @@ +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!") + + +if __name__ == "__main__": + 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() diff --git a/tests/test_telegram_bot.py b/tests/test_telegram_bot.py new file mode 100644 index 0000000..b5092d3 --- /dev/null +++ b/tests/test_telegram_bot.py @@ -0,0 +1,47 @@ +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)