I’m in the process of creating a Telegram bot that successfully checks commands and sends replies. However, it does not initiate the game when the start button is clicked; instead, it gets stuck displaying ‘Fetching updates…’ and does not proceed to process further messages or callbacks. Below is a condensed version of my Python implementation:
import requests
import time
import json
BOT_TOKEN = 'TOKEN'
GAME_ENDPOINT = 'https://www.yyyyyyy.info/index.html'
API_URL = f'https://api.telegram.org/bot{BOT_TOKEN}'
last_update_id = None # Initializing last_update_id
def send_message(chat_id, message, button=None):
api_url = f'{API_URL}/sendMessage'
payload = {'chat_id': chat_id, 'text': message}
if button:
payload['reply_markup'] = json.dumps(button)
requests.post(api_url, json=payload)
def respond_callback(callback_id, endpoint):
response_link = f'{endpoint}?game_url={GAME_ENDPOINT}'
req = requests.get(f'{API_URL}/answerCallbackQuery', params={'callback_query_id': callback_id, 'url': response_link})
print(f'Response to Callback: {req.text}')
def initiate_game(chat_id):
game_notification = 'Game is launching! Hang tight...'
send_message(chat_id, game_notification)
while True:
try:
print('Fetching updates...')
response_data = requests.get(f'{API_URL}/getUpdates', params={'offset': last_update_id}).json()
if response_data.get('ok'):
messages = response_data['result']
print(f'Received messages: {messages}') # Debug log for messages received
for message in messages:
print(f'Processing message: {message}')
if 'message' in message:
chat_id = message['message']['chat']['id']
content = message['message'].get('text', '')
button_layout = {
'inline_keyboard': [[{'text': 'Start game', 'callback_data': 'init_game'}]]
}
if content == '/start' or content.startswith('/'):
print(f'Sending greeting to chat {chat_id}')
send_message(chat_id, 'Greetings! Tap the button to start the game.', button_layout)
else:
print(f'Unknown command to chat {chat_id}')
send_message(chat_id, 'Command not recognized. Tap the button to start the game.', button_layout)
elif 'callback_query' in message:
callback_id = message['callback_query']['id']
if message['callback_query']['data'] == 'init_game':
print(f'Handling callback {callback_id} to initiate game')
initiate_game(message['callback_query']['message']['chat']['id'])
respond_callback(callback_id, GAME_ENDPOINT)
# Update last_update_id after processing each callback_query
last_update_id = message['callback_query']['id'] + 1
# Update last_update_id after processing each message
last_update_id = message['update_id'] + 1
else:
print(f'Error in fetching updates: {response_data}')
except Exception as e:
print(f'An error occurred: {e}')
time.sleep(1) # Pause before next retrieval
The problem appears to relate to the last_update_id
not updating correctly, leading to the bot fetching the same updates repeatedly. What changes should I make to my code to resolve this issue and allow the game to start when the ‘Start game’ button is clicked?
Feel free to make any necessary adjustments. Best of luck with your bot development!