Pythonanywhere Telegram bot POST function not working

Hi im brand new on Pythonanywhere and i have choosen it to migrate a bot im deploying with Apps Script, after reading many old tutorials from 2018 this is the piece of code that i came up with:

from flask import Flask, request
import telepot
import urllib3

proxy_url = "http://proxy.server:3128"
telepot.api._pools = {
 'default': urllib3.ProxyManager(proxy_url=proxy_url, num_pools=3, maxsize=10, retries=False, 
timeout=30),
}
telepot.api._onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=proxy_url, num_pools=1, 
maxsize=1, retries=False, timeout=30))

secret = "1234"
bot = telepot.Bot('token')
bot.setWebhook("https://logrt.pythonanywhere.com/{}".format(secret), max_connections=1)

app = Flask(__name__)

@app.route('/{}'.format(secret), methods=["POST"])
def telegram_webhook():
    update = request.get_json()
    if "message" in update:
        chat_id = update["message"]["chat"]["id"]
        if "text" in update["message"]:
            text = update["message"]["text"]
            bot.sendMessage(chat_id, "From the web: you said '{}'".format(text))
        else:
            bot.sendMessage(chat_id, "From the web: sorry, I didn't understand that kind of 
message")
    return "OK"

i checked and am sure the webhook is set and even able to send messages to the bot but any attempt to make the post function work is failing, some newb questions:

thanks for your answer

Upvotes: 1

Views: 714

Answers (1)

caseneuve
caseneuve

Reputation: 476

The answer for the main question is: yes, currently you need a paid account to use the code from the tutorial, see:

For some reason Telegram has stopped accepting wildcard HTTPS certificates for bots, so unfortunately this tutorial will no longer work on a PythonAnywhere free account. For the webhooks part of the tutorial to work, you will have to get a paying account with a custom domain. Thanks to fivechar for letting us know in the comments.

(Update 2020-01-12 in the blog post)

As for the errors -- check the error log for the web app.

Upvotes: 1

Related Questions