-
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.
Merge branch 'main' of https://github.com/kirill-push/chatbot
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 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
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() |
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,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) |