-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.py
346 lines (251 loc) · 16.9 KB
/
app.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# libraries for bot
import telebot
from telebot import types
import sqlite3
import math
import sys
from io import BytesIO
import requests
from PIL import Image
# python files functions
from geocoder_coords import coords_to_address, addess_to_coords
from static_map_passengers import create_static_map_order # get static map geopos for choose order
# TOKEN for bot
token = "1892369905:AAErBKLVAT95cNV7oLTzrTmxjhEM4fvEMNE"
bot = telebot.TeleBot(token)
@bot.message_handler(commands=["start"])
def start(message):
name = message.text
bot.send_message(message.chat.id, "Привет <b>{first_name}</b>, рад тебя видеть. Пожалуйста, отправьте мне свой номер для этого есть команда /phone".format(first_name=message.from_user.first_name), parse_mode='HTML', reply_markup=types.ReplyKeyboardRemove())
@bot.message_handler(commands=["phone"])
def phone(message):
user_markup = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True, one_time_keyboard=True)
button_phone = types.KeyboardButton(text="Отправить номер телефона", request_contact=True)
user_markup.add(button_phone)
msg = bot.send_message(message.chat.id, "Согласны ли вы предоставить ваш номер телефона для регистрации в системе?", reply_markup=user_markup)
bot.register_next_step_handler(msg, reg_or_auth)
def reg_or_auth(message):
try:
# user phone
input_phone = message.contact.phone_number
# connect to base
mydb = sqlite3.connect('base.db')
mycursor = mydb.cursor()
# find phone in passengers table
mycursor.execute('SELECT * FROM passengers')
passengers = mycursor.fetchall()
for user in passengers:
table_phone = user[1]
if table_phone == input_phone: # if user_phone find in passengers table
# keyboard for auth passenger
buttons_actions = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
button_history_ways = types.KeyboardButton(text="Мои поездки")
button_add_order = types.KeyboardButton(text="Новая поездка")
buttons_actions.add(button_history_ways)
buttons_actions.add(button_add_order)
mess = bot.send_message(message.chat.id, "Выберите действие.", reply_markup=buttons_actions)
bot.register_next_step_handler(mess, choose_action_passenger, input_phone, message.chat.id)
return '' # stop function
mycursor.execute('SELECT * FROM taxi_drivers')
drivers = mycursor.fetchall()
print(drivers)
for user in drivers:
table_phone = user[1]
if table_phone == input_phone: # if user_phone find in taxi_drivers table
# keyboard for auth taxi driver
buttons_actions = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
button_settings = types.KeyboardButton(text="Настройки")
button_choose_order = types.KeyboardButton(text="Выбрать поездку")
buttons_actions.add(button_settings)
buttons_actions.add(button_choose_order)
mess = bot.send_message(message.chat.id, "Выберите действие.", reply_markup=buttons_actions)
bot.register_next_step_handler(mess, choose_action_taxi_driver, input_phone, message.chat.id)
return '' # stop function
# if table is empty
buttons_characters = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
button_taxi_driver = types.KeyboardButton(text="Таксист")
button_passenger = types.KeyboardButton(text="Пассажир")
buttons_characters.add(button_taxi_driver)
buttons_characters.add(button_passenger)
mess = bot.send_message(message.chat.id, "Выберите кем вы являетесь?", reply_markup=buttons_characters)
bot.register_next_step_handler(mess, choose_character, input_phone)
except:
mess = bot.send_message(message.chat.id, "Ошибка. Отправьте номер телефона!")
bot.register_next_step_handler(mess, reg_or_auth)
@bot.message_handler(content_types=['text'])
def choose_action_passenger(message, user_phone, teg_id): # auth passenger action
if message.text == 'Мои поездки':
# connect to base
mydb = sqlite3.connect('base.db')
mycursor = mydb.cursor()
# find orders for history orders to passenger
mycursor.execute('SELECT * FROM orders')
orders = mycursor.fetchall()
for order in orders:
if order[1] == user_phone:
first_checkpoint = coords_to_address(order[2], order[3]) # start address
second_checkpoint = coords_to_address(order[4], order[5]) # end address
bot.send_message(message.chat.id, f"<i><b>Заказ №{order[0]}.</b></i>\n\n<i><b>Начальная точка:</b></i> {first_checkpoint}\n\n<i><b>Конечная точка:</b></i> {second_checkpoint}\n\n<i><b>Расстояние:</b></i> {order[7]} м\n\n<i><b>Время пути:</b></i> {order[8]} мин\n\n<b>Цена:</b> {order[6]} ₽", parse_mode='HTML', reply_markup=types.ReplyKeyboardRemove())
elif message.text == 'Новая поездка':
# geolocation new order
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
button_loca = types.KeyboardButton(text="🌐 Определить местоположение", request_location=True)
keyboard.add(button_loca)
mess = bot.send_message(message.chat.id, "Отправьте вашу геолокацию.🌐", reply_markup=keyboard)
bot.register_next_step_handler(mess, geo_location, user_phone, 'Пассажир')
@bot.message_handler(content_types=['text'])
def choose_action_taxi_driver(message, user_phone, teg_id): # auth taxi driver action
if message.text == 'Выбрать поездку':
# connect to base
mydb = sqlite3.connect('base.db')
mycursor = mydb.cursor()
print(user_phone)
# choose order for taxi driver
mycursor.execute(f'SELECT * FROM taxi_drivers')
taxi_drivers = mycursor.fetchall()
for taxi_driver in taxi_drivers:
if taxi_driver[1] == user_phone:
print(taxi_driver)
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
button_loca = types.KeyboardButton(text="🌐 Определить местоположение", request_location=True)
keyboard.add(button_loca)
mess = bot.send_message(message.chat.id, "Отправьте вашу геолокацию.🌐", reply_markup=keyboard)
bot.register_next_step_handler(mess, geo_location, user_phone, 'Таксист', firm=taxi_driver[2], car_numbers=taxi_driver[3], src_photo_car=taxi_driver[-2])
break
@bot.message_handler(content_types=['text'])
def choose_character(message, user_phone): # choose taxi_drivers or passenger
if message.text == 'Таксист':
mess = bot.send_message(message.chat.id, "Введите марку машины.", reply_markup=types.ReplyKeyboardRemove())
bot.register_next_step_handler(mess, machine_firm, user_phone)
elif message.text == 'Пассажир':
# connect to base
mydb = sqlite3.connect('base.db')
mycursor = mydb.cursor()
# Add new passenger in 'passengers' table
sqlFormula = "INSERT INTO passengers ('phone', 'teg_id') VALUES (?,?)"
mycursor.execute(sqlFormula, (user_phone, message.chat.id))
mydb.commit()
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
button_loca = types.KeyboardButton(text="🌐 Определить местоположение", request_location=True)
keyboard.add(button_loca)
mess = bot.send_message(message.chat.id, "Отправьте вашу геолокацию.🌐", reply_markup=keyboard)
bot.register_next_step_handler(mess, geo_location, user_phone, 'Пассажир')
@bot.message_handler(content_types=['text']) # machine_firm
def machine_firm(message, phone):
firm = message.text
mess = bot.send_message(message.chat.id, "Введите номера машины.", reply_markup=types.ReplyKeyboardRemove())
bot.register_next_step_handler(mess, car_numbers, phone, firm)
@bot.message_handler(content_types=['text']) # car_numbers
def car_numbers(message, phone, machine_firm):
car_numbers = message.text
mess = bot.send_message(message.chat.id, "Отправьте фото машины.")
bot.register_next_step_handler(mess, handle_docs_photo, car_numbers, phone, machine_firm)
@bot.message_handler(content_types=['photo']) # function for get photo car from user
def handle_docs_photo(message, car_numbers, phone, machine_firm):
try:
chat_id = message.chat.id
file_info = bot.get_file(message.photo[0].file_id)
downloaded_file = bot.download_file(file_info.file_path)
src = 'photo_cars/' + car_numbers + '.png'; # save png photo name - car_numbers
with open(src, 'wb') as new_file:
new_file.write(downloaded_file)
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
button_loca = types.KeyboardButton(text="🌐 Определить местоположение", request_location=True)
keyboard.add(button_loca)
mess = bot.send_message(message.chat.id, "Отправьте вашу геолокацию.🌐", reply_markup=keyboard)
bot.register_next_step_handler(mess, geo_location, phone, 'Таксист', firm=machine_firm, car_numbers=car_numbers, src_photo_car=src)
except:
pass
@bot.message_handler(content_types=['text'])
def geo_location(message, phone, job, firm=None, car_numbers=None, src_photo_car=None): # firm and car_numbers if taxi, default passenger
latitude = message.location.latitude
longitude = message.location.longitude
dict_length = {}
address_location = coords_to_address(longitude, latitude) # get address from coords, function file geocoder.py
bot.send_message(message.chat.id, address_location, reply_markup=types.ReplyKeyboardRemove())
mydb = sqlite3.connect('base.db')
mycursor = mydb.cursor()
if job == 'Таксист':
c = 0
mycursor.execute(f'SELECT * FROM taxi_drivers')
taxi_drivers = mycursor.fetchall()
for driver in taxi_drivers:
if driver[1] != phone:
c += 1
if c == len(taxi_drivers):
sqlFormula = "INSERT INTO taxi_drivers ('phone', 'machine_firm', 'car_numbers', 'longitude', 'latitude', 'photo_car', 'teg_id') VALUES (?,?,?,?,?,?,?)"
mycursor.execute(sqlFormula, (phone, firm, car_numbers, longitude, latitude, src_photo_car, message.chat.id))
mydb.commit()
mydb = sqlite3.connect('base.db')
mycursor = mydb.cursor()
mycursor.execute(f'SELECT * FROM orders')
orders = mycursor.fetchall()
for us in orders:
user = us
first_checkpoint = coords_to_address(user[2], user[3]) # start address
second_checkpoint = coords_to_address(user[4], user[5]) # end address
bot.send_message(message.chat.id, f"<i><b>Заказ №{user[0]}.</b></i>\n\n<i><b>Начальная точка:</b></i> {first_checkpoint}\n\n<i><b>Конечная точка:</b></i> {second_checkpoint}\n\n<i><b>Расстояние:</b></i> {user[7]} м\n\n<i><b>Время пути:</b></i> {user[8]} мин\n\n<b>Цена:</b> {user[6]} ₽", parse_mode='HTML', reply_markup=types.ReplyKeyboardRemove())
mess = bot.send_message(message.chat.id, "Введите номер заказа.", parse_mode='HTML', reply_markup=types.ReplyKeyboardRemove())
bot.register_next_step_handler(mess, choose_order)
elif job == 'Пассажир':
mess = bot.send_message(message.chat.id, "<b>Куда едем?</b>", parse_mode='HTML', reply_markup=types.ReplyKeyboardRemove())
bot.register_next_step_handler(mess, where_go, phone, longitude, latitude)
@bot.message_handler(content_types=['text'])
def choose_order(message): # num order
num_order = message.text
mydb = sqlite3.connect('base.db')
mycursor = mydb.cursor()
mycursor.execute(f'SELECT * FROM orders')
users = mycursor.fetchall()
passenger = []
for us in users: # find order in table by id
if us[0] == int(num_order):
passenger.append(us)
first_checkpoint = coords_to_address(passenger[0][2], passenger[0][3]) # start address
second_checkpoint = coords_to_address(passenger[0][4], passenger[0][5]) # end address
bot.send_message(message.chat.id, f"<i><b>Номер пассажира: {passenger[0][1]}.</b></i>\n\n<i><b>Начальная точка:</b></i> {first_checkpoint}\n\n<i><b>Конечная точка:</b></i> {second_checkpoint}\n\n<i><b>Расстояние:</b></i> {passenger[0][7]} м\n\n<i><b>Время пути:</b></i> {passenger[0][8]} мин\n\n<b>Цена:</b> {passenger[0][6]} ₽", parse_mode='HTML', reply_markup=types.ReplyKeyboardRemove())
# create map_point.png for choose order
create_static_map_order(f'{passenger[0][2]},{passenger[0][3]}')
# send map_point.png to taxi driver
bot.send_photo(message.chat.id, open('map_point.png', 'rb'));
# send photo taxi_car to passenger
mydb = sqlite3.connect('base.db')
mycursor = mydb.cursor()
mycursor.execute(f'SELECT * FROM taxi_drivers WHERE teg_id={message.chat.id}')
user_taxi = mycursor.fetchall()
src_photo_car = user_taxi[0][6] # src_photo_car
bot.send_photo(passenger[0][-1], open(src_photo_car, 'rb')); # passenger[0][-1] - teg_id user
sql = 'DELETE FROM orders WHERE id=?'
mycursor.execute(sql, (int(num_order),))
mydb.commit()
@bot.message_handler(content_types=['text'])
def where_go(message, phone, longitude_start, latitude_start): # end address for passenger
address_go = message.text
longitude_end, latitude_end = [float(x) for x in addess_to_coords(address_go).split(' ')]
mess = bot.send_message(message.chat.id, "<b>Укажите желаемую цену в ₽.</b>", parse_mode='HTML', reply_markup=types.ReplyKeyboardRemove())
bot.register_next_step_handler(mess, price_way, phone, longitude_start, latitude_start, longitude_end, latitude_end)
@bot.message_handler(content_types=['text'])
def price_way(message, phone, longitude_start, latitude_start, longitude_end, latitude_end): # end address for passenger
price_way = int(message.text)
# length of way
x1, y1 = longitude_start, latitude_start
x2, y2 = longitude_end, latitude_end
y = math.radians((y1 + y2) / 2)
x = math.cos(y)
n = abs(x1 - x2) * 111000 * x
n2 = abs(y1 - y2) * 111000
length_way = round(math.sqrt(n * n + n2 * n2))
#---------------
# time way
time_way = round(length_way / (40 * 1000) * 60)
#--------
first_checkpoint = coords_to_address(longitude_start, latitude_start)
second_checkpoint = coords_to_address(longitude_end, latitude_end)
bot.send_message(message.chat.id, f"<i><b>Ваш заказ.</b></i>\n\n<i><b>Начальная точка:</b></i> {first_checkpoint}\n\n<i><b>Конечная точка:</b></i> {second_checkpoint}\n\n<i><b>Расстояние:</b></i> {length_way} м\n\n<i><b>Время пути:</b></i> {time_way} мин\n\n<b>Цена:</b> {price_way} ₽", parse_mode='HTML', reply_markup=types.ReplyKeyboardRemove())
mydb = sqlite3.connect('base.db')
mycursor = mydb.cursor()
sqlFormula = "INSERT INTO orders ('phone', 'longitude_start', 'latitude_start', 'longitude_end', 'latitude_end', 'price', 'length_way', 'time_way', 'teg_id') VALUES (?,?,?,?,?,?,?,?,?)"
mycursor.execute(sqlFormula, (phone, longitude_start, latitude_start, longitude_end, latitude_end, price_way, length_way, time_way, message.chat.id))
mydb.commit()
if __name__ == '__main__':
bot.polling(none_stop=True, interval=0)