from django.contrib import messages from django.http import HttpResponseRedirect import requests from django.urls import reverse from django.shortcuts import render from bot_sys.config import GetTelegramBotApiToken def send_telegram_message(request, chat_id): if request.method == 'POST': message = request.POST.get('message') bot_token = GetTelegramBotApiToken() url = f'https://api.telegram.org/bot{bot_token}/sendMessage' payload = { 'chat_id': chat_id, 'text': message, 'parse_mode': 'markdown' } response = requests.post(url, json=payload) if response.status_code == 200: messages.success(request, "Сообщение успешно отправлено") back_url = reverse('') return HttpResponseRedirect(back_url) else: messages.error(request, "Сообщение не отправлено") back_url = reverse('') return HttpResponseRedirect(back_url) else: return render(request, 'send_telegram_message.html')