C
C
chochocho2021-04-10 15:24:04
Python
chochocho, 2021-04-10 15:24:04

How to make webhook in telegram bot and put it on hosting?

I am using Telebot library. It's time to put the bot on the host (I want to use Pythonanywhere). Now the bot works on bot.polling(), I want to transfer it to Webhook, but after looking at the documentation I didn’t understand anything exactly how to do it.
Tell me how to make a webhook and how to upload it to the hosting?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-04-10
@chochocho

In the secret of some kind of line, so that no one needs to poke once again.
First you need to remove the hook, suddenly it already exists, then re-attach. username - name on pythonanywhere
Then we hang the hook.

secret = '43d2yIyIVM'
username = 'user'

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

app = Flask(__name__)

Then the flask handler is hung up, updates come to it and are transferred to telebot
@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

The rest is the same, except that bot.pollin() is naturally not

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question