-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
92 lines (67 loc) · 1.96 KB
/
Copy pathbot.py
File metadata and controls
92 lines (67 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import logging
import os
import time
import dotenv
import telebot
from main import Chatbot
def format_for_telegram(text: str) -> str:
return (
text.replace("*", "\*")
.replace("~", "\~")
.replace("`", "\`")
.replace(">", "\>")
.replace("#", "\#")
.replace("|", "\|")
.replace("{", "\{")
.replace("}", "\}")
)
logging.basicConfig(
format='[%(threadName)s] %(levelname)s: %(message)s"', level=logging.INFO
)
dotenv.load_dotenv("env")
time.sleep(15)
bot = telebot.TeleBot(
token=f"{os.environ.get('BOT_TOKEN')}",
parse_mode="MARKDOWN",
threaded=True,
)
c = Chatbot(
f"{os.environ.get('CHATBOT_MODEL')}",
f"{os.environ.get('API_LINK')}",
f"{os.environ.get('TOKEN')}",
["data/orientation.md"],
)
c.build_database()
@bot.message_handler(func=lambda message: message.text in ("/start", "/help", "Help"))
def welcome(message: telebot.types.Message) -> None:
"""
Handle the user's first message to the bot, sending a greeting and instructions on how to use the bot.
:param message: The message object received from the user.
:return: None
"""
# Replying with welcome message
bot.reply_to(
message=message,
text="Hi there! My name is CyberBoris and I am a kinda smart bot, which can answer questions about Skoltech.",
)
@bot.message_handler(content_types=["text"])
def send_question(message: telebot.types.Message) -> None:
"""
Sends the question to the backend to be answered.
:param message: The message object received from the user.
:return: None
"""
question = message.text
logging.info(
f"User @{message.from_user.username} with chat id {message.chat.id} sent a question: {question}"
)
bot.reply_to(
message=message,
text=format_for_telegram(c.question(question)),
)
def start_bot() -> None:
"""
Starts the bot.
:return: None
"""
bot.infinity_polling()