【问题标题】:python-telegram-bot using webhookpython-telegram-bot 使用 webhook
【发布时间】:2018-03-12 09:19:04
【问题描述】:

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

既然机器人很好,我想把它放在一个运行 Apache 的网络服务器上。但我有一些问题。

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

【问题讨论】:

    标签: python python-telegram-bot


    【解决方案1】:

    您可以通过以下步骤设置 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()

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

    【讨论】:

      猜你喜欢
      • 2020-09-22
      • 1970-01-01
      • 2017-01-11
      • 2017-08-03
      • 2016-04-10
      • 2016-08-27
      • 2017-07-22
      • 2016-07-15
      • 1970-01-01
      相关资源
      最近更新 更多