NULL on error flipping bits whilst updating pixels

Hosting Telegram bots on Cloud Run for free

I write a lot of Telegram bots using the library python-telegram-bot. Writing Telegram bots is fun, but you will also need someplace to host them.

I personally like the new Google Cloud Run; or run, for short, is perfect because it has a “gorgeous” free quota that should be mostly sufficient to host your bots, also, and is it super simple to deploy and get running.

To create Telegram bots, first, you need to talk to BotFather and get a TOKEN.

Secondly, you need some coding. As I mentioned before, you can use python-telegram-bot to do your bots. Here is the documentation.


Here is the base code that you will need to run on Cloud Run.

import os
import http

from flask import Flask, request
from werkzeug.wrappers import Response

from telegram import Bot, Update
from telegram.ext import Dispatcher, Filters, MessageHandler, CallbackContext

app = Flask(__name__)

def echo(update: Update, context: CallbackContext) -> None:

bot = Bot(token=os.environ["TOKEN"])

dispatcher = Dispatcher(bot=bot, update_queue=None, workers=0)
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))

@app.route("/", methods=["POST"])
def index() -> Response:
        Update.de_json(request.get_json(force=True), bot))

    return "", http.HTTPStatus.NO_CONTENT




FROM python:3.8-slim
COPY *.txt .
RUN pip install --no-cache-dir --upgrade pip -r requirements.txt
COPY . ./

CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app


Finally, you need to deploy. You can do it in a single step, but first, let’s run the command below to set the default region (optionally).

gcloud config set run/region us-central1

Then deploy to Cloud Run:

gcloud beta run deploy your-bot-name \
    --source . \
    --set-env-vars TOKEN=your-telegram-bot-token \
    --platform managed \
    --allow-unauthenticated \
    --project your-project-name

After this, you will receive a public URL of your run, and you will need to set the Telegram bot webHook using cURL

curl ""

You should replace the YOUR-BOT:TOKEN by the bot’s token and the public URL of your Cloud Run.

This should be enough.