From adf164a56e1d2156403de9c447c1ad1e4915d485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=BE=20=D0=A0=D1=94=D0=B7?= =?UTF-8?q?=D1=94=D0=BD=D0=BA=D0=BE=D0=B2?= <108422398+RezenkovD@users.noreply.github.com> Date: Wed, 16 Aug 2023 13:11:47 +0300 Subject: [PATCH] Feat/telegram bot * feat: update example.env * feat: add bot for send mess about booking * feat: update yml file --- .github/workflows/ci_cd.yml | 6 ++++++ example.env | 4 ++++ vr_club/settings.py | 2 ++ vr_club_site/utils.py | 24 ++++++++++++++++++++++-- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml index d69a77d..4db64f5 100644 --- a/.github/workflows/ci_cd.yml +++ b/.github/workflows/ci_cd.yml @@ -87,6 +87,8 @@ jobs: GOOGLE_SECRET: ${{ secrets.GOOGLE_SECRET }} CSRF_TRUSTED_ORIGINS: ${{ secrets.CSRF_TRUSTED_ORIGINS }} USE_X_FORWARDED_HOST: ${{ secrets.USE_X_FORWARDED_HOST }} + BOOKING_TELEGRAM_API_TOKEN: ${{ secrets.BOOKING_TELEGRAM_API_TOKEN }} + BOOKING_TELEGRAM_CHAT_ID: ${{ secrets.BOOKING_TELEGRAM_CHAT_ID }} - name: Upload coverage report uses: actions/upload-artifact@v2 @@ -174,6 +176,8 @@ jobs: export SUB_DOMAIN='${{ secrets.SUB_DOMAIN }}' export CSRF_TRUSTED_ORIGINS=${{ secrets.CSRF_TRUSTED_ORIGINS }} export USE_X_FORWARDED_HOST='${{ secrets.USE_X_FORWARDED_HOST }}' + export BOOKING_TELEGRAM_API_TOKEN='${{ secrets.BOOKING_TELEGRAM_API_TOKEN }}' + export BOOKING_TELEGRAM_CHAT_ID='${{ secrets.BOOKING_TELEGRAM_CHAT_ID }}' git pull pip3 install -r requirements.txt @@ -209,6 +213,8 @@ jobs: export SUB_DOMAIN='${{ secrets.SUB_DOMAIN }}' export CSRF_TRUSTED_ORIGINS=${{ secrets.CSRF_TRUSTED_ORIGINS }} export USE_X_FORWARDED_HOST='${{ secrets.USE_X_FORWARDED_HOST }}' + export BOOKING_TELEGRAM_API_TOKEN='${{ secrets.BOOKING_TELEGRAM_API_TOKEN }}' + export BOOKING_TELEGRAM_CHAT_ID='${{ secrets.BOOKING_TELEGRAM_CHAT_ID }}' sudo apt-get install -y libpq-dev python3-dev sudo apt-get install -y build-essential diff --git a/example.env b/example.env index 6739d2e..23ae8c3 100644 --- a/example.env +++ b/example.env @@ -17,3 +17,7 @@ GOOGLE_SECRET='' # EMAIL EMAIL_HOST_USER=host@example.com EMAIL_HOST_PASSWORD='' + +# TELEGRAM BOT +BOOKING_TELEGRAM_API_TOKEN='' +BOOKING_TELEGRAM_CHAT_ID='' \ No newline at end of file diff --git a/vr_club/settings.py b/vr_club/settings.py index 2e49c3d..66822d1 100644 --- a/vr_club/settings.py +++ b/vr_club/settings.py @@ -39,6 +39,8 @@ SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") USE_X_FORWARDED_HOST = env("USE_X_FORWARDED_HOST") # Application definition +BOOKING_BOT_TOKEN = env("BOOKING_TELEGRAM_API_TOKEN") +BOOKING_CHAT_ID = env("BOOKING_TELEGRAM_CHAT_ID") INSTALLED_APPS = [ # "whitenoise.runserver_nostatic", diff --git a/vr_club_site/utils.py b/vr_club_site/utils.py index 8311d23..0e57a29 100644 --- a/vr_club_site/utils.py +++ b/vr_club_site/utils.py @@ -1,4 +1,6 @@ import calendar +import requests +import os import decimal from datetime import datetime, date @@ -8,12 +10,19 @@ from django.contrib import messages from .models import Booking, BookingTime, ACTUAL, Settings +from vr_club.settings import BOOKING_BOT_TOKEN, BOOKING_CHAT_ID - -COST_SESSION = 200 MAX_SESSION = 2 +def send_order_details(api_token, chat_id, order_details): + url = f"https://api.telegram.org/bot{api_token}/sendMessage" + message = f"Нове замовлення:\n\n{order_details}" + payload = {"chat_id": chat_id, "text": message} + response = requests.post(url, data=payload) + return response + + def get_price(date): day_of_week = date.weekday() if day_of_week >= 5: @@ -191,5 +200,16 @@ def book_session(request, slots, data_to_save): for booking_time in booking_times_to_create: booking.time.add(booking_time) + + order_details = f"Ім'я: {booking.name}\n" + order_details += f"Email: {booking.email}\n" + order_details += f"Телефон: {booking.phone_number}\n" + order_details += f"Дата: {data_to_save['date']}\n" + order_details += f"Слоти: {slots}\n" + order_details += f"Кількість людей: {booking.people_count}\n" + order_details += f"Ціна: {booking.price} грн\n" + order_details += f"Коментар: {booking.comment}\n" + + send_order_details(BOOKING_BOT_TOKEN, BOOKING_CHAT_ID, order_details) except IntegrityError as e: messages.error(request, f"Помилка бронювання: {e}")