Dooratre's picture
Update app.py
d444343 verified
import telebot
import requests
from telebot import apihelper
import socket
import os
# Set up the proxy if required
apihelper.proxy = {'http': 'http://api.telegram.org:443'}
# Set up environment variables for the proxy
os.environ['http_proxy'] = 'http://api.telegram.org:443'
os.environ['https_proxy'] = 'http://api.telegram.org:443'
# Function to set custom DNS
def set_custom_dns():
dns_servers = ['8.8.8.8', '8.8.4.4']
for dns in dns_servers:
socket.setdefaulttimeout(5)
try:
socket.gethostbyname(dns)
socket.setdefaulttimeout(None)
return
except socket.error:
continue
set_custom_dns()
# Read the bot token from a file
with open('cokk.txt', 'r', encoding='utf-8') as file:
info = file.read()
# Replace 'YOUR_TOKEN' with your actual bot token
bot = telebot.TeleBot('YOUR_TOKEN')
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()
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)
bot.polling()