Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/telegram bot #5

Merged
merged 6 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,4 @@ cython_debug/

# VSCode files
.vscode/
.token
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)