Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-push committed Nov 5, 2023
2 parents 3cb7a3b + f12fe53 commit 341581c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/chatbot/telegram_bot.py
Original file line number Diff line number Diff line change
@@ -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()
47 changes: 47 additions & 0 deletions tests/test_telegram_bot.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 341581c

Please sign in to comment.