-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
872 lines (672 loc) · 34.9 KB
/
bot.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
from telegram.ext import Updater, CommandHandler, ConversationHandler, MessageHandler, Filters, CallbackQueryHandler
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, InlineKeyboardMarkup, InlineKeyboardButton
import requests
from maps_api.request import geocoder_request, map_request
from maps_api.geocoder import get_pos, get_bbox, get_country_code, get_city, check_response
from maps_api.static import get_static_map
from news_parser.parser import parse_news
from weather.weather import get_current_weather, get_forecast_weather
from schedule_api.airports import airs
from schedule_api.schedule import get_flights
from speech_api.speech_analyze import speech_analyze
from speech_api.xml_parser import speech_parser
from headhunter_api.suggestions import keywords_suggest, region_suggest
from headhunter_api import vacancies_request, full_vacancy_request
from os import environ
# from config import TELEGRAM_TOKEN, SPEECH_TOKEN, WEATHER_TOKEN
TELEGRAM_TOKEN = environ['telegram']
SPEECH_TOKEN = environ['speech']
WEATHER_TOKEN = environ['weather']
import logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
keyboard1 = [['↪️Пропустить']]
keyboard2 = [['🗺Показать на карте'], ['🗞Последние новости'], ['🌧Погода'], ['🛩Расписания'], ['💸Вакансии'],
['🔙Вернуться назад']]
keyboard3 = [['🔙Вернуться назад']]
keyboard4 = [['🌤Текущая погода'], ['☔️Прогноз на 6 дней'], ['🔙Вернуться назад']]
keyboard5 = [['✈️Найти авиарейс'], ['🔙Вернуться назад']]
keyboard6 = [['📚Сервисы для города'], ['👤Показать текущий профиль вакансий'], ['⚙Настройки профиля вакансий']]
keyboard7 = [['🔠Настройка ключевых слов'], ['🌆Настройка города'], ['🔙Вернуться назад']]
inline_news_state1 = InlineKeyboardMarkup(
[[InlineKeyboardButton('Следующая новость▶️', callback_data=1)], [InlineKeyboardButton('🔙Назад', callback_data=3)]])
inline_news_state2 = InlineKeyboardMarkup([
[InlineKeyboardButton('◀️Предыдущая новость', callback_data=2),
InlineKeyboardButton('Следующая новость▶️', callback_data=1)],
[InlineKeyboardButton('🔙Назад', callback_data=3)]
])
inline_news_state3 = InlineKeyboardMarkup([
[InlineKeyboardButton('◀️Предыдущая новость', callback_data=2)],
[InlineKeyboardButton('🔙Назад', callback_data=3)]
])
inline_maps = InlineKeyboardMarkup([
[InlineKeyboardButton('🗺Карта', callback_data='map')],
[InlineKeyboardButton('🛰Спутник', callback_data='sat')],
[InlineKeyboardButton('🗺➕🛰Гибрид', callback_data='sat,skl')],
])
inline_sch_state1 = InlineKeyboardMarkup(
[[InlineKeyboardButton('Следующий рейс▶️', callback_data=1)], [InlineKeyboardButton('🔙Назад', callback_data=3)]])
inline_sch_state2 = InlineKeyboardMarkup([
[InlineKeyboardButton('◀️Предыдущий рейс', callback_data=2),
InlineKeyboardButton('Следующий рейс▶️', callback_data=1)],
[InlineKeyboardButton('🔙Назад', callback_data=3)]
])
inline_sch_state3 = InlineKeyboardMarkup([
[InlineKeyboardButton('⏮Предыдущий рейс', callback_data=2)],
[InlineKeyboardButton('🔙Назад', callback_data=3)]
])
def start(bot, update):
update.message.reply_text(
'Как Вас зовут?', reply_markup=ReplyKeyboardMarkup(keyboard1), one_time_keyboard=False)
return ENTER_NAME
def enter_name(bot, update, user_data):
name = update.message.text
if name != '↪️Пропустить':
user_data['username'] = name
else:
user_data['username'] = None
user_data['vacancy'] = {
'region_name': None,
'region_id': None,
'keywords': None
}
update.message.reply_text('В каком городе Вы живете?')
return ENTER_LOCATION
def enter_location(bot, update, user_data):
location = update.message.text
if location != '↪️Пропустить':
user_data['location'] = location
suggests = region_suggest(location)
user_data['region_suggests'] = suggests
if len(suggests) != 0:
location_keyboard = [['↪️Пропустить']]
for suggestion in suggests:
location_keyboard.append([suggestion])
update.message.reply_text(
'Найдено несколько регионов, соответствующих введенному.\n'
'Выберите один из них.',
reply_markup=ReplyKeyboardMarkup(location_keyboard)
)
return LOCATION_APPLY
update.message.reply_text(
'Данный город не найдена в базе регионов.'
)
else:
user_data['location'] = None
name = ', {}'.format(user_data['username']) if user_data['username'] is not None else ''
update.message.reply_text('Добро пожаловать{}!'.format(name), reply_markup=ReplyKeyboardMarkup(keyboard6))
return MAIN_MENU
def location_apply(bot, update, user_data):
text = update.message.text
if text in user_data['region_suggests']:
user_data['location'] = text
user_data['vacancy']['region_name'] = text
user_data['vacancy']['region_id'] = user_data['region_suggests'][text]
update.message.reply_text(
'Город успешно установлен!'
)
elif text != '↪️Пропустить':
update.message.reply_text(
'Введенный текст не является ни одним из перечисленных регионов.\n'
'Попробуйте ввести название региона ещё раз.'
)
return LOCATION_APPLY
name = ', {}'.format(user_data['username']) if user_data['username'] is not None else ''
update.message.reply_text('Добро пожаловать{}!'.format(name), reply_markup=ReplyKeyboardMarkup(keyboard6))
return MAIN_MENU
def main_menu(bot, update, user_data):
text = update.message.text
if text == '📚Сервисы для города':
update.message.reply_text(
'Введите город, информацию о котором Вы хотите узнать',
reply_markup=ReplyKeyboardMarkup(keyboard3)
)
return SEARCH_HANDLER
elif text == '👤Показать текущий профиль вакансий':
region = user_data['vacancy']['region_name']
if region is None:
if user_data['location'] is None:
region = 'Не указано'
else:
region = 'Указанный город не найден в базе данных HeadHunter'
keywords = user_data['vacancy']['keywords']
if keywords is None: keywords = 'Не указано'
update.message.reply_text(
'Город: {}\n'
'Ключевые слова: {}\n'.format(
region, keywords
)
)
elif text == '⚙Настройки профиля вакансий':
update.message.reply_text(
'Выберите параметры, которые Вы хотите настроить',
reply_markup=ReplyKeyboardMarkup(keyboard7)
)
return PROFILE_CONFIG
return MAIN_MENU
def profile_config(bot, update, user_data):
text = update.message.text
if text == '🔠Настройка ключевых слов':
update.message.reply_text(
'Введите ключевые слова, которые будут использоваться при поиске вакансий',
reply_markup=ReplyKeyboardMarkup(keyboard3)
)
return KEYWORDS_CONFIG
elif text == '🌆Настройка города':
update.message.reply_text('Введите город, в котором ищите вакансию',
reply_markup=ReplyKeyboardMarkup(keyboard1))
return ENTER_LOCATION
elif text == '🔙Вернуться назад':
update.message.reply_text('Что Вы хотите сделать?', reply_markup=ReplyKeyboardMarkup(keyboard6))
return MAIN_MENU
return PROFILE_CONFIG
def keywords_config(bot, update, user_data):
text = update.message.text
suggests = keywords_suggest(text)
if text == '🔙Вернуться назад':
update.message.reply_text(
'Возвращаемся в меню настроек',
reply_markup=ReplyKeyboardMarkup(keyboard7)
)
return PROFILE_CONFIG
if len(suggests) != 0:
user_data['keywords_suggests'] = suggests
keywords_keyboard = [['🔙Вернуться назад']]
for suggestion in suggests:
keywords_keyboard.append([suggestion])
update.message.reply_text(
'Бот нашел несколько схожих ключевых слов. Выберите одно из них',
reply_markup=ReplyKeyboardMarkup(keywords_keyboard)
)
return KEYWORDS_APPLY
else:
user_data['vacancy']['keywords'] = text
update.message.reply_text(
'Ключевые слова успешно установлены! Возвращаемся в меню настроек',
reply_markup=ReplyKeyboardMarkup(keyboard7)
)
return PROFILE_CONFIG
def keywords_apply(bot, update, user_data):
text = update.message.text
if text in user_data['keywords_suggests']:
user_data['vacancy']['keywords'] = text
update.message.reply_text(
'Ключевые слова успешно установлены! Возвращаемся в меню настроек',
reply_markup=ReplyKeyboardMarkup(keyboard7)
)
return PROFILE_CONFIG
elif text == '🔙Вернуться назад':
update.message.reply_text(
'Возвращаемся в меню настроек',
reply_markup=ReplyKeyboardMarkup(keyboard7)
)
return PROFILE_CONFIG
else:
update.message.reply_text(
'Введенный текст не является ни одним из перечисленных ключевых слов.\n'
'Попробуйте ввести ключевые слова ещё раз.'
)
return KEYWORDS_APPLY
def search_handler(bot, update, user_data):
text = update.message.text
if text == '🔙Вернуться назад':
update.message.reply_text(
'Возвращаемся в главное меню',
reply_markup=ReplyKeyboardMarkup(keyboard6)
)
return MAIN_MENU
response = geocoder_request(geocode=text, format='json')
if check_response(response):
update.message.reply_text(
'Город определен',
reply_markup=ReplyKeyboardMarkup(keyboard2)
)
update.message.reply_text('Выберите одну из возможных функций для данного города:',
reply_markup=ReplyKeyboardMarkup(keyboard2))
user_data['current_response'] = response
return LOCATION_HANDLER
update.message.reply_text('Заданный город не найден.')
return SEARCH_HANDLER
def voice_to_text(bot, update, user_data):
voice = update.message.voice.get_file()
file = requests.get(voice.file_path).content
response = speech_analyze(SPEECH_TOKEN, file)
text = speech_parser(response)
data = geocoder_request(geocode=text, format='json')
if check_response(data):
update.message.reply_text(
'Город определен',
reply_markup=ReplyKeyboardMarkup(keyboard2)
)
update.message.reply_text('Выберите одну из возможных функций для данного города:',
reply_markup=ReplyKeyboardMarkup(keyboard2))
user_data['current_response'] = data
return LOCATION_HANDLER
update.message.reply_text('По данному адресу ничего не найдено.')
return SEARCH_HANDLER
def location_handler(bot, update, user_data):
text = update.message.text
if text == '🗺Показать на карте':
res = "[]({}){}".format(get_static_map(user_data),
'Карта для города ' + get_city(user_data['current_response'], 'ru-RU'))
update.message.reply_text(res, parse_mode='markdown', reply_markup=inline_maps)
elif text == '🗞Последние новости':
news = parse_news(user_data['current_response'])
if news is not None:
user_data['array'] = news
user_data['index'] = 0
user_data['length'] = len(news)
update.message.reply_text('Найдено новостей в заданном городе: {}'.format(len(news)),
reply_markup=ReplyKeyboardRemove())
update.message.reply_text('*{0}*\n{1}\n[Подробнее:]({2})'.format(*news[0]), parse_mode='markdown',
reply_markup=inline_news_state1)
return NEWS_HANDLER
else:
update.message.reply_text('Новостей для этой местности не найдено')
elif text == '🌧Погода':
update.message.reply_text(
'Что вы хотите узнать о погоде в городе {}?'.format(get_city(user_data['current_response'], 'ru-RU')),
reply_markup=ReplyKeyboardMarkup(keyboard4))
return WEATHER_HANDLER
elif text == '🛩Расписания':
update.message.reply_text(
'Выберите один из вариантов поиска:',
reply_markup=ReplyKeyboardMarkup(keyboard5))
return RASP_HANDLER
elif text == '💸Вакансии':
try:
data = geocoder_request(geocode=get_city(user_data['current_response']), format='json')
city = get_city(data, 'ru_RU')
region = list(region_suggest(city).items())[0][1]
except:
update.message.reply_text(
'Данный город не найден в базе данных HeadHunter.',
reply_markup=ReplyKeyboardMarkup(keyboard2)
)
return LOCATION_HANDLER
try:
params = {
'area': region
}
if user_data['vacancy']['keywords'] is not None:
params['text'] = user_data['vacancy']['keywords']
user_data['vacancies_response'] = vacancies_request(**params)['items']
if len(user_data['vacancies_response']) == 0:
update.message.reply_text(
'Для данного города не найдено ни одной вакансии.',
reply_markup=ReplyKeyboardMarkup(keyboard2)
)
return LOCATION_HANDLER
user_data['vacancies_index'] = 0
user_data['vacancies_image'] = 'logo'
update.message.reply_text('Найдено несколько вакансий', reply_markup=ReplyKeyboardRemove())
_keyboard = [
[InlineKeyboardButton('Следующая вакансия▶️', callback_data=1)],
[InlineKeyboardButton('🗺Местоположение', callback_data=4)],
[InlineKeyboardButton('🔙Назад', callback_data=3)]
]
if len(user_data['vacancies_response']) == 1:
_keyboard.pop(0)
reply = form_vacancy_reply(user_data)
if reply['address'] == 'Адрес не указан':
_keyboard.pop(1)
update.message.reply_text(
(
'*{title}*\n'
'{experience}\n'
'{address}\n'
'[Подробнее:]({url})\n'
'[]({image_url})' # EMPTY STRING IN BRACKETS
).format(**reply),
parse_mode='markdown',
reply_markup=InlineKeyboardMarkup(_keyboard)
)
return VACANCIES_HANDLER
except Exception as e:
logger.exception(e)
elif text == '🔙Вернуться назад':
update.message.reply_text(
'Введите город, информацию о котором Вы хотите узнать',
reply_markup=ReplyKeyboardMarkup(keyboard3)
)
return SEARCH_HANDLER
return LOCATION_HANDLER
def scrolling_vacancy(bot, update, user_data):
query = update.callback_query
try:
if query.data == '1':
if user_data['vacancies_index'] != len(user_data['vacancies_response']) - 1:
user_data['vacancies_index'] += 1
elif query.data == '2':
if user_data['vacancies_index'] != 0:
user_data['vacancies_index'] -= 1
elif query.data == '3':
bot.deleteMessage(
chat_id=query.message.chat_id,
message_id=query.message.message_id
)
bot.send_message(
query.message.chat_id,
'Выберите одну из возможных функций для данного местоположения:',
reply_markup=ReplyKeyboardMarkup(keyboard2)
)
return LOCATION_HANDLER
elif query.data == '4':
user_data['vacancies_image'] = 'location'
elif query.data == '5':
user_data['vacancies_image'] = 'logo'
keyboard = [
[],
[],
[InlineKeyboardButton('🔙Назад', callback_data=3)]
]
if user_data['vacancies_index'] != 0:
keyboard[0].append(InlineKeyboardButton(
'◀️Предыдущая вакансия', callback_data=2
))
if user_data['vacancies_index'] != len(user_data['vacancies_response']) - 1:
keyboard[0].append(InlineKeyboardButton(
'Следующая вакансия▶️', callback_data=1
))
reply = form_vacancy_reply(
user_data,
user_data['vacancies_image'] == 'location'
)
if reply['address'] != 'Адрес не указан':
if user_data['vacancies_image'] == 'location':
keyboard[1].append(InlineKeyboardButton('🎫Логотип', callback_data=5))
else:
keyboard[1].append(InlineKeyboardButton('🗺Местоположение', callback_data=4))
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=(
'*{title}*\n'
'{experience}\n'
'{address}\n'
'[]({image_url})' # EMPTY STRING IN BRACKETS
'[Подробнее:]({url})\n'
).format(**reply),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode='markdown'
)
return VACANCIES_HANDLER
except Exception as e:
logger.exception(e)
def form_vacancy_reply(user_data, add_location_image=False):
vacancy_id = user_data['vacancies_response'][user_data['vacancies_index']]['id']
vacancy = full_vacancy_request(vacancy_id)
title = vacancy['name']
experience = vacancy['experience']['name']
address = vacancy['address']
if address is not None:
try:
address = address['city'] + ', ' + address['street'] + ' ' + address['building']
except TypeError:
address = 'Адрес не указан'
else:
address = 'Адрес не указан'
vacancy_url = vacancy['alternate_url']
if add_location_image:
if vacancy['address'] is not None:
pos = vacancy['address']['lng'], vacancy['address']['lat']
image_url = map_request(
ll='{},{}'.format(*pos),
pt='{},{},pm2rdm'.format(*pos),
l='map'
)
else:
if vacancy['employer']['logo_urls'] is not None:
image_url = vacancy['employer']['logo_urls']['original']
else:
image_url = ''
else:
if vacancy['employer']['logo_urls'] is not None:
image_url = vacancy['employer']['logo_urls']['original']
else:
image_url = ''
return {
'title': title,
'experience': experience,
'address': address,
'url': vacancy_url,
'image_url': image_url
}
def scrolling_news(bot, update, user_data):
query = update.callback_query
d = {0: inline_news_state1, user_data['length'] - 1: inline_news_state3}
if query.data == '1':
user_data['index'] = min(user_data['length'], user_data['index'] + 1)
elif query.data == '2':
user_data['index'] = max(0, user_data['index'] - 1)
elif query.data == '3':
bot.deleteMessage(chat_id=query.message.chat_id,
message_id=query.message.message_id)
bot.send_message(query.message.chat_id, 'Выберите одну из возможных функций для данного местоположения:',
reply_markup=ReplyKeyboardMarkup(keyboard2))
return LOCATION_HANDLER
try:
bot.edit_message_text(text='*{0}*\n{1}\n[Подробнее:]({2})'.format(*user_data['array'][user_data['index']]),
chat_id=query.message.chat_id,
message_id=query.message.message_id, parse_mode='markdown',
reply_markup=d[user_data['index']] if user_data['index'] in d else inline_news_state2)
except IndexError:
if user_data['index'] < 0:
user_data['index'] = 0
else:
user_data['index'] = user_data['length'] - 1
def choosing_map_type(bot, update, user_data):
query = update.callback_query
bot.edit_message_text(chat_id=query.message.chat_id, message_id=query.message.message_id,
text="[]({}){}".format(get_static_map(user_data, query.data),
'Карта для города ' + get_city(
user_data['current_response'], 'ru-RU')),
parse_mode='markdown', reply_markup=inline_maps)
def enter_the_map(bot, update):
text = update.message.text
if text == '🔙Вернуться назад':
return LOCATION_HANDLER
def weather(bot, update, user_data):
text = update.message.text
if text == '🌤Текущая погода':
city, code = get_city(user_data['current_response']), get_country_code(user_data['current_response'])
update.message.reply_text(
get_current_weather(city, code, WEATHER_TOKEN, get_city(user_data['current_response'], 'ru-RU')))
elif text == '☔️Прогноз на 6 дней':
city, code = get_city(user_data['current_response']), get_country_code(user_data['current_response'])
update.message.reply_text(
get_forecast_weather(city, code, WEATHER_TOKEN, get_city(user_data['current_response'], 'ru-RU')))
elif text == '🔙Вернуться назад':
update.message.reply_text('Выберите одну из возможных функций для данного местоположения:',
reply_markup=ReplyKeyboardMarkup(keyboard2))
return LOCATION_HANDLER
def schedule(bot, update, user_data):
text = update.message.text
if text == '✈️Найти авиарейс':
city_ru, city_en = get_city(user_data['current_response'], 'ru_RU'), get_city(user_data['current_response'])
airports = airs.get(city_ru, []) + airs.get(city_en, [])
if airports:
airport_question(update, city_ru, city_en)
return SET_SECOND_CITY_HANDLER
else:
update.message.reply_text(
'В заданном городе аэропорта не найдено')
elif text == '🔙Вернуться назад':
update.message.reply_text('Выберите одну из возможных функций для данного местоположения:',
reply_markup=ReplyKeyboardMarkup(keyboard2))
return LOCATION_HANDLER
def set_second_city(bot, update, user_data):
text = update.message.text
if text == '🔙Вернуться назад':
update.message.reply_text(
'Выберите один из вариантов поиска:',
reply_markup=ReplyKeyboardMarkup(keyboard5))
return RASP_HANDLER
elif text == '🔚Вернуться в меню':
update.message.reply_text('Выберите одну из возможных функций для данного местоположения:',
reply_markup=ReplyKeyboardMarkup(keyboard2))
return LOCATION_HANDLER
else:
user_data['airport1'] = text.split(', ')[-1]
update.message.reply_text('Введите город пункта назначения:',
reply_markup=ReplyKeyboardMarkup(keyboard3 + [['🔚Вернуться в меню']]))
return SET_SECOND_AIRPORT_HANDLER
def set_second_airport(bot, update, user_data):
text = update.message.text
if text == '🔙Вернуться назад':
city_ru, city_en = get_city(user_data['current_response'], 'ru_RU'), get_city(user_data['current_response'])
airport_question(update, city_ru, city_en)
return SET_SECOND_CITY_HANDLER
elif text == '🔚Вернуться в меню':
update.message.reply_text('Выберите одну из возможных функций для данного местоположения:',
reply_markup=ReplyKeyboardMarkup(keyboard2))
return LOCATION_HANDLER
else:
response = geocoder_request(geocode=text, format='json')
if check_response(response):
user_data['city2'] = get_city(response, 'ru_RU')
city_en = get_city(response)
airports = airs.get(user_data['city2'], []) + airs.get(city_en, [])
if not airports:
update.message.reply_text('Введеный город не найден. Проверьте написание.')
return SET_SECOND_AIRPORT_HANDLER
update.message.reply_text('Выберите аэропорт прибытия:',
reply_markup=ReplyKeyboardMarkup(
[[elem[1] + ', ' + elem[0]] for elem in airports] + [['🔙Вернуться назад'],
['🔚Вернуться в меню']]))
return FIND_FLIGHTS_HANDLER
update.message.reply_text('Введеный город не найден. Проверьте написание.')
def find_flights(bot, update, user_data):
text = update.message.text
if text == '🔙Вернуться назад':
city_ru, city_en = get_city(user_data['current_response'], 'ru_RU'), get_city(user_data['current_response'])
airport_question(update, city_ru, city_en)
return SET_SECOND_CITY_HANDLER
elif text == '🔚Вернуться в меню':
update.message.reply_text('Выберите одну из возможных функций для данного местоположения:',
reply_markup=ReplyKeyboardMarkup(keyboard2))
return LOCATION_HANDLER
else:
airport2 = text.split(', ')[-1]
flights = get_flights(user_data['airport1'], airport2)
if not flights:
update.message.reply_text('Рейсов между указанными ранее аэропортами не найдено!')
city_ru, city_en = get_city(user_data['current_response'], 'ru_RU'), get_city(user_data['current_response'])
airport_question(update, city_ru, city_en)
return SET_SECOND_CITY_HANDLER
user_data['array'] = flights
user_data['index'] = 0
user_data['length'] = len(flights)
update.message.reply_text('Найдено рейсов для данного направления: {}'.format(len(flights)),
reply_markup=ReplyKeyboardRemove())
update.message.reply_text(
'[Данные предоставлены сервисом Яндекс.Расписания](http://rasp.yandex.ru/)',
parse_mode='markdown'
)
update.message.reply_text(flights[0],
reply_markup=inline_sch_state1 if len(flights) > 1 else ReplyKeyboardMarkup(
keyboard3))
def scrolling_flights(bot, update, user_data):
print(user_data['array'])
query = update.callback_query
d = {0: inline_sch_state1, user_data['length'] - 1: inline_sch_state3}
if query.data == '1':
user_data['index'] = min(user_data['length'], user_data['index'] + 1)
elif query.data == '2':
user_data['index'] = max(0, user_data['index'] - 1)
elif query.data == '3':
bot.deleteMessage(chat_id=query.message.chat_id,
message_id=query.message.message_id)
city_ru, city_en = get_city(user_data['current_response'], 'ru_RU'), get_city(user_data['current_response'])
airports = airs.get(city_ru, []) + airs.get(city_en, [])
bot.sendMessage(text='Из какого аэропорта города {} вы хотите найти рейс?'.format(city_ru),
chat_id=query.message.chat_id,
reply_markup=ReplyKeyboardMarkup(
[[elem[1] + ', ' + elem[0]] for elem in airports] + [['🔙Вернуться назад'],
['🔚Вернуться в меню']]))
return SET_SECOND_CITY_HANDLER
try:
bot.edit_message_text(text=user_data['array'][user_data['index']],
chat_id=query.message.chat_id,
message_id=query.message.message_id, parse_mode='markdown',
reply_markup=d[user_data['index']] if user_data['index'] in d else inline_sch_state2)
except IndexError:
if user_data['index'] < 0:
user_data['index'] = 0
else:
user_data['index'] = user_data['length'] - 1
def airport_question(update, city_ru, city_en):
airports = airs.get(city_ru, []) + airs.get(city_en, [])
update.message.reply_text('Из какого аэропорта города {} вы хотите найти рейс?'.format(city_ru),
reply_markup=ReplyKeyboardMarkup(
[[elem[1] + ', ' + elem[0]] for elem in airports] + [['🔙Вернуться назад'],
['🔚Вернуться в меню']]))
def stop(bot, update):
update.message.reply_text('Пока!', reply_markup=ReplyKeyboardRemove())
update.message.reply_text('Для того, чтобы начать работу с ботом заново напишите /start')
return ConversationHandler.END
def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"', update, error)
def main():
updater = Updater(TELEGRAM_TOKEN)
dp = updater.dispatcher
dp.add_error_handler(error)
dp.add_handler(conversation_handler)
updater.start_polling()
updater.idle()
(
ENTER_NAME, ENTER_LOCATION, SEARCH_HANDLER, LOCATION_HANDLER,
LOCATION_APPLY, MAIN_MENU, PROFILE_CONFIG, KEYWORDS_CONFIG, KEYWORDS_APPLY,
VACANCIES_HANDLER, NEWS_HANDLER, WEATHER_HANDLER, RASP_HANDLER,
SET_SECOND_CITY_HANDLER, SET_SECOND_AIRPORT_HANDLER,
FIND_FLIGHTS_HANDLER
) = range(16)
conversation_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
ENTER_NAME: [MessageHandler(Filters.text, enter_name, pass_user_data=True)],
ENTER_LOCATION: [MessageHandler(Filters.text, enter_location, pass_user_data=True)],
LOCATION_APPLY: [MessageHandler(Filters.text, location_apply, pass_user_data=True)],
MAIN_MENU: [MessageHandler(Filters.text, main_menu, pass_user_data=True)],
PROFILE_CONFIG: [MessageHandler(Filters.text, profile_config, pass_user_data=True)],
KEYWORDS_CONFIG: [MessageHandler(Filters.text, keywords_config, pass_user_data=True)],
KEYWORDS_APPLY: [MessageHandler(Filters.text, keywords_apply, pass_user_data=True)],
SEARCH_HANDLER: [
MessageHandler(Filters.text, search_handler, pass_user_data=True),
MessageHandler(Filters.voice, voice_to_text, pass_user_data=True)
],
LOCATION_HANDLER: [
MessageHandler(Filters.text, location_handler, pass_user_data=True),
CallbackQueryHandler(choosing_map_type, pass_user_data=True),
],
NEWS_HANDLER: [
CallbackQueryHandler(scrolling_news, pass_user_data=True)
],
VACANCIES_HANDLER: [
CallbackQueryHandler(scrolling_vacancy, pass_user_data=True)
],
WEATHER_HANDLER: [
MessageHandler(Filters.text, weather, pass_user_data=True)
],
RASP_HANDLER: [
MessageHandler(Filters.text, schedule, pass_user_data=True)
],
SET_SECOND_CITY_HANDLER: [
MessageHandler(Filters.text, set_second_city, pass_user_data=True)
],
SET_SECOND_AIRPORT_HANDLER: [
MessageHandler(Filters.text, set_second_airport, pass_user_data=True)
],
FIND_FLIGHTS_HANDLER: [
MessageHandler(Filters.text, find_flights, pass_user_data=True),
CallbackQueryHandler(scrolling_flights, pass_user_data=True),
]
},
fallbacks=[CommandHandler('stop', stop)]
)
if __name__ == '__main__':
main()