简体   繁体   中英

python-telegram-bot using webhook

I was developing a telegram bot, running from my personal machine (Mac) The bot is running on python, in a specific environment with modules installed on that environment

Now that the bot is fine, I'd like to put it on a web server, running Apache. But I have some questions.

1) I have to install each module at the server to everybody or I can create an environment for this specific bot and apache runs that bot on that environment?

2) I was using getUpdates (makes my machine very slow, but is better to debug errors) and now I want to run using webhook. What a have to change exactly to make him work if webhook instead of getUpdates going to telegram server? Today the code to start and keep running is that:

def main():

updater = Updater(bot_token)
dp = updater.dispatcher

# Commands
dp.add_handler(CommandHandler("info", ranking_putaria))
dp.add_handler(CommandHandler("start", start))

# Start checking updates
dp.add_handler(MessageHandler(Filters.text,echo_msg))
dp.add_handler(MessageHandler(Filters.video | Filters.photo | Filters.document, echo_file))
dp.add_handler(MessageHandler(Filters.sticker, echo_sticker))

# Log errors
#dp.add_error_handler(error)

# start the bot
updater.start_polling()

# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()


if __name__ == '__main__':
main()

You can set up webhook with the following steps:

  1. Set up webhook on your telegram bot object:

    import os PORT = int(os.environ.get('PORT', '5000')) bot = telegram.Bot(token = "YOUR TOKEN HERE") bot.setWebhook("YOUR WEB SERVER LINK HERE" + "YOUR TOKEN HERE")

  2. Replace your long polling with webhook - ie replace updater.start_polling() with

    updater.start_webhook(listen="0.0.0.0", port=PORT, url_path="YOUR TOKEN HERE") updater.bot.setWebhook("YOUR WEB SERVER LINK HERE" + "YOUR TOKEN HERE") updater.idle()

This worked for me when I hosted my bot using Heroku. Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM