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 g_telegram_bot_api_token def send_telegram_message(request, chat_id): if request.method == 'POST': message = request.POST.get('message') bot_token = g_telegram_bot_api_token url = f'https://api.telegram.org/bot{bot_token}/sendMessage?text={message}&chat_id={chat_id}' response = requests.get(url) 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')