Посылаем данные статьи в телеграм-канал #8
This commit is contained in:
parent
fa575c845a
commit
27d5c9e411
16
cms/views.py
16
cms/views.py
@ -11,8 +11,24 @@ from cms.models import Article
|
|||||||
|
|
||||||
@method_decorator(csrf_exempt, name='dispatch')
|
@method_decorator(csrf_exempt, name='dispatch')
|
||||||
class ArticleView(View):
|
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):
|
def post(self, request):
|
||||||
article_data = JSONDecoder().decode(request.body.decode())
|
article_data = JSONDecoder().decode(request.body.decode())
|
||||||
article = Article.objects.create(**article_data)
|
article = Article.objects.create(**article_data)
|
||||||
|
self._send_to_channel(article)
|
||||||
response = {'ok': True}
|
response = {'ok': True}
|
||||||
return JsonResponse(response)
|
return JsonResponse(response)
|
Binary file not shown.
Binary file not shown.
@ -11,9 +11,13 @@ https://docs.djangoproject.com/en/4.1/ref/settings/
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from os import path
|
||||||
|
import dotenv
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
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
|
# Quick-start development settings - unsuitable for production
|
||||||
@ -37,6 +41,7 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
'cms'
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user