-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
93 lines (81 loc) · 3.08 KB
/
Copy pathdata.py
File metadata and controls
93 lines (81 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# data.py
# Работа с хранилищем заказов (Google Sheets опционально, иначе in-memory)
import os
import logging
from datetime import datetime
# In-memory хранилище если Google Sheets недоступен
TICKETS_DB = {} # {user_id: {order_id: {...}, ...}}
REFERRALS_DB = {} # {user_id: [referred_user_ids]}
BONUSES_DB = {} # {user_id: bonus_amount}
try:
from oauth2client.service_account import ServiceAccountCredentials
import gspread
CREDS_FILE = "google-credentials.json"
SCOPE = ["https://spreadsheets.google.com/auth/spreadsheets"]
USE_GSHEET = os.path.exists(CREDS_FILE)
if USE_GSHEET:
try:
creds = ServiceAccountCredentials.from_json_keyfile_name(CREDS_FILE, SCOPE)
client = gspread.authorize(creds)
USE_GSHEET = True
except:
USE_GSHEET = False
except:
USE_GSHEET = False
def get_gsheet():
"""Получить Google Sheet (если доступен)"""
if not USE_GSHEET:
return None
try:
sheet = client.open("BotOrders").sheet1
return sheet
except Exception as e:
logging.error(f"Ошибка подключения к Google Sheets: {e}")
return None
def save_ticket(user_id, order_id, data):
"""Сохранить заказ"""
ticket = {
"order_id": order_id,
"user_id": user_id,
"timestamp": datetime.now().isoformat(),
"status": "новый",
"data": data
}
# Сохранить в памяти
if user_id not in TICKETS_DB:
TICKETS_DB[user_id] = {}
TICKETS_DB[user_id][order_id] = ticket
# Попытаться сохранить в Google Sheets
if USE_GSHEET:
try:
sheet = get_gsheet()
if sheet:
sheet.append_row([
order_id,
user_id,
data.get('fio', ''),
data.get('contact', ''),
"новый",
datetime.now().isoformat()
])
except Exception as e:
logging.warning(f"Не удалось сохранить в Google Sheets: {e}")
def get_ticket_status(user_id, order_id=None):
"""Получить статус заказов пользователя"""
# Ищем в памяти
if user_id in TICKETS_DB:
if order_id:
ticket = TICKETS_DB[user_id].get(order_id, {})
if ticket:
return f"Статус: {ticket.get('status', 'неизвестно')}\nВремя: {ticket.get('timestamp', '')}"
# Вернуть все заказы пользователя
tickets = TICKETS_DB[user_id]
if tickets:
result = "Ваши заказы:\n"
for tid, tdata in tickets.items():
result += f"• {tid}: {tdata.get('status', 'неизвестно')}\n"
return result
return "У вас нет заказов"
def get_all_tickets():
"""Получить все заказы"""
return TICKETS_DB