0

My app.yaml file is as follows:

runtime: python
env: flex
entrypoint: gunicorn -b :8443 main:app
threadsafe: true

runtime_config:
  python_version: 2

So, when I run this python script in GAE (of course, having deleted the previous webhook), the webhook doesn't get set up. I couldn't figure out what did I do wrong.

import sys
import os
import time
from flask import Flask, request
import telegram

# CONFIG
TOKEN    = '<token>'
HOST     = 'example.appspot.com' # Same FQDN used when generating SSL Cert
PORT     = 8443
CERT     = "certificate.pem"
CERT_KEY = "key.pem"


bot = telegram.Bot(TOKEN)
app = Flask(__name__)


@app.route('/')
def hello():
    return 'Hello World!'


@app.route('/' + TOKEN, methods=['POST','GET'])
def webhook():
    update = telegram.Update.de_json( request.get_json(force = True), bot )
    chat_id = update.message.chat.id
    bot.sendMessage(chat_id = chat_id, text = 'Hello, there')

    return 'OK'


def setwebhook():
    bot.setWebhook(url = "https://%s:%s/%s" % (HOST, PORT, TOKEN), certificate = open(CERT, 'rb'))


if __name__ == '__main__':
    context = (CERT, CERT_KEY)
    setwebhook()
    time.sleep(5)
    app.run(host = '0.0.0.0', port = PORT, ssl_context = context, debug = True)

I thought there might be an issue with SSL certificates, but if I just do this without running the python code, everything works out fine:

curl -F "url=https://example.appspot.com:8443/<token>"  -F "certificate=@certificate.pem" 
      https://api.telegram.org/bot<token>/setWebhook
  • Are you getting any error when executing your python code? If yes, can you post your stack trace? – bharath Mar 20 '18 at 17:25
  • Nope, I don't get any errors. "Hello World" is displayed whenever I run it. When I try to run webhook() I get "Bad Request The browser (or proxy) sent a request that this server could not understand". But I think that's because the webhook to telegram simply is not set up (I checked it with getWebhookInfo in the Telegram Bot API) – Trafalgar Law Mar 20 '18 at 17:44
  • 2
    Possible duplicate of [Can't setup a webhook for a telegram bot](https://stackoverflow.com/questions/49370552/cant-setup-a-webhook-for-a-telegram-bot) – Sean Wei Mar 20 '18 at 22:29

0 Answers0