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

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +31 -45
main.py CHANGED
@@ -1,49 +1,35 @@
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))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
 
 
 
 
 
2
 
3
+ # Replace 'YOUR_BOT_TOKEN' with your actual bot token
4
+ bot_token = '6990801595:AAE79xNVO1D_0SeWZlzYLE57Suwfp9GyKT8'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ def send_message(chat_id, text):
7
+ url = f'https://api.telegram.org/bot{bot_token}/sendMessage'
8
+ params = {'chat_id': chat_id, 'text': text}
9
+ response = requests.get(url, params=params)
10
+ return response.json()
11
 
12
+ def handle_message(message):
13
+ chat_id = message['chat']['id']
14
+ text = message['text']
15
 
16
+ if text == '/start':
17
+ send_message(chat_id, 'Hi')
18
+
19
+ def main():
20
+ offset = None
21
+
22
+ while True:
23
+ url = f'https://api.telegram.org/bot{bot_token}/getUpdates'
24
+ params = {'offset': offset}
25
+ response = requests.get(url, params=params)
26
+ data = response.json()
27
+
28
+ if data['ok']:
29
+ for update in data['result']:
30
+ offset = update['update_id'] + 1
31
+ if 'message' in update:
32
+ handle_message(update['message'])
33
+
34
+ if __name__ == '__main__':
35
+ main()