(Python3) (telebot) Как выполнить функцию при правильном запросе?
Хочу добавить функцию def time(): print(time.ctime()) Которая будет выполняться если пользователь введет «time», «время»
Пытался добавлять if в def bot(): но выводилась ошибка
import json
import random
import time
import telebot
import logging
from sklearn.metrics import classification_report
from nltk import edit_distance
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from telegram import Update, ForceReply
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
TOKEN = "52182--60:AAG7hZKR-FuMr---QawpviFmm-fOTvwEGUig"
# RANDOM_STATE = 42
# with open("BOT_CONFIG.json", "r") as file:
# BOT_CONFIG = json.load(file)
# BOT_CONFIG["intents"]["hello"]
# [
# ["сегодня","хорошоя","погода","сегодня"],
# ["хорошая","погода","была","погода"],
# ["пасмурно","питер","погода","дождь"]
# ]
# X = []
# y = []
# count = 0
# for intent in BOT_CONFIG["intents"].keys():
# try:
# for example in BOT_CONFIG["intents"][intent]["examples"]:
# X.append(example)
# y.append(intent)
# except KeyError:
# print(BOT_CONFIG["intents"][intent])
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state = RANDOM_STATE)
# len(X_train), len(X_test)
# len(y_train), len(y_test)
# vectorizer = CountVectorizer(ngram_range=(1, 5), analyzer="char")
# X_train_vectorized = vectorizer.fit_transform(X_train)
# X_test_vectorized = vectorizer.transform(X_test)
# model = RandomForestClassifier(random_state = RANDOM_STATE, n_estimators = 200)
# model.fit(X_train_vectorized, y_train)
# model.score(X_train_vectorized, y_train)
# model.score(X_test_vectorized, y_test)
# model.predict(vectorizer.transform(["как погода?"]))
# def get_intent(input_text):
# return model.predict(vectorizer.transform([input_text]))[0]
# def bot(input_text):
# intent = get_intent(input_text)
# return random.choice(BOT_CONFIG["intents"][intent]["responses"])
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
def start(update: Update, context: CallbackContext) -> None:
user = update.effective_user
update.message.reply_markdown_v2(
fr'Hi {user.mention_markdown_v2()}\!',
reply_markup=ForceReply(selective=True),
)
# def bot_tl(update: Update, context: CallbackContext) -> None:
# update.message.reply_text(bot(update.message.text))
def main() -> None:
"""Start the bot."""
updater = Updater(TOKEN)
dispatcher = updater.dispatcher
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command))#, bot_tl
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()