-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
108 lines (87 loc) · 3.68 KB
/
main.py
File metadata and controls
108 lines (87 loc) · 3.68 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from datetime import datetime
import os
import asyncio
import logging
from aiogram import Bot, Dispatcher, types, F
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.filters import CommandStart
from aiogram.types import Message
from keyboards import mark_type_button, location_button
from db import db_set, db_update, db_update_batch
from utils import (load_and_refresh_credentials, update_token)
SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
BOT_TOKEN = os.getenv("BOT_TOKEN")
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher(storage=MemoryStorage())
user_sessions = {}
background_tasks = set()
creds = load_and_refresh_credentials(scopes=SCOPES)
@dp.message(CommandStart())
async def command_start_handler(message: Message) -> None:
username = message.from_user.username
user_sessions[username] = {}
await message.answer("Выбери тип метки",
reply_markup=mark_type_button())
@dp.callback_query(F.data == "type:fire")
async def handle_fire(callback: types.CallbackQuery):
username = callback.from_user.username
session = user_sessions[username]
session["type"] = "fire"
await callback.message.answer("Пришли локацию возгорания", reply_markup=location_button())
await callback.answer()
@dp.callback_query(F.data == "type:me")
async def handle_fire(callback: types.CallbackQuery):
username = callback.from_user.username
session = user_sessions[username]
session['type'] = "me"
await callback.message.answer("Пришли свою локацию", reply_markup=location_button())
await callback.answer()
@dp.message(F.location.is_not(None))
async def handle_location(message: Message):
username = message.from_user.username
location = message.location
session = user_sessions[username]
inputs = {'ts': datetime.now().isoformat(),
'username': username,
'latitude': location.latitude,
'longitude': location.longitude,
'horizontal_accuracy': location.horizontal_accuracy,
'source': 'tg_location',
'event_type': session["type"]
}
if 'updates' not in session:
session['inputs'] = inputs
fire_id_data = db_set(**inputs)
session['updates'] = fire_id_data['updates']
else:
session['inputs'] = inputs
range_to_update = session['updates']['updatedRange']
row_to_update = range_to_update.split('!')[1].split(':')[0][1]
db_update_batch('A', row_to_update, 'H', **inputs)
if session["type"] == 'me':
await message.answer("Записано, вам будут приходить пинги")
task = asyncio.create_task(pinger(message))
background_tasks.add(task)
task.add_done_callback(background_tasks.discard)
else:
await message.answer("Опишите возгорание")
@dp.message(F.text.is_not(None))
async def handle_text(message: Message):
username = message.from_user.username
text = message.text
session = user_sessions[username]
if session['type'] == 'fire':
range_to_update = user_sessions[username]['updates']['updatedRange']
row_to_update = range_to_update.split('!')[1].split(':')[0][1]
to_update = user_sessions[username]['inputs']
to_update['description'] = text
db_update('H', row_to_update, text)
await message.answer('Записано')
async def pinger(message: Message):
await asyncio.sleep(2)
await message.answer('Обнови свою локацию', reply_markup=location_button())
if __name__ == "__main__":
if not os.path.exists('token.json'):
update_token()
logging.basicConfig(level=logging.INFO)
asyncio.run(dp.start_polling(bot))