Telegram bot sends constant replies instead of running the given code (utilizing webhook)

I am currently attempting to set up my Telegram bot for a multiplayer game using Google App Engine with webhooks. My database structure includes the following:

class MultiplayerGame(ndb.Model):
    chat_identifier = ndb.IntegerProperty(required=True)
    mission_count = ndb.IntegerProperty(default=1)
    round_count = ndb.IntegerProperty(default=1)

class GameParticipant(ndb.Model):
    id_number = ndb.IntegerProperty(required=True)
    role = ndb.StringProperty(
        choices=['spy', 'resistance']
    )

In the webhook handling segment, I have the following code:

if user_input.startswith('/'): 
    if user_input == '/begin':
        send_response('Bot activated')
        updateStatus(chat_identifier, True)
    elif user_input == '/end':
        send_response('Bot deactivated')
        updateStatus(chat_identifier, False)
    elif user_input == '/startgame':
        if chat_category == 'group':
            current_game = MultiplayerGame.query(MultiplayerGame.chat_identifier == chat_identifier).get()
            if current_game:
                send_response('A game is already in progress.')
            else:
                # Code to initiate a new game goes here
                # I am currently unsure about this part
                #==============================#
                #send_response('This line repeats continuously')
                #==============================#
                new_multiplayer_game = MultiplayerGame(
                    chat_identifier=chat_identifier,
                    id=chat_identifier
                )
                game_key = new_multiplayer_game.put()

                player_entry = GameParticipant(
                    parent=game_key,
                    id_number=from_user_id,
                    id=from_user_id
                )
                player_entry.put()
                send_response('Awaiting more players to join!')
        else:
            send_response('Please start the game in a group chat! Invite more friends!')
    else:
        send_response('Unrecognized command.')
else:
    if isActive(chat_identifier):
        send_response('I received your message! But I am unsure how to respond.')
    else:
        logging.info('Not active for chat_identifier {}'.format(chat_identifier))

The issue I encounter is that sending the ‘/startgame’ command in a group does not yield any reply. If I uncomment the following line, the bot continuously sends “This line repeats continuously” as a response:

#send_response('This line repeats continuously')

The send_response function is defined as:

def send_response(message=None, image=None):
    if message:
        response = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
            'chat_id': str(chat_identifier),
            'text': message.encode('utf-8'),
            'disable_web_page_preview': 'true',
            'reply_to_message_id': str(message_id),
        })).read()
    elif image:
        response = multipart.post_multipart(BASE_URL + 'sendPhoto', [
            ('chat_id', str(chat_identifier)),
            ('reply_to_message_id', str(message_id)),
        ], [
            ('photo', 'image.jpg', image),
        ])
    else:
        logging.error('No message or image given')
        response = None

    logging.info('Sent response:')
    logging.info(response)

I’d also like to mention that I received an error with this message:

Internal Server Error
The server encountered an issue or was unable to perform the requested action.

The traceback indicated that the error stems from line 66 in the following handler code:

class UpdatesHandler(webapp2.RequestHandler):
    def get(self):
        urlfetch.set_default_fetch_deadline(60)
        self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getUpdates'))))

As a beginner, I would greatly appreciate any suggestions or guidance!

One potential issue may be that your bot is not handling updates correctly, causing unexpected loops or unhandled exceptions. To prevent this, you might want to ensure that you properly acknowledge each update received from Telegram. For instance, in the UpdatesHandler, confirm that you’re fetching updates only once and marking them as processed by setting an offset in getUpdates. It’s also beneficial to include error handling within your webhook to catch common server errors, which could aid in identifying why the server returns an internal error. Consider adding checks before creating new game entries and ensure all database transactions are correctly handled to avoid simultaneous writes which might cause confusion in your bot logic.