-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_app.py
180 lines (152 loc) Β· 7.41 KB
/
main_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
import logging
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update, ParseMode
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
ConversationHandler,
CallbackContext,
)
# Enable logging
from module_study_word import menu_study_word
from module_test_word import menu_test_word
from picture_test import get_url, check_result_picture
from test_after_translate import test_after_translate
from translate_word import menu_translate_word
from module_statistics import get_static
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
GENDER, PHOTO, LOCATION, BIO, TESTS = range(5)
def start_menu(update: Update, context: CallbackContext):
reply_keyboard = [['Translate Words π', 'Study Words π', 'Take A Quiz π', 'Statistics π']]
update.message.reply_text(
'<i> Please Click An Option ππ» </i>',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),parse_mode=ParseMode.HTML
)
def start(update: Update, context: CallbackContext) -> int:
start_menu(update,CallbackContext)
return GENDER
def gender(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
if(update.message.text == 'Study Words π' or update.message.text == 'Next Word β‘οΈ' ):
s = menu_study_word(update.effective_chat.id,'/study_word')
context.bot.send_message(chat_id=update.effective_chat.id,
text=str(s),parse_mode=ParseMode.HTML)
reply_keyboard = [['Next Word β‘οΈ', 'Go Back To Menu β©οΈ']]
update.message.reply_text(
'<i> Please Click An Option ππ»</i>',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),parse_mode=ParseMode.HTML
)
return PHOTO
if(update.message.text == 'Go Back To Menu β©οΈ'):
start_menu(update, CallbackContext)
return GENDER
if(update.message.text == 'Translate Words π'):
update.message.reply_text('Please Enter A Word To Translate')
return BIO
if(update.message.text == 'Statistics π'):
update.message.reply_text(get_static(update.effective_chat.id), parse_mode=ParseMode.HTML)
start_menu(update, CallbackContext)
return GENDER
if(update.message.text == "Lets Go To Test π"):
update.message.reply_text(test_after_translate(update.effective_chat.id))
return LOCATION
if update.message.text == 'Take A Quiz π' or update.message.text == 'For Another Test π':
reply_keyboard = [['Test On Words π'], ['Test On Images π
'], ['Go Back To Menu β©οΈ']]
update.message.reply_text(
'<i> Please Click An Option ππ»</i>',
reply_markup = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),parse_mode=ParseMode.HTML
)
return GENDER
if (update.message.text == 'Test On Words π'):
s = menu_test_word(update.effective_chat.id,'/test')
update.message.reply_text(s, parse_mode=ParseMode.HTML)
return LOCATION
if (update.message.text == 'Test On Images π
'):
s = get_url(update.effective_chat.id)
update.message.reply_text(s, parse_mode=ParseMode.HTML)
return TESTS
else:
return ConversationHandler.END
def location(update: Update, context: CallbackContext) -> int:
if (update.message.text == 'Go Back To Menu β©οΈ'):
start_menu(update, CallbackContext)
return GENDER
s = menu_test_word(update.effective_chat.id, update.message.text)
context.bot.send_message(chat_id=update.effective_chat.id,
text=str(s), parse_mode=ParseMode.HTML)
reply_keyboard = [['For Another Test π', 'Go Back To Menu β©οΈ']]
update.message.reply_text(
'<i> Please Click An Option ππ»</i>',
reply_markup = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),parse_mode=ParseMode.HTML
)
return GENDER
def bio(update: Update, context: CallbackContext) -> int:
if (update.message.text == "Lets Go To Test π"):
s = test_after_translate(update.effective_chat.id)
update.message.reply_text(s, parse_mode=ParseMode.HTML)
return LOCATION
if (update.message.text == 'Go Back To Menu β©οΈ'):
start_menu(update, CallbackContext)
return GENDER
user = update.message.from_user
s = menu_translate_word(update.effective_chat.id, update.message.text)
split_s = s.split('\n\n')
update.message.reply_text(split_s[0], parse_mode=ParseMode.HTML)
reply_keyboard = [["Lets Go To Test π"]]
update.message.reply_text(
split_s[1],
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),parse_mode=ParseMode.HTML
)
return BIO
def tests(update: Update, context: CallbackContext) -> int:
if (update.message.text == 'Go Back To Menu β©οΈ'):
start_menu(update, CallbackContext)
return GENDER
s = check_result_picture(update.effective_chat.id, update.message.text)
context.bot.send_message(chat_id=update.effective_chat.id,
text=str(s), parse_mode=ParseMode.HTML)
reply_keyboard = [['For Another Test π', 'Go Back To Menu β©οΈ']]
update.message.reply_text(
'<i> Please Click An Option ππ»</i>',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),parse_mode=ParseMode.HTML
)
return GENDER
def cancel(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
logger.info("User %s canceled the conversation.", user.first_name)
update.message.reply_text(
'Bye! I hope we can learn English together again some day.', reply_markup=ReplyKeyboardRemove()
)
return ConversationHandler.END
def main() -> None:
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
TOKEN = '1402469846:AAEDgL_Irb2yvMXEc7009gvUf8bnf125KkY'
updater = Updater(TOKEN, use_context=True)
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# Add conversation handler with the states GENDER, PHOTO, LOCATION and BIO
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
GENDER: [MessageHandler(Filters.regex('^(Translate Words π|Study Words π|Take A Quiz π|Statistics π|Go Back To Menu β©οΈ|For Another Test π|Test On Words π|Test On Images π
)$'), gender)],
PHOTO: [MessageHandler(Filters.regex('^(Next Word β‘οΈ|Go Back To Menu β©οΈ|For Another Test π)$'), gender)],
LOCATION: [MessageHandler(Filters.text & ~Filters.command, location)],
BIO: [MessageHandler(Filters.text & ~Filters.command, bio)],
TESTS: [MessageHandler(Filters.text & ~Filters.command, tests)]
},
fallbacks=[CommandHandler('cancel', cancel)],
)
dispatcher.add_handler(conv_handler)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
main()