|
|
|
|
@ -1,15 +1,15 @@
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from django.core.handlers.wsgi import WSGIRequest
|
|
|
|
|
from django.http import JsonResponse
|
|
|
|
|
from django.http import JsonResponse, HttpResponse
|
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
|
|
|
|
from max_bot.max_api import maxbot_send_text_message
|
|
|
|
|
from max_bot.models import Partner, Integration, Client, BonusTransaction, App
|
|
|
|
|
from max_bot.models import Partner, Integration, Client, BonusTransaction, App, PromoCode
|
|
|
|
|
from restoran_max_bot import settings
|
|
|
|
|
from restoran_max_bot.bot_started import bot_started
|
|
|
|
|
from restoran_max_bot.bot_stopped import bot_stopped
|
|
|
|
|
from restoran_max_bot.bot_message import bot_message, bot_promo
|
|
|
|
|
from restoran_max_bot.common import common_get_contact
|
|
|
|
|
from restoran_max_bot.common import common_get_contact, common_check_registration
|
|
|
|
|
from restoran_max_bot.settings import DEBUG
|
|
|
|
|
from restoran_max_bot.utils import is_json, has_key
|
|
|
|
|
|
|
|
|
|
@ -72,13 +72,6 @@ def api_start_max_v1(request, token, **kwargs):
|
|
|
|
|
rt = {'success': False, 'error': 'data update_type not found', 'data': ''}
|
|
|
|
|
return JsonResponse(rt, status=200)
|
|
|
|
|
|
|
|
|
|
# обработчик промо кодов с запросов
|
|
|
|
|
# https://dev.max.ru/docs/chatbots/bots-coding/prepare
|
|
|
|
|
# https://max.ru/id032606883607_2_bot?start=source_site
|
|
|
|
|
if data.get("payload", None):
|
|
|
|
|
print(data['payload'])
|
|
|
|
|
bot_promo(client=client, message=data, settings_max=settings_max)
|
|
|
|
|
|
|
|
|
|
# получение сообщения из бота
|
|
|
|
|
if data['update_type'] == 'message_created':
|
|
|
|
|
bot_message(client=client, message=data, settings_max=settings_max, message_type='message_created')
|
|
|
|
|
@ -88,6 +81,12 @@ def api_start_max_v1(request, token, **kwargs):
|
|
|
|
|
|
|
|
|
|
# start - запуск бота
|
|
|
|
|
if data['update_type'] == 'bot_started':
|
|
|
|
|
# https://dev.max.ru/docs/chatbots/bots-coding/prepare
|
|
|
|
|
# https://max.ru/id032606883607_2_bot?start=source_site
|
|
|
|
|
# роверка на промокод
|
|
|
|
|
if data.get("payload", None):
|
|
|
|
|
bot_promo(client=client, message=data, settings_max=settings_max)
|
|
|
|
|
if not common_check_registration(client=client, chat_id=data['chat_id']):
|
|
|
|
|
bot_started(client=client, message=data, settings=settings_max)
|
|
|
|
|
|
|
|
|
|
# выход из бота
|
|
|
|
|
@ -136,3 +135,88 @@ def iiko_send_message(request: WSGIRequest, token):
|
|
|
|
|
maxbot_send_text_message(chat_id=contact.maxx, max_token=integration.token, message=text)
|
|
|
|
|
rt = {'success': True, 'data': ''}
|
|
|
|
|
return JsonResponse(rt, status=200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def promo_status(request, promo_code, chat_id):
|
|
|
|
|
try:
|
|
|
|
|
promo = PromoCode.objects.get(promo=promo_code, chat_id=chat_id)
|
|
|
|
|
except PromoCode.DoesNotExist:
|
|
|
|
|
return HttpResponse("Промокод не найден для данного чата", status=404)
|
|
|
|
|
|
|
|
|
|
# Если промокод уже использован, просто показываем статус
|
|
|
|
|
if promo.used:
|
|
|
|
|
html = f"""
|
|
|
|
|
<html>
|
|
|
|
|
<body>
|
|
|
|
|
<h1>Промокод: {promo.promo}</h1>
|
|
|
|
|
<p>ID чата: {promo.chat_id}</p>
|
|
|
|
|
<p>Статус: <strong>Использован</strong></p>
|
|
|
|
|
<p>Время использования: {promo.used_at}</p>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
return HttpResponse(html)
|
|
|
|
|
|
|
|
|
|
# Если не использован, показываем кнопку с запросом PIN-кода
|
|
|
|
|
html = f"""
|
|
|
|
|
<html>
|
|
|
|
|
<body>
|
|
|
|
|
<h1>Промокод: {promo.promo}</h1>
|
|
|
|
|
<p>ID чата: {promo.chat_id}</p>
|
|
|
|
|
<p>Статус: <strong style="color:green;">Не использован</strong></p>
|
|
|
|
|
<button onclick="activate()">Активировать</button>
|
|
|
|
|
<script>
|
|
|
|
|
function activate() {{
|
|
|
|
|
var pin = prompt("Введите PIN-код администратора:");
|
|
|
|
|
if (pin) {{
|
|
|
|
|
fetch('/promo/activate/{promo_code}/{chat_id}', {{
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {{'Content-Type': 'application/json'}},
|
|
|
|
|
body: JSON.stringify({{pin: pin}})
|
|
|
|
|
}})
|
|
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(data => {{
|
|
|
|
|
alert(data.message);
|
|
|
|
|
if (data.success) location.reload();
|
|
|
|
|
}});
|
|
|
|
|
}}
|
|
|
|
|
}}
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
"""
|
|
|
|
|
return HttpResponse(html)
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
|
def promo_activate(request, promo_code, chat_id):
|
|
|
|
|
if request.method != 'POST':
|
|
|
|
|
return JsonResponse({'error': 'Method not allowed'}, status=405)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
data = json.loads(request.body)
|
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
|
return JsonResponse({'error': 'Invalid JSON'}, status=400)
|
|
|
|
|
|
|
|
|
|
pin = data.get('pin')
|
|
|
|
|
if pin != settings.PROMO_ADMIN_PIN:
|
|
|
|
|
return JsonResponse({'error': 'Неверный PIN-код'}, status=403)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
promo = PromoCode.objects.get(promo=promo_code, chat_id=chat_id, used=False)
|
|
|
|
|
except PromoCode.DoesNotExist:
|
|
|
|
|
return JsonResponse({'error': 'Промокод не найден или уже использован'}, status=404)
|
|
|
|
|
|
|
|
|
|
promo.used = True
|
|
|
|
|
promo.used_at = datetime.now()
|
|
|
|
|
promo.save()
|
|
|
|
|
|
|
|
|
|
# Отправляем уведомление клиенту
|
|
|
|
|
integration = Integration.objects.filter(client=promo.client, partner__slug='max_bot', status=True).first()
|
|
|
|
|
if integration:
|
|
|
|
|
maxbot_send_text_message(
|
|
|
|
|
chat_id=promo.chat_id,
|
|
|
|
|
max_token=integration.token,
|
|
|
|
|
message="🎉 Ваш подарок активирован! Спасибо, что посетили нас."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return JsonResponse({'success': True, 'message': 'Промокод активирован, клиент получил уведомление'})
|