Dooratre commited on
Commit
36c1258
·
verified ·
1 Parent(s): a5d5284

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +49 -71
main.py CHANGED
@@ -1,71 +1,49 @@
1
- import telebot
2
-
3
- import requests
4
-
5
- with open('cokk.txt', 'r', encoding='utf-8') as file:
6
- info = file.read()
7
-
8
-
9
- # Replace 'YOUR_TOKEN' with your actual bot token
10
- bot = telebot.TeleBot('6990801595:AAE79xNVO1D_0SeWZlzYLE57Suwfp9GyKT8')
11
-
12
- def get_assistant_response(user_input):
13
- payload = {
14
- "mode": "chat",
15
- "chat_history": conversation_history,
16
- "data": {
17
- "query": f"{info}\n \n \n \n CHAT START HERE : \n \n Stuident : {user_input} \n C Learner :",
18
- "loader": "PDFReader",
19
- "text": ""
20
- }
21
- }
22
-
23
- response = requests.post(url2, headers=headers, json=payload)
24
- data = response.json()
25
-
26
- # Extract the response from the data
27
- response_text = data["data"]["response"]
28
-
29
- return response_text
30
-
31
-
32
-
33
- url2 = "https://api.braininc.net/be/vectordb/indexers/"
34
- headers = {
35
- "Authorization": "token 72ec00483379076f580eb8126f29da802a5140c3",
36
- "Content-Type": "application/json"
37
- }
38
-
39
- conversation_history = []
40
-
41
- @bot.message_handler(commands=['start', 'help'])
42
- def send_welcome(message):
43
- global conversation_history
44
- conversation_history=[]
45
-
46
- bot.reply_to(message, "Hello there!")
47
-
48
- @bot.message_handler(func=lambda message: True)
49
- def echo_all(message):
50
- chat_id = message.chat.id
51
- user_input = message.text
52
- conversation_history.append({
53
- "role": "user",
54
- "content": f"Stuident :{user_input}",
55
- "additional_kwargs": {}
56
- })
57
-
58
- if user_input.lower() == "exit":
59
- bot.reply_to(message, "Goodbye!")
60
- return
61
-
62
- response_text = get_assistant_response(user_input)
63
- conversation_history.append({
64
- "role": "assistant",
65
- "content": f"C Learner :{response_text}",
66
- "additional_kwargs": {}
67
- })
68
- bot.reply_to(message, response_text)
69
- print(conversation_history)
70
-
71
- bot.polling()
 
1
+ from contextlib import asynccontextmanager
2
+ from http import HTTPStatus
3
+ from telegram import Update
4
+ from telegram.ext import Application, CommandHandler
5
+ from telegram.ext._contexttypes import ContextTypes
6
+ from fastapi import FastAPI, Request, Response
7
+
8
+ # Initialize python telegram bot
9
+ ptb = (
10
+ Application.builder()
11
+ .updater(None)
12
+ .token('6990801595:AAE79xNVO1D_0SeWZlzYLE57Suwfp9GyKT8') # replace <your-bot-token>
13
+ .read_timeout(7)
14
+ .get_updates_read_timeout(42)
15
+ .build()
16
+ )
17
+
18
+ @asynccontextmanager
19
+ async def lifespan(_: FastAPI):
20
+ await ptb.bot.setWebhook('https://dooratre-telegrambottt.hf.space/') # replace <your-webhook-url>
21
+ async with ptb:
22
+ await ptb.start()
23
+ yield
24
+ await ptb.stop()
25
+
26
+ # Initialize FastAPI app (similar to Flask)
27
+ app = FastAPI(lifespan=lifespan)
28
+
29
+ @app.get("/")
30
+ def read_general():
31
+ return {"response": "Started"}
32
+
33
+ @app.post("/")
34
+ async def process_update(request: Request):
35
+ print('entrato')
36
+ req = await request.json()
37
+ print(req)
38
+ update = Update.de_json(req, ptb.bot)
39
+ await ptb.process_update(update)
40
+ return Response(status_code=HTTPStatus.OK)
41
+
42
+
43
+
44
+ # Example handler
45
+ async def start(update, _: ContextTypes.DEFAULT_TYPE):
46
+ """Send a message when the command /start is issued."""
47
+ await update.message.reply_text("starting...")
48
+
49
+ ptb.add_handler(CommandHandler("start", start))