使用webhook的python-telegram-bot

时间:2017-09-30 19:20:28

标签: python python-telegram-bot

我正在开发一个从我的个人计算机(Mac)运行的电报机器人 僵尸程序在python上运行,在特定环境中,在该环境中安装了模块

现在机器人很好,我想将它放在运行Apache的Web服务器上。但我有一些问题。

1)我必须在服务器上为每个人安装每个模块,或者我可以为这个特定的机器人创建一个环境,并且apache在该环境中运行该机器人?

2)我正在使用getUpdates(使我的机器非常慢,但更好地调试错误)现在我想使用webhook运行。如果webhook而不是getUpdates去电报服务器,那么必须改变才能使他工作?今天,启动和继续运行的代码是:

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()

1 个答案:

答案 0 :(得分:0)

您可以使用以下步骤设置webhook:

  1. 在电报机器人对象上设置webhook:

    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. 用webhook替换长轮询 - 即用

    替换updater.start_polling()

    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()

  3. 当我使用Heroku托管我的机器人时,这对我有用。干杯!

相关问题