Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -1,71 +1,49 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import
|
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 |
-
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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|