|
|
@ -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) |