Manishx commited on
Commit
0e1ae88
1 Parent(s): 588b27c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +28 -16
main.py CHANGED
@@ -1,15 +1,14 @@
 
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('7163529766:AAGGGlSjVqtcm_b0vJJu7sU9Y7VzSzeYNZg') # replace <your-bot-token>
13
  .read_timeout(7)
14
  .get_updates_read_timeout(42)
15
  .build()
@@ -17,7 +16,7 @@ ptb = (
17
 
18
  @asynccontextmanager
19
  async def lifespan(_: FastAPI):
20
- await ptb.bot.setWebhook('https://manishx-telegrambot.hf.space/') # replace <your-webhook-url>
21
  async with ptb:
22
  await ptb.start()
23
  yield
@@ -27,23 +26,36 @@ async def lifespan(_: FastAPI):
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))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, Response
2
  from contextlib import asynccontextmanager
3
  from http import HTTPStatus
4
  from telegram import Update
5
+ from telegram.ext import Application, CommandHandler, ContextTypes
 
 
6
 
7
  # Initialize python telegram bot
8
  ptb = (
9
  Application.builder()
10
  .updater(None)
11
+ .token('7163529766:AAGGGlSjVqtcm_b0vJJu7sU9Y7VzSzeYNZg')
12
  .read_timeout(7)
13
  .get_updates_read_timeout(42)
14
  .build()
 
16
 
17
  @asynccontextmanager
18
  async def lifespan(_: FastAPI):
19
+ await ptb.bot.setWebhook('https://manishx-telegrambot.hf.space/')
20
  async with ptb:
21
  await ptb.start()
22
  yield
 
26
  app = FastAPI(lifespan=lifespan)
27
 
28
  @app.get("/")
29
+ def read_general():
30
+ return {"response": "Started"}
31
 
32
  @app.post("/")
33
  async def process_update(request: Request):
 
34
  req = await request.json()
 
35
  update = Update.de_json(req, ptb.bot)
36
  await ptb.process_update(update)
37
  return Response(status_code=HTTPStatus.OK)
38
 
39
+ # Define a few command handlers. These usually take the two arguments update and context.
40
+ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
 
 
41
  """Send a message when the command /start is issued."""
42
+ user = update.effective_user
43
+ await update.message.reply_html(
44
+ rf"Hi {user.mention_html()}!",
45
+ reply_markup=ForceReply(selective=True),
46
+ )
47
+
48
+ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
49
+ """Send a message when the command /help is issued."""
50
+ await update.message.reply_text("Help!")
51
+
52
+ async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
53
+ """Echo the user message."""
54
+ await update.message.reply_text(update.message.text)
55
+
56
+ # on different commands - answer in Telegram
57
+ ptb.add_handler(CommandHandler("start", start))
58
+ ptb.add_handler(CommandHandler("help", help_command))
59
+
60
+ # on non command i.e message - echo the message on Telegram
61
+ ptb.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))