This repository was archived by the owner on May 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
208 lines (160 loc) · 6.31 KB
/
main.py
File metadata and controls
208 lines (160 loc) · 6.31 KB
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
import sys
import settings
sys.dont_write_bytecode = True
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
import django
django.setup()
from db.models import Feedback
from telegram.ext import Updater, ConversationHandler, CallbackContext, \
MessageHandler, Filters, CommandHandler
from telegram import Update, ReplyKeyboardMarkup, KeyboardButton, \
ReplyKeyboardRemove, MessageEntity
updater: Updater = Updater(
token=settings.TELEGRAM_TOKEN)
dispatcher = updater.dispatcher
MENU_STATE, FIRST_NAME_STATE, LAST_NAME_STATE, PHONE_STATE, FEEDBACK_STATE \
= range(5)
def menu_keyboard():
return ReplyKeyboardMarkup([
[KeyboardButton('Yangi fikr')],
[KeyboardButton('Mening fikrlarim')],
], resize_keyboard=True, one_time_keyboard=True)
def phone_keyboard():
return ReplyKeyboardMarkup([
[KeyboardButton('Telefon raqam yuborish',
request_contact=True)]
], resize_keyboard=True, one_time_keyboard=True)
def start_handler(update: Update, context: CallbackContext):
update.message.reply_text('Salom!', reply_markup=menu_keyboard())
return MENU_STATE
# def code_handler(update: Update, context: CallbackContext):
# args = context.args
# code = ''.join(args)
#
# if len(code) > 0 and code == '007':
# update.message.reply_text('Hush kelibsiz, 007')
# else:
# return start_handler(update, context)
def menu_handler(update: Update, context: CallbackContext):
update.message.reply_text(
'Pastdagi menyuni tanlang',
reply_markup=menu_keyboard(),
)
return MENU_STATE
def new_feedback_handler(update: Update, context: CallbackContext):
update.message.reply_text('Ismingizni kiriting!')
return FIRST_NAME_STATE
def first_name_handler(update: Update, context: CallbackContext):
context.chat_data.update({
'first_name': update.message.text,
})
print(context.chat_data)
update.message.reply_text('Ajoyib! Ana endi familiyangizni kiriting!')
return LAST_NAME_STATE
def last_name_handler(update: Update, context: CallbackContext):
context.chat_data.update({
'last_name': update.message.text,
})
update.message \
.reply_text('Telefon raqamingizni kiriting, yoki pastdagi '
'knopkani bosing',
reply_markup=phone_keyboard())
return PHONE_STATE
def last_name_resend_handler(update: Update, context: CallbackContext):
update.message.reply_text('Familiyangizni kiriting!')
def phone_entity_handler(update: Update, context: CallbackContext):
phone_number_entity = pne = \
list(filter(lambda e: e.type == 'phone_number',
update.message.entities))[0]
phone_number = update.message.text[pne.offset:pne.offset + pne.length]
context.chat_data.update({
'phone_number': phone_number,
})
update.message.reply_text('Endi fikringizni qoldiring')
return FEEDBACK_STATE
def phone_contact_handler(update: Update, context: CallbackContext):
contact = update.message.contact
context.chat_data.update({
'phone_number': '+' + contact.phone_number,
})
update.message.reply_text('Endi fikringizni qoldiring')
return FEEDBACK_STATE
def phone_resend_handler(update: Update, context: CallbackContext):
update.message \
.reply_text('Telefon raqamingizni kiriting, yoki pastdagi '
'knopkani bosing',
reply_markup=phone_keyboard())
def feedback_handler(update: Update, context: CallbackContext):
context.chat_data.update({
'feedback': update.message.text,
})
update.message.reply_text('Fikringiz uchun rahmat!')
print(context.chat_data)
cd = context.chat_data
feedback = Feedback.objects.create(
first_name=cd['first_name'][0:255],
last_name=cd['last_name'][0:255],
phone_number=cd['phone_number'][0:63],
feedback=cd['feedback'],
user_id=update.effective_user.id,
)
print(feedback)
return menu_handler(update, context)
def feedback_resend_handler(update: Update, context: CallbackContext):
update.message.reply_text('Fikringizni matn orqali qoldiring')
def all_feedbacks_handler(update: Update, context: CallbackContext):
update.message.reply_text(
'Mening ohirgi 5 ta fikrim'
)
feedbacks = Feedback.objects.order_by('-id').filter(user_id=update.effective_user.id)[:5]
print(feedbacks)
if len(feedbacks) == 0:
update.message.reply_text('Siz hech qanday fikr qoldirmagansiz')
else:
for feedback in feedbacks:
update.message.reply_text(f'{feedback.feedback}'
f'\n\n'
f'_{feedback.first_name}_ _{feedback.last_name}_',
parse_mode='Markdown')
def stop_handler(update: Update, context: CallbackContext):
update.message.reply_text('Hayr!', reply_markup=ReplyKeyboardRemove())
dispatcher.add_handler(ConversationHandler(
entry_points=[
# CommandHandler('code', code_handler),
MessageHandler(Filters.all, start_handler),
],
states={
MENU_STATE: [
MessageHandler(Filters.regex(r'^Yangi fikr$'),
new_feedback_handler),
MessageHandler(Filters.regex(r'^Mening fikrlarim$'),
all_feedbacks_handler),
MessageHandler(Filters.all, menu_handler),
],
FIRST_NAME_STATE: [
MessageHandler(Filters.text, first_name_handler),
MessageHandler(Filters.all, new_feedback_handler),
],
LAST_NAME_STATE: [
MessageHandler(Filters.text, last_name_handler),
MessageHandler(Filters.all, last_name_resend_handler),
],
PHONE_STATE: [
MessageHandler(
Filters.text & Filters.entity(MessageEntity.PHONE_NUMBER),
phone_entity_handler),
MessageHandler(Filters.contact, phone_contact_handler),
MessageHandler(Filters.all, phone_resend_handler),
],
FEEDBACK_STATE: [
MessageHandler(Filters.text, feedback_handler),
MessageHandler(Filters.all, feedback_resend_handler),
],
},
fallbacks=[
CommandHandler('stop', stop_handler),
],
))
updater.start_polling()
updater.idle()