Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/telegram bot #40

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ GOOGLE_SECRET='<client-secret>'
# EMAIL
[email protected]
EMAIL_HOST_PASSWORD='<host-password>'

# TELEGRAM BOT
BOOKING_TELEGRAM_API_TOKEN='<api-token>'
BOOKING_TELEGRAM_CHAT_ID='<chat-id>'
2 changes: 2 additions & 0 deletions vr_club/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 22 additions & 2 deletions vr_club_site/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import calendar
import requests
import os
import decimal
from datetime import datetime, date

Expand All @@ -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:
Expand Down Expand Up @@ -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}")
Loading