diff --git a/cms/views.py b/cms/views.py index dc03680..71b7c3d 100644 --- a/cms/views.py +++ b/cms/views.py @@ -11,8 +11,24 @@ from cms.models import Article @method_decorator(csrf_exempt, name='dispatch') class ArticleView(View): + def _send_to_channel(self, article: Article): + bot_token = os.getenv('TELEGRAM_BOT_TOKEN') + channel_id = os.getenv('TELEGRAM_CHAT_ID') + + long_text = f'{article.title}\n{article.body}' + + send_message_url = f'https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={channel_id}&text={long_text}' + + response = requests.get(send_message_url) + result = response.json() + if result['ok']: + print('Мы послали сообщение, ура!') + else: + print('Похоже, нас послали доделывать приложение :-(') + def post(self, request): article_data = JSONDecoder().decode(request.body.decode()) article = Article.objects.create(**article_data) + self._send_to_channel(article) response = {'ok': True} return JsonResponse(response) \ No newline at end of file diff --git a/crossposting_backend/__pycache__/settings.cpython-311.pyc b/crossposting_backend/__pycache__/settings.cpython-311.pyc deleted file mode 100644 index 1d7bc38..0000000 Binary files a/crossposting_backend/__pycache__/settings.cpython-311.pyc and /dev/null differ diff --git a/crossposting_backend/__pycache__/urls.cpython-311.pyc b/crossposting_backend/__pycache__/urls.cpython-311.pyc deleted file mode 100644 index 40faf63..0000000 Binary files a/crossposting_backend/__pycache__/urls.cpython-311.pyc and /dev/null differ diff --git a/crossposting_backend/settings.py b/crossposting_backend/settings.py index a215258..ec01258 100644 --- a/crossposting_backend/settings.py +++ b/crossposting_backend/settings.py @@ -11,9 +11,13 @@ https://docs.djangoproject.com/en/4.1/ref/settings/ """ from pathlib import Path +from os import path +import dotenv # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent +env_file = path.join(BASE_DIR, '.env') +dotenv.read_dotenv(env_file) # Quick-start development settings - unsuitable for production @@ -37,6 +41,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'cms' ] MIDDLEWARE = [