S
S
Sergey Ganzhela2020-06-16 18:49:29
Django
Sergey Ganzhela, 2020-06-16 18:49:29

How to import django projects files when running a script as a service?

Good day to all.
There is a bot.py script,
it almost doesn’t know how, but that’s not the point. :)
The bot itself works, but I need to import the model files there to push the data into the database, and this doesn’t work, the service doesn’t start?
Here's how to defeat this non-arc
it runs as a service

[Service]
Type=simple
User=root
WorkingDirectory=/home/alx/stp/stp/bot
ExecStart=/home/alx/envs/stp-env/bin/python3 /home/alx/stp/stp/bot.py
RestartSec=10
Restart=always
 
[Install]
WantedBy=multi-user.target

import sys
import telebot
from aiohttp import web
import ssl
import json


WEBHOOK_LISTEN = "0.0.0.0"
WEBHOOK_PORT = 8443

WEBHOOK_SSL_CERT = "/etc/ssl/********.crt"
WEBHOOK_SSL_PRIV = "/etc/ssl/*******.key"

API_TOKEN = '******'
bot = telebot.TeleBot(API_TOKEN)

app = web.Application()


# process only requests with correct bot token
async def handle(request):
    if request.match_info.get("token") == bot.token:
        request_body_dict = await request.json()
        update = telebot.types.Update.de_json(request_body_dict)
        bot.process_new_updates([update])
        return web.Response()
    else:
        return web.Response(status=403)

app.router.add_post("/{token}/", handle)

help_string = ["*Some bot* - just a bot.\n\n", "/start - greetings\n", "/help - shows this help"]

# - - - messages


@bot.message_handler(commands=["start"])
def send_welcome(message):
    f = open('/home/alx/bot/bot.txt', 'w')
    chat_id = message.json['chat']['id']
    user_id = message.json['text'].split(' ')[-1]
    # try:
    #     if StpUser.objects.filter(id=int(user_id)).exists():
    #         user = StpUser.objects.get(id=int(user_id))
    #         if not IdChatBotMessenger.filter.objects(user=user).exists():
    #             data = {"user": user, 'telegram_chat_id': chat['id']}
    #             IdChatBotMessenger.objects.create(**data)
    # except ValueError:
    #     pass
    f.close()
    bot.send_message(message.chat.id, "Вас приветствует бот компании SystemToPeople")


@bot.message_handler(commands=["help"])
def send_help(message):
    bot.send_message(message.chat.id, "".join(help_string), parse_mode="Markdown")

context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.load_cert_chain(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV)

# start aiohttp server (our bot)
web.run_app(
    app,
    host=WEBHOOK_LISTEN,
    port=WEBHOOK_PORT,
    ssl_context=context,
)


bot.service - Telegram bot System To People
   Loaded: loaded (/etc/systemd/system/bot.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Вт 2020-06-16 18:25:43 EEST; 1s ago
  Process: 30165 ExecStart=/home/***/envs/stp-env/bin/python3 /home/**/stp/stp/bot.py (code=exited, status=1/FAILURE)
 Main PID: 30165 (code=exited, status=1/FAILURE)

июн 16 18:25:43 systemtopeople.com systemd[1]: bot.service: Unit entered failed state.
июн 16 18:25:43 systemtopeople.com systemd[1]: bot.service: Failed with result 'exit-code'.

gives an error when starting the service and everything When I remove imports, the service starts, as I understand it, it cannot find the path
and how to tell it where to look for these files

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-06-16
@Sergiy_Hanzhela

Customize the bot as https://docs.djangoproject.com/en/3.0/howto/custom... in "django projects"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question