【问题标题】:How I can run a telegram bot with the Django project?如何使用 Django 项目运行电报机器人?
【发布时间】:2021-03-01 19:55:20
【问题描述】:

现在,要通过 Django 运行机器人,我首先使用 python manage.py runserver 命令,然后按照链接使用我的机器人启动视图。你能告诉我在启动 Django 项目时是否有更简单的方法来自动启动我的机器人?

【问题讨论】:

    标签: django telegram-bot python-telegram-bot


    【解决方案1】:

    实际上,您可以使用管理命令来运行您的机器人,例如

    python manage.py runbot

    所有 Django 上下文,包括数据库和设置,都将可用

    参考管理命令页面:

    https://simpleisbetterthancomplex.com/tutorial/2018/08/27/how-to-create-custom-django-management-commands.html

    【讨论】:

      【解决方案2】:

      也许有点晚了,但您可以执行以下操作:

      在 manage.py 所在的同一文件夹中创建 bot.py 文件。

      确保在 bot.py 中导入以下内容:

      import django
      import os
      os.environ['DJANGO_SETTINGS_MODULE'] = '{Folder where your settings are}.settings'
      django.setup()
      

      为了运行你只需输入 python bot.py

      【讨论】:

        猜你喜欢
        • 2022-11-14
        • 1970-01-01
        • 2017-08-18
        • 1970-01-01
        • 1970-01-01
        • 2015-07-21
        • 1970-01-01
        • 2023-01-20
        • 2016-03-31
        相关资源
        最近更新 更多