Running Telegram bot running as a background worker results in errors

Hi Render team,

I am trying to run a telegram bot in Docker container which runs polling using python-telegram-bot library. Here is a sample code:

def main():
    logging.info("Starting X Bot")
    application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
    application.add_handler(CommandHandler("start", start))

    application.run_polling()

if __name__ == '__main__':
    main()

Dockerfile:

FROM python:3.11.1-slim

# Install pipenv
RUN pip install pipenv

# Set working directory
WORKDIR /app

# Copy files into container
COPY bot.py .
COPY Pipfile .
COPY Pipfile.lock .

# Install dependencies with pipenv
RUN pipenv install --deploy

# Run bot.py with pipenv
CMD ["pipenv", "run", "python", "bot.py"]

The bot runs perfectly fine in a container locally, but once ran on render.com there are constant errors from the telegram python library:

telegram.error.Conflict: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running

There have been concerns raised regarding running telegram bots as web services on Render based on my research and I am doing that in as a background worker as suggested, but still getting errors.
I am aware, that Telegram does not allow you to run two instances of the bot using the same API. I made sure the API key used in the background worker is only used by that worker by reissuing the key and updating this in the settings.
I am 99% confident that only place that API key is used is from inside render.com.

I have a feeling that Render has multiple containers running when doing a deployment to support zero time deployment, which makes starting new container impossible due to telegram API limitations key usage.

Is there a way to limit Render to run only one instance of a container at time to avoid the issues described above?

Hey,

Dug a little bit here and couldn’t really find anything conclusive. Could you contact us via the “Contact Support” form in the dashboard (bottom of the page) and provide the content of your Dockerfile? We’ll conduct a more thorough investigation there.

Regards.
Jérémy, Render Support

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.