我在开发一个电报机器人,从我的个人电脑(Mac)上运行 bot在python上运行,在一个特定的环境中,该环境中安装了模块
现在bot已经好了,我想把它放到一个运行Apache的web服务器上。但我有一些问题。在
1)我必须将服务器上的每个模块都安装给每个人,或者我可以为这个特定的bot创建一个环境,然后apache在该环境中运行该bot?在
2)我使用的是getUpdates(使我的机器非常慢,但更适合调试错误),现在我想使用webhook运行。如果webhook而不是getUpdates转到telegram服务器,需要做什么改变才能使他工作?今天,启动并继续运行的代码是:
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()
您可以使用以下步骤设置webhook:
在telegram bot对象上设置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")
将长轮询替换为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托管bot时,这对我很有用。干杯!在
相关问题 更多 >
编程相关推荐