-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
439 lines (343 loc) · 15.2 KB
/
main.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import logging
from os import getenv
from dotenv import load_dotenv
from telegram import Update
from telegram import InputFile
from telegram import KeyboardButton
from telegram import ReplyKeyboardMarkup
from telegram import ReplyKeyboardRemove
from telegram import InlineKeyboardButton
from telegram import InlineKeyboardMarkup
from telegram.ext import Updater
from telegram.ext import Filters
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler
from telegram.ext import CallbackContext
from telegram.ext import CallbackQueryHandler
from telegram.ext import ConversationHandler
# Take environment variables from .env.
load_dotenv()
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
# States for ConversationHandler
ASK, CAR, VIN, DETAIL, LOCATION, CONTACT = range(6)
# Links for channels
CHANNELS = {
'lviv': '@lvivavtopro', #
'kyiv': '@kyivavtopro', #
'sumy': '@sumyavtopro', #
'volyn': '@volynavtopro', #
'odesa': '@odessaavtopro', #
'ivano-frankivsk': '@ivanofrankivskavtopro', #
'ternopil': '@ternopilavtopro', #
'khmelnitsky': '@khmelnitskyavtopro', #
'zakarpattya': '@zakarpattiaavtopro', #
'chernivetskya': '@chernivtsiavtopro', #
'vinnytsia': '@vinnytsiaavtopro', #
'zhytomyr': '@zhytomyravtopro', #
'dnipro': '@dniproavtopro', #
'zaporozhye': '@zaporozhyeavtopro', #
'kropyvnytskyi': '@kropyvnytskyiavtopro', #
'mykolaiv': '@nikolaevavtopro', #
'poltava': '@poltavaavtopro', #
'kharkiv': '@kharkivavtopro', #
'kherson': '@khersonavtopro', #
'cherkasy': '@cherkasyavtopro', #
'chernigiv': '@chernihivavtopro', #
'rivne': '@rivneavtopro', #
'lugansk': '@luganskavtopro', #
'donezk': '@donezkavtopto', #
}
def start(update: Update, context: CallbackContext) -> int:
"""Starts the conversation and asks the user about their gender."""
user = update.message.from_user
# Create the keyboard
reply_keyboard = [
[ KeyboardButton('Додати запит на деталь ⚙️') ],
[ KeyboardButton('Наявні запити') ],
]
# Send message
update.message.reply_text(
text="""Натискай "Додати запит на деталь ⚙", щоб у п’ять кроків знайти необхідне у своєму регіоні.""",
reply_markup=ReplyKeyboardMarkup(
reply_keyboard,
one_time_keyboard=True,
input_field_placeholder='Тисни',
resize_keyboard=True
)
)
# Logger
logger.info("%s: send /start command;", user.first_name)
def helper(update: Update, context: CallbackContext) -> int:
"""Command Help /help"""
user = update.message.from_user
# Send message
update.message.reply_text(
text="""ℹ️ Вкажи якомога докладніше, яку саме запчастину та на яке авто шукаєш. Виконуй вказівки бота, щоб надати необхідну інформацію продавцям та прискорити відгук на свій запит."""
)
# Logger
logger.info("%s: send /help command;", user.first_name)
def channel_list(update: Update, context: CallbackContext) -> int:
"""Send list of chats to user"""
user = update.message.from_user
# Send message
update.message.reply_text(
text="""Якщо маєш запчастини на продаж, знайди покупця у місцевому чаті 🤝:\n"""+'\n'.join([x for x in CHANNELS.values()])
)
# Logger
logger.info("%s: ask channel list;", user.first_name)
def ask(update: Update, context: CallbackContext) -> int:
"""Stores the selected gender and asks for a photo."""
user = update.message.from_user
# Create user detail request in user_data
context.user_data['detail_request'] = {}
# Send message and remove keyboard
update.message.reply_text(
text="1️⃣ Вкажи марку та модель свого авто:",
reply_markup=ReplyKeyboardRemove(),
)
# Logger
logger.info("%s: start creating the request", user.first_name)
return CAR
def car(update: Update, context: CallbackContext) -> int:
"""Stores the photo and asks for a location."""
user = update.message.from_user
# Set car name to user_data
value = update.message.text
context.user_data['detail_request']['car'] = value
# Send message
update.message.reply_text(
text="2️⃣Напиши VIN номер авто:"
)
# Logger
logger.info("%s: set %s as car;", user.first_name, value)
return VIN
def vin(update: Update, context: CallbackContext) -> int:
"""Stores the location and asks for some info about the user."""
user = update.message.from_user
# Set car VIN name to user_data
value = update.message.text
context.user_data['detail_request']['car_vin'] = value
# Send message
update.message.reply_text(
text="3️⃣Напиши через кому всі запчастини, які тобі потрібні:"
)
# Logger
logger.info("%s: set VIN: %s;", user.first_name, value)
return DETAIL
def error_vin(update: Update, context: CallbackContext) -> int:
"""Stores the location and asks for some info about the user."""
user = update.message.from_user
value = update.message.text
# Send message
update.message.reply_text(
text="VIN номер неправильний. Він має містити 17 символів. Спробуй ще раз."
)
# Logger
logger.info("%s: send invalid VIN: %s;", user.first_name, value)
return VIN
def detail(update: Update, context: CallbackContext) -> int:
"""Stores the photo and asks for a location."""
user = update.message.from_user
# Set detail_name to user_data
value = update.message.text
context.user_data['detail_request']['detail'] = value
# Make the keyboard
keyboard = [
[
InlineKeyboardButton("Львівська", callback_data='lviv'),
InlineKeyboardButton("Івано-Франківська", callback_data='ivano-frankivsk'),
InlineKeyboardButton("Тернопільська", callback_data='ternopil')
],
[
InlineKeyboardButton("Хмельницька", callback_data='khmelnitsky'),
InlineKeyboardButton("Закарпатська", callback_data='zakarpattya'),
InlineKeyboardButton("Чернівецька", callback_data='chernivetskya')
],
[
InlineKeyboardButton("Вінницька", callback_data='vinnytsia'),
InlineKeyboardButton("Житомирська", callback_data='zhytomyr'),
InlineKeyboardButton("Київська", callback_data='kyiv')
],
[
InlineKeyboardButton("Дніпропетровська", callback_data='dnipro'),
InlineKeyboardButton("Запорізька", callback_data='zaporozhye'),
InlineKeyboardButton("Кіровоградська", callback_data='kropyvnytskyi')
],
[
InlineKeyboardButton("Миколаївська", callback_data='mykolaiv'),
InlineKeyboardButton("Волинська", callback_data='volyn'),
InlineKeyboardButton("Одеська", callback_data='odesa')
],
[
InlineKeyboardButton("Полтавська", callback_data='poltava'),
InlineKeyboardButton("Сумська", callback_data='sumy'),
InlineKeyboardButton("Харківська", callback_data='kharkiv')
],
[
InlineKeyboardButton("Херсонська", callback_data='kherson'),
InlineKeyboardButton("Черкаська", callback_data='cherkasy'),
InlineKeyboardButton("Чернігівська", callback_data='chernigiv')
],
[
InlineKeyboardButton("Луганська", callback_data='lugansk'),
InlineKeyboardButton("Рівненська", callback_data='rivne'),
InlineKeyboardButton("Донецька", callback_data='donezk'),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
# Send message
update.message.reply_text(
text="4️⃣ Вкажи свою область, щоб наші продавці швидше надали тобі запчастини:",
reply_markup=reply_markup
)
# Logger
logger.info("%s: ask detail: %s;", user.first_name, value)
return LOCATION
def location(update: Update, context: CallbackContext) -> int:
"""Stores the location and asks for some info about the user."""
query = update.callback_query
query.answer()
# Get callback
chat_id = update.callback_query.message.chat.id
message_id = update.callback_query.message.message_id
# Set request location name to user_data
location = update.callback_query.data
context.user_data['detail_request']['location'] = location
# Delete message with location inline keyboard
context.bot.delete_message(chat_id, message_id)
# Send message
context.bot.send_message(
chat_id=chat_id,
text="""5⃣ Натискай "Відправити номер📞", щоб продавець міг з тобою зв'язатися" """,
reply_markup=ReplyKeyboardMarkup(
[
[KeyboardButton(
text='Відправити номер 📞',
request_contact=True
)]
],
one_time_keyboard=True,
input_field_placeholder='Надішли свій номер натиснувши кнопку нижче ⤵️',
resize_keyboard=True
),
)
# Logger
logger.info("%s: set location: %s;", chat_id, location)
return CONTACT
def error_location(update: Update, context: CallbackContext) -> int:
"""Stores the location and asks for some info about the user."""
user = update.message.from_user
value = update.message.text
# Send message
update.message.reply_text(
text="Надішли локацію за допомогою клавіатури у повідомлені вище."
)
# Logger
logger.info("%s: send invalid location: %s;", user.first_name, value)
return LOCATION
def contact(update: Update, context: CallbackContext) -> int:
"""Get contact info about the user."""
user = update.message.from_user
# Get request info from user_data
detail_request = context.user_data.get('detail_request', 'Not found')
# get channel name
channel_name = CHANNELS[detail_request['location']]
# set contact
contact = update.message.contact
context.user_data['detail_request']['contact'] = {}
context.user_data['detail_request']['contact']['user_id'] = contact.user_id
context.user_data['detail_request']['contact']['phone_number'] = contact.phone_number
# Send message
update.message.reply_text(
text=f"Дякую за звернення ✅\nОчікуй дзвінок від продавця найближчим часом!\nСтан запиту можна відстежувати у каналі - {channel_name}",
reply_markup=ReplyKeyboardRemove()
)
# Logger 1
logger.info("%s: set contact: %s;", user.first_name, contact)
# Send request to channels
context.bot.send_message(
chat_id=channel_name,
text=f"""✅ Нова заявка на запчастину!\n\n🚗 Автомобіль: {detail_request['car']};\n⚙️ Необхідна деталь: {detail_request['detail']};\nVIN номер: {detail_request['car_vin']};\n\nОбласть: {detail_request['location']};\n📞 Контакт: {detail_request['contact']['phone_number']};""",
)
# Logger 2
logger.info("Bot send %s`s request to %s;", user.first_name, channel_name)
return ConversationHandler.END
def error_contact(update: Update, context: CallbackContext) -> int:
"""Stores the location and asks for some info about the user."""
user = update.message.from_user
value = update.message.text
# Send message
update.message.reply_text(
text="Надішліть номер кнопкою нижче"
)
# Logger
logger.info("%s: send invalid Contact: %s;", user.first_name, value)
return CONTACT
def cancel(update: Update, context: CallbackContext) -> int:
"""Cancels and ends the conversation."""
user = update.message.from_user
update.message.reply_text(
text='Дякуємо за увагу, до побачення 👋',
reply_markup=ReplyKeyboardRemove()
)
# Logger
logger.info("User %s canceled the conversation.", user.first_name)
return ConversationHandler.END
def main() -> None:
"""Run the bot."""
# Create the Updater and pass it your bot's token.
updater = Updater(getenv("BOT_TOKEN"))
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# Add conversation handler
conv_handler = ConversationHandler(
entry_points=[
MessageHandler(Filters.regex('^Додати запит на деталь ⚙️$'), ask)
],
states={
CAR: [
CommandHandler('cancel', cancel),
MessageHandler(Filters.text, car)
],
VIN: [
MessageHandler(
Filters.regex(r"^(?=.*[0-9])(?=.*[A-z])[0-9A-z-]{17}$"), vin
),
CommandHandler('cancel', cancel),
MessageHandler(Filters.text, error_vin)
],
DETAIL: [
CommandHandler('cancel', cancel),
MessageHandler(Filters.text, detail)
],
LOCATION: [
CallbackQueryHandler(location),
CommandHandler('cancel', cancel),
MessageHandler(Filters.text, error_location)
],
CONTACT: [
MessageHandler(Filters.contact, contact),
CommandHandler('cancel', cancel),
MessageHandler(Filters.text, error_contact)
],
},
fallbacks=[
CommandHandler('cancel', cancel)
]
)
dispatcher.add_handler(conv_handler)
# command handler
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", helper))
dispatcher.add_handler(
MessageHandler(Filters.regex('^Наявні запити$'), channel_list)
)
# Start the Bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()