File size: 1,331 Bytes
d9d21f7
 
 
 
 
 
 
 
9b2bff5
 
2cd7197
d9d21f7
 
9b2bff5
d9d21f7
 
 
 
 
 
 
 
bda88a2
9b2bff5
 
bda88a2
d9d21f7
 
 
 
 
 
 
 
 
 
 
 
 
 
9b2bff5
1
2
3
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
from contextlib import asynccontextmanager
from http import HTTPStatus
from telegram import Update
from telegram.ext import Application, CommandHandler
from telegram.ext._contexttypes import ContextTypes
from fastapi import FastAPI, Request, Response

# Initialize python telegram bot
bot_token = '7163529766:AAGGGlSjVqtcm_b0vJJu7sU9Y7VzSzeYNZg'  # replace <your-bot-token>
ptb = Application(bot_token=bot_token, read_timeout=7, get_updates_read_timeout=42)

@asynccontextmanager
async def lifespan(_: FastAPI):
    await ptb.bot.setWebhook('https://manishx-telegrambot.hf.space')  # replace <your-webhook-url>
    async with ptb:
        await ptb.start()
        yield
        await ptb.stop()

# Initialize FastAPI app (similar to Flask)
app = FastAPI(lifespan=lifespan)

@app.get("/")
def read_general():
    return {"response": "Started"}

@app.post("/")
async def process_update(request: Request):
    print('entrato')
    req = await request.json()
    print(req)
    update = Update.de_json(req, ptb.bot)
    await ptb.process_update(update)
    return Response(status_code=HTTPStatus.OK)

# Example handler
async def start(update, _: ContextTypes.DEFAULT_TYPE):
    """Send a message when the command /start is issued."""
    await update.message.reply_text("starting...")

ptb.add_handler(CommandHandler("start", start))