-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegbot.py
173 lines (138 loc) · 6.02 KB
/
telegbot.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
import logging
import os
import subprocess
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
import apifetch
import dbase
# adding a logger to monitor crashes and easier debugging
LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(filename='./telegbot.log',
format=LOG_FORMAT,
filemode='w',
level=logging.DEBUG)
logger = logging.getLogger()
global baseurl_g
baseurl_g = "http://ka2yab.com:8000/api/"
def get_token():
token = os.getenv("FORUSHANDE_BOT")
if token is None or not token:
token = subprocess.call(["echo", "$FORUSHANDE_BOT"])
if token:
print(token)
return token
# raise Exception("Err: shell variable not fonud")
def start(bot, update):
update.message.reply_text('خوش آمدید!')
logger.info("start command used by {} "
.format(update.message.from_user.first_name))
logger.debug("new user << {} >>started the bot"
.format(update.message.from_user))
reply_markup = parents_menu(bot, update)
logger.debug("a keyboard was generated from categories")
update.message.reply_text('Please choose a category:',
reply_markup=reply_markup)
logger.info("message with keyboard was sent")
def help(bot, update):
update.message.reply_text(
'Help\n'
'dear ` {} `,\n'
'here are the commands for this bot:\n'
'/start - starts the bot\n'
'/help - shows this message\n'
'/hell - go to hell\n'.format(update.message.from_user.first_name))
logger.debug("help message is set")
logger.info("help command used by {}"
.format(update.message.from_user.first_name))
def build_menu(buttons,
n_cols,
header_buttons=None,
footer_buttons=None):
"""
:param buttons: a list of buttons.
:param n_cols: how many columns to show the butt,ons in
:param header_buttons: list of buttons appended to the beginning
:param footer_buttons: list of buttons added to the end
:return: the menu
"""
menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
print(buttons)
logger.debug("buttons created")
if header_buttons:
menu.insert(0, header_buttons)
if footer_buttons:
menu.append(footer_buttons)
logger.debug("header and footer buttons added")
print(InlineKeyboardButton(menu))
return InlineKeyboardMarkup(menu)
def button_parent(bot, update):
query = update.callback_query
logger.debug("a query was sent to parents Qhandler {}".format(query.data))
baseurl = baseurl_g
suburl = "category/subs/all/{}".format(query.data[5:])
child_categories = apifetch.fetch_json(baseurl, suburl)
cat_names, cat_menu = gen_category(child_categories, "name", "id", "caid:")
reply_markup = build_menu(cat_menu, n_cols=3)
logger.debug("query handler parent built a menu")
bot.send_message(text="Selected: %s" % query.data[5:],
chat_id=query.message.chat_id,
reply_markup=reply_markup,
parse_mode='HTML')
query.answer()
logger.debug("callback query handled by button_parent")
def button_category(bot, update):
query = update.callback_query
logger.debug("a query was sent for category qhandler {}".format(query.data))
baseurl = baseurl_g
suburl = "category/products/{}".format(query.data[5:])
products = apifetch.fetch_json(baseurl, suburl)
product_names, product_menu = gen_category(products, "name", "id", "prid:",
url="http://www.ka2yab.com/products/{}")
baseurl = baseurl_g
suburl = "category/subs/all/{}".format(query.data[5:])
sub_cats = apifetch.fetch_json(baseurl, suburl)
cat_names, cats_menu = gen_category(sub_cats, "name", "id", "caid:")
reply_markup = build_menu(product_menu, n_cols=1, header_buttons=cats_menu)
bot.send_message(text="there are Sub categories and products here:",
chat_id=query.message.chat_id,
reply_markup=reply_markup,
parse_mode='HTML')
query.answer()
logger.debug("callback query handled by button_parent")
def button_more(bot, update):
query = update.callback_query
data_base = dbase.start_redis(db=2)
more_buttons = data_base.get("more_buttons")
query.edit_message_reply_markup(reply_markup=more_buttons)
logger.debug("callback query handled by button_more")
def parents_menu(bot, update):
categories = apifetch.fetch_json(baseurl_g,
"category/parents")
# TODO: implement fetch from database instead of url
logger.debug("update categories requested!")
option_btn = 'name'
callback = 'id'
parent_names, button_list = gen_category(categories, option_btn,
callback, "paid:")
if len(parent_names) < 6:
reply_markup = build_menu(button_list, n_cols=3)
else:
show_more = InlineKeyboardButton("بیشتر...",
callback_data="more_categories")
button_rest = button_list[6:]
del button_list[6:]
reply_markup = build_menu(button_list, n_cols=3,
footer_buttons=[show_more])
logger.debug("reply keyboard for category was returned")
return reply_markup
def gen_category(categories, buttonfield,
callbackfield, callbackheader, url=""):
cat_names = []
for item in categories:
print(item)
cat_names.append(item[buttonfield])
logger.info("generated a list from the name of categories; {}"
.format(cat_names))
button_list = [InlineKeyboardButton(s, url=url.format(str(categories[cat_names.index(s)][callbackfield])),
callback_data=callbackheader + str(categories[cat_names.index(s)][callbackfield]))
for s in cat_names]
return cat_names, button_list