-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.py
174 lines (133 loc) · 6.95 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
from datetime import datetime, timedelta
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup
from telegram.ext import (Updater, CommandHandler, CallbackQueryHandler, ConversationHandler, MessageHandler, Filters)
from conf import TOKEN, DB_NAME
from db_helper import DBHelper
BTN_TODAY, BTN_TOMORROW, BTN_MONTH, BTN_REGION, BTN_DUA = (
'⌛️ Бугун', '⏳ Эртага', '📅 Тўлиқ тақвим', '🇺🇿 Минтақа', '🤲 Дуо')
main_buttons = ReplyKeyboardMarkup([
[BTN_TODAY], [BTN_TOMORROW, BTN_MONTH], [BTN_REGION], [BTN_DUA]
], resize_keyboard=True)
STATE_REGION = 1
STATE_CALENDAR = 2
user_region = dict()
db = DBHelper(DB_NAME)
def region_buttons():
regions = db.get_regions()
buttons = []
tmp_b = []
for region in regions:
tmp_b.append(InlineKeyboardButton(region['name'], callback_data=region['id']))
if len(tmp_b) == 2:
buttons.append(tmp_b)
tmp_b = []
return buttons
def start(update, context):
user = update.message.from_user
user_region[user.id] = None
buttons = region_buttons()
update.message.reply_html(
'Ассалому алайкум <b>{}!</b>\n \n<b>Рамазон ойи муборак бўлсин</b>\n \nСизга қайси минтақа бўйича маълумот берайин?'.
format(user.first_name), reply_markup=InlineKeyboardMarkup(buttons))
return STATE_REGION
def inline_callback(update, context):
try:
query = update.callback_query
user_id = query.from_user.id
user_region[user_id] = int(query.data)
query.message.delete()
query.message.reply_html(text='<b>Рамазон тақвими</b> 2️⃣0️⃣2️⃣0️⃣\n \nҚуйдагилардан бирини танланг 👇',
reply_markup=main_buttons)
return STATE_CALENDAR
except Exception as e:
print('error ', str(e))
def calendar_today(update, context):
try:
user_id = update.message.from_user.id
if not user_region[user_id]:
return STATE_REGION
region_id = user_region[user_id]
region = db.get_region(region_id)
today = str(datetime.now().date())
calendar = db.get_calendar_by_region(region_id, today)
photo_path = 'images/{}.jpg'.format(calendar['id'])
message = '<b>Рамазон</b> 2020\n<b>{}</b> вақти\n \nСаҳарлик: <b>{}</b>\nИфторлик: <b>{}</b>'.format(
region['name'], calendar['fajr'][:5], calendar['maghrib'][:5])
update.message.reply_photo(photo=open(photo_path, 'rb'), caption=message, parse_mode='HTML',
reply_markup=main_buttons)
except Exception as e:
print('Error ', str(e))
def calendar_tomorrow(update, context):
try:
user_id = update.message.from_user.id
if not user_region[user_id]:
return STATE_REGION
region_id = user_region[user_id]
region = db.get_region(region_id)
dt = str(datetime.now().date() + timedelta(days=1))
calendar = db.get_calendar_by_region(region_id, dt)
photo_path = 'images/{}.jpg'.format(calendar['id'])
message = '<b>Рамазон</b> 2020\n<b>{}</b> вақти\n \nСаҳарлик: <b>{}</b>\nИфторлик: <b>{}</b>'.format(
region['name'], calendar['fajr'][:5], calendar['maghrib'][:5])
update.message.reply_photo(photo=open(photo_path, 'rb'), caption=message, parse_mode='HTML',
reply_markup=main_buttons)
except Exception as e:
print('Error ', str(e))
def calendar_month(update, context):
try:
user_id = update.message.from_user.id
if not user_region[user_id]:
return STATE_REGION
region_id = user_region[user_id]
region = db.get_region(region_id)
photo_path = 'images/table/region_{}.jpg'.format(region['id'])
message = '<b>Рамазон</b> 2020\n<b>{}</b> вақти'.format(region['name'])
update.message.reply_photo(photo=open(photo_path, 'rb'), caption=message, parse_mode='HTML',
reply_markup=main_buttons)
except Exception as e:
print('Error ', str(e))
def select_region(update, context):
buttons = region_buttons()
update.message.reply_text('Сизга қайси минтақа бўйича маълумот берайин?',
reply_markup=InlineKeyboardMarkup(buttons))
return STATE_REGION
def select_dua(update, context):
saharlik = "<b>Саҳарлик (оғиз ёпиш) дуоси:</b>\nНавайту ан асума совма шаҳри Рамазона минал фажри илал мағриби, холисан лиллаҳи таъала."
iftorlik = "<b>Ифторлик (оғиз очиш) дуоси:</b>\nАллоҳумма лака сумту ва бика аманту ва аъалайка таваккалту ва ъала ризқика афтарту, фағфирли, йа Ғоффару, ма қоддамту вама аххорту."
update.message.reply_photo(photo=open('images/ramadan_dua.png', 'rb'),
caption="{}\n \n{}".format(saharlik, iftorlik), parse_mode='HTML',
reply_markup=main_buttons)
def main():
# Updater o`rnatib olamiz
updater = Updater(TOKEN, use_context=True)
# Dispatcher eventlarni aniqlash uchun
dispatcher = updater.dispatcher
# start kommandasini ushlab qolish
# dispatcher.add_handler(CommandHandler('start', start))
# inline button query
# dispatcher.add_handler(CallbackQueryHandler(inline_callback))
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
STATE_REGION: [
CallbackQueryHandler(inline_callback),
MessageHandler(Filters.regex('^(' + BTN_TODAY + ')$'), calendar_today),
MessageHandler(Filters.regex('^(' + BTN_TOMORROW + ')$'), calendar_tomorrow),
MessageHandler(Filters.regex('^(' + BTN_MONTH + ')$'), calendar_month),
MessageHandler(Filters.regex('^(' + BTN_REGION + ')$'), select_region),
MessageHandler(Filters.regex('^(' + BTN_DUA + ')$'), select_dua)
],
STATE_CALENDAR: [
MessageHandler(Filters.regex('^(' + BTN_TODAY + ')$'), calendar_today),
MessageHandler(Filters.regex('^(' + BTN_TOMORROW + ')$'), calendar_tomorrow),
MessageHandler(Filters.regex('^(' + BTN_MONTH + ')$'), calendar_month),
MessageHandler(Filters.regex('^(' + BTN_REGION + ')$'), select_region),
MessageHandler(Filters.regex('^(' + BTN_DUA + ')$'), select_dua)
],
},
fallbacks=[CommandHandler('start', start)]
)
dispatcher.add_handler(conv_handler)
updater.start_polling()
updater.idle()
main()