C
C
Carmacat2022-01-13 00:13:06
Python
Carmacat, 2022-01-13 00:13:06

How to set up webhook in Telegram API for Flask bot on pythonanywhere?

Greetings! I wrote my bot in python using the pytelegrambotapi library.
The bot uses webhooks to get updates from the server and up to this point I have been using ngrok to get notifications from the telegram server to my local machine.

Now, when it became necessary to keep the bot on the server to access it 24/7, I ran into a problem - I literally don’t know how to deploy my bot on pythonanywhere and I can’t find a solution to my problem on the Internet.

All the guides on the Internet that I come across are not give explanations on how to set up a webhook on pythonanywhere and what address it should be registered to.

I would be grateful if someone already has a solution to this issue that can be reused.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2022-01-13
@SoreMix

bot = telebot.TeleBot(token, skip_pending=True, threaded=False)

secret = '' #набор символов какой нибудь

username = '' # имя пользователя

bot.remove_webhook()
time.sleep(1)
bot.set_webhook(url='https://{}.pythonanywhere.com/{}'.format(username, secret))

app = Flask(__name__)

@app.route('/{}'.format(secret), methods=["POST"])
def webhook():
    bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
    return 'ok', 200

Further the usual code, without bot.polling, of course

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question