My Python Telegram bot’s response time is unexpectedly slow (6–8 seconds). What may cause this delay and how can I enhance it? Can webhooks help? See alternate code below:
import requests
import time
API_KEY = 'ABCXYZ:1234567890'
BASE = f'https://api.telegram.org/bot{API_KEY}'
next_id = 0
while True:
time.sleep(0.1)
reply = requests.get(f'{BASE}/getupdates?offset={next_id}').json()
if reply.get('result'):
text_msg = reply['result'][0]['message']['text']
print(text_msg)
if text_msg == 'Hello':
requests.get(f'{BASE}/sendmessage?chat_id=987654321&text=Hello there')
next_id = reply['result'][0]['update_id'] + 1