import telebot
import requests

with open('cokk.txt', 'r', encoding='utf-8') as file:
    info = file.read()

# Replace 'YOUR_TOKEN' with your actual bot token
bot = telebot.TeleBot('6990801595:AAE79xNVO1D_0SeWZlzYLE57Suwfp9GyKT8')

def get_assistant_response(user_input):
    payload = {
        "mode": "chat",
        "chat_history": conversation_history,
        "data": {
            "query": f"{info}\n \n \n \n CHAT START HERE : \n \n Stuident : {user_input} \n C Learner :",
            "loader": "PDFReader",
            "text": ""
        }
    }

    response = requests.post(url2, headers=headers, json=payload)
    data = response.json()

    # Extract the response from the data
    response_text = data["data"]["response"]

    return response_text

url2 = "https://api.braininc.net/be/vectordb/indexers/"
headers = {
    "Authorization": "token 72ec00483379076f580eb8126f29da802a5140c3",
    "Content-Type": "application/json"
}

conversation_history = []

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
    global conversation_history
    conversation_history=[]

    bot.reply_to(message, "Hello there")

@bot.message_handler(func=lambda message: True)
def echo_all(message):
    chat_id = message.chat.id
    user_input = message.text
    conversation_history.append({
        "role": "user",
        "content": f"Stuident :{user_input}",
        "additional_kwargs": {}
    })

    if user_input.lower() == "exit":
        bot.reply_to(message, "Goodbye!")
        return

    response_text = get_assistant_response(user_input)
    conversation_history.append({
        "role": "assistant",
        "content": f"C Learner :{response_text}",
        "additional_kwargs": {}
    })
    bot.reply_to(message, response_text)
    print(conversation_history)

# Specify the host parameter when calling the polling method
bot.polling(dp, skip_updates=True)