This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
122 lines (97 loc) · 5.87 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
#!/usr/bin/env python3
import os
import sys
import json
import random
from ai_storage.storage import Storage
from telegram import ParseMode, KeyboardButton, ReplyKeyboardMarkup
from telegram.ext import Updater, run_async, CommandHandler, ConversationHandler, MessageHandler, Filters
s = Storage('data/data.json', 'data/tayga_upos_skipgram_300_2_2019/model.bin')
_stories = json.loads(open("data/stories.json", 'r').read())["data"]
_events = "".join(json.loads(open("data/events.json", 'r').read())["data"])
_info = json.loads(open("data/info.json", 'r').read())
@run_async
def start(update, context):
context.bot.send_message(chat_id=update.message.chat_id,
text="Здравствуйте!\n" + \
"Меня зовут Петр, я бот приемной комиссии НИУ МФТИ. Здесь можно найти ответы на интересующие " + \
"Вас *вопросы* о поступлении, введя команду /info, или же можно просто *спросить* меня, а я попытаюсь ответить :)\n\n" + \
"Все возможности бота доступны по коменде /help",
parse_mode=ParseMode.MARKDOWN)
@run_async
def info(update, context):
info_keys = list(_info.keys())
keyboard = [[KeyboardButton(info_keys[0], callback_data=info_keys[0])],
[KeyboardButton(info_keys[1], callback_data=info_keys[1])],
[KeyboardButton(info_keys[2], callback_data=info_keys[2])],
[KeyboardButton(info_keys[3], callback_data=info_keys[3])],
[KeyboardButton(info_keys[4], callback_data=info_keys[4])],
[KeyboardButton(info_keys[5], callback_data=info_keys[5])],
[KeyboardButton(info_keys[6], callback_data=info_keys[6])],
[KeyboardButton(info_keys[7], callback_data=info_keys[7])],
[KeyboardButton("Отмена", callback_data="Отмена")]]
reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True)
update.message.reply_text(
"Выберите, что Вас интересует:",
reply_markup=reply_markup
)
return 1
@run_async
def show_info(update, context):
if update.message.text == 'Отмена':
return ConversationHandler.END
answer = _info[update.message.text]
context.bot.send_message(chat_id=update.message.chat_id,
text=answer,
parse_mode=ParseMode.MARKDOWN)
return ConversationHandler.END
@run_async
def contacts(update, context):
context.bot.send_message(chat_id=update.message.chat_id,
text="*График работы* приёмной комиссии летом 2020 года по приёму документов для поступающих на 1 курс " + \
"бакалавриата и специалитета будет опубликован не позднее 01 июня 2020 года.\n\n" + \
"*Время работы (вне приемной кампании)*: с понедельника по пятницу, с 9:00 до 18:00, обед с 12:00 до 13:00\n" + \
"*Телефон*: +7 (495) 408-48-00\n" + \
"*E-mail*: [pk@mipt.ru](pk@mipt.ru)",
parse_mode=ParseMode.MARKDOWN)
@run_async
def help(update, context):
context.bot.send_message(chat_id=update.message.chat_id,
text="/info - Ответы на часто задаваемые *вопросы*\n" + \
"/events - Важные события и *график работы* приемной комиссии\n" + \
"/contacts - Контакты приемной комиссии\n" + \
"/random - Случайная физтеховская байка\n\n" + \
"Также я могу ответить на многие другие вопросы о поступлении - просто спросите:)",
parse_mode=ParseMode.MARKDOWN)
@run_async
def events(update, context):
context.bot.send_message(chat_id=update.message.chat_id,
text=_events,
parse_mode=ParseMode.MARKDOWN)
@run_async
def story(update, context):
story_list = list(_stories.values())
context.bot.send_message(chat_id=update.message.chat_id,
text=story_list[random.randint(0,len(story_list)-1)],
parse_mode=ParseMode.MARKDOWN)
@run_async
def custom_text_question(update, context):
question = update.message.text
answer = s.search(question)[0]
context.bot.send_message(chat_id=update.message.chat_id,
text=answer,
parse_mode=ParseMode.MARKDOWN)
if __name__ == '__main__':
telegram_api_token = os.environ.get('TELEGRAM_API_TOKEN')
if telegram_api_token is None:
sys.exit(0x01)
updater = Updater(telegram_api_token, use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('info', info))
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_handler(CommandHandler('contacts', contacts))
updater.dispatcher.add_handler(CommandHandler('events', events))
updater.dispatcher.add_handler(CommandHandler('random', story))
updater.dispatcher.add_handler(MessageHandler(Filters.text and Filters.regex('^(?!.*Отмена)'), custom_text_question))
updater.start_polling()
updater.idle()