-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
367 lines (324 loc) · 17.3 KB
/
bot.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import os
from core.database import Database
from core.seed import create_tables
from telegram.ext import Updater, MessageHandler, CommandHandler, CallbackQueryHandler, Filters
from barcode import Code128
from barcode.writer import ImageWriter
from dotenv import load_dotenv
from core.clients import Clients, Client
from core.items import Items, Item
from core.purchases import Purchases
class CoasterBotHandler:
def __init__(self, database, admin_telegram_id):
self.database = database
self.admin_telegram_id = admin_telegram_id
self.clients = Clients(self.database)
self.items = Items(self.database)
self.purchases = Purchases(self.database)
def is_admin(self, update, context):
if int(update.message.from_user.id) != int(self.admin_telegram_id):
context.bot.send_message(chat_id=update.effective_chat.id,
text="Only admin is allowed to perform this action.")
return False
return True
def add_product(self, update, context):
if not self.is_admin(update, context):
return
command_split = update.message.text.split(" ")
if len(command_split) < 5:
context.bot.send_message(chat_id=update.effective_chat.id,
text="format: /add_product product description EAN_code price")
return
product_name = command_split[1].strip()
product_description = command_split[2].strip()
product_ean = command_split[3].strip()
product_price = command_split[4].strip()
product = Item.create(product_name, product_description, product_ean, product_price)
self.items.persist(product)
context.bot.send_message(chat_id=update.effective_chat.id,
text="The product {} is added.".format(product_name))
def remove_product(self, update, context):
if not self.is_admin(update, context):
return
command_split = update.message.text.split(" ")
if len(command_split) < 2:
context.bot.send_message(chat_id=update.effective_chat.id,
text="format: /remove_product productname")
return
product_name = command_split[1].strip()
product = self.items.get_by_item_name(product_name)
if not product:
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"The product with name {product_name} is not found.")
self.items.remove(product.item_id)
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"The product {product_name} is removed.")
def change_price(self, update, context):
if not self.is_admin(update, context):
return
command_split = update.message.text.split(" ")
if len(command_split) < 3:
context.bot.send_message(chat_id=update.effective_chat.id,
text="format: /change_price productname new_price")
return
product_name = command_split[1].strip()
product_new_price = command_split[2].strip()
product = self.items.get_by_item_name(product_name)
product.price = product_new_price
self.items.persist(product)
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"The product {product_name}'s new price is {product_new_price}.")
def get_balance(self, update, context):
telegram_id = update.message.from_user.id
client = self.clients.get_by_telegram_id(telegram_id)
if client:
context.bot.send_message(chat_id=update.effective_chat.id,
text="Hello, {}, your balance is: {}".format(client.nickname,
round(client.balance, 2)))
else:
context.bot.send_message(chat_id=update.effective_chat.id,
text="No client with telegram id {} can be found".format(telegram_id))
def reset_balance(self, update, context):
if not self.is_admin(update, context):
return
command_split = update.message.text.split(" ")
if len(command_split) < 2:
context.bot.send_message(chat_id=update.effective_chat.id,
text="Format: /reset_balance nickname")
return
client_name = command_split[1]
client = self.clients.get_by_nickname(client_name)
if client is not None:
client.balance = 0
self.clients.persist(client)
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"Balance of {client_name} is reset.")
else:
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"No client with name {client_name}.")
def create_client(self, update, context):
if not self.is_admin(update, context):
return
command_split = update.message.text.split(" ")
if len(command_split) < 3:
context.bot.send_message(chat_id=update.effective_chat.id,
text="Format: /create_client nickname telegram_id")
return
client_name = command_split[1]
client_telegram_id = command_split[2]
new_client = Client.create(client_name, client_telegram_id)
self.clients.persist(new_client)
context.bot.send_message(chat_id=update.effective_chat.id,
text="Hello, {}! Have a drink, have a snack, it's all on you tonight!".format(
new_client.nickname))
def delete_client(self, update, context):
if not self.is_admin(update, context):
return
command_split = update.message.text.split(" ")
if len(command_split) < 2:
context.bot.send_message(chat_id=update.effective_chat.id,
text="Format: /delete_client nickname")
return
client_name = command_split[1]
client = self.clients.get_by_nickname(client_name)
if not client:
context.bot.send_message(chat_id=update.effective_chat.id,
text="No client with nickname {}".format(client_name))
return
if client.balance > 0:
context.bot.send_message(chat_id=update.effective_chat.id,
text="Cannot delete a client with a positive balance.".format(client_name))
return
self.clients.remove(client_name)
context.bot.send_message(chat_id=update.effective_chat.id,
text="Client with nickname {} is deleted.".format(
client_name))
@staticmethod
def get_telegram_id(update, context):
telegram_id = update.message.from_user.id
context.bot.send_message(chat_id=update.effective_chat.id,
text="Your telegram ID is: {}".format(telegram_id))
def get_barcode(self, update, context):
telegram_id = update.message.from_user.id
client = self.clients.get_by_telegram_id(telegram_id)
if client is not None:
filename = "barcodes/{}.png".format(client.barcode)
if not os.path.isfile(filename):
code = Code128(client.barcode, writer=ImageWriter())
code.save("barcodes/{}".format(client.barcode))
context.bot.send_photo(chat_id=update.effective_chat.id,
photo=open("barcodes/{}.png".format(client.barcode), "rb"))
else:
context.bot.send_message(chat_id=update.effective_chat.id,
text="You currently are not a client.")
def list_stock(self, update, context):
if not self.is_admin(update, context):
return
items_list = self.items.list()
if not len(items_list):
context.bot.send_message(chat_id=update.effective_chat.id,
text="No products yet.")
return
context.bot.send_message(chat_id=update.effective_chat.id,
text="\n".join(["{}: {}".format(item.name, item.stock) for item in items_list]))
def list_balances(self, update, context):
if not self.is_admin(update, context):
return
client_list = self.clients.list()
if not len(client_list):
context.bot.send_message(chat_id=update.effective_chat.id,
text="No clients yet.")
return
context.bot.send_message(chat_id=update.effective_chat.id,
text="\n".join([f"Client {client.nickname}: {round(client.balance, 2)}" for client in
client_list]))
def list_purchases(self, update, context):
telegram_id = update.message.from_user.id
client = self.clients.get_by_telegram_id(telegram_id)
if client is not None:
purchases_by_client = self.purchases.get_by_user_name(client.nickname)
# TODO: Limit to a specific amount
if not len(purchases_by_client):
context.bot.send_message(chat_id=update.effective_chat.id,
text="You currently don't have any purchases.")
return
context.bot.send_message(chat_id=update.effective_chat.id,
text="\n".join([
f"{purchase.timestamp[:purchase.timestamp.find('T')]}:"
f" {purchase.item_name}, {purchase.paid_price}"
for
purchase in purchases_by_client]))
else:
context.bot.send_message(chat_id=update.effective_chat.id,
text="You currently are not a client.")
def get_item_barcode(self, update, context):
command_split = update.message.text.split(" ")
if len(command_split) < 2:
context.bot.send_message(chat_id=update.effective_chat.id,
text="Format: /get_item_barcode item")
return
item_name = command_split[1]
item = self.items.get_by_item_name(item_name)
if item is not None:
filename = "barcodes/{}.png".format(item.barcode)
if not os.path.isfile(filename):
code = Code128(item.barcode, writer=ImageWriter())
code.save("barcodes/{}".format(item.barcode))
context.bot.send_photo(chat_id=update.effective_chat.id,
photo=open("barcodes/{}.png".format(item.barcode), "rb"))
else:
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"The item with name {item_name} currently does not exist.")
def list_item_prices(self, update, context):
items_list = self.items.list()
if not len(items_list):
context.bot.send_message(chat_id=update.effective_chat.id,
text="No products yet.")
return
context.bot.send_message(chat_id=update.effective_chat.id,
text="\n".join(["{}: {}".format(item.name, item.price) for item in items_list]))
def add_stock(self, update, context):
if not self.is_admin(update, context):
return
command_split = update.message.text.split(" ")
if len(command_split) < 3:
context.bot.send_message(chat_id=update.effective_chat.id,
text="Format: /add_stock product_name quantity")
return
item_name = command_split[1]
item_add_stock = int(command_split[2])
item = self.items.get_by_item_name(item_name)
if not item:
context.bot.send_message(chat_id=update.effective_chat.id,
text="No product with name {} was found.".format(item_name))
item.stock += item_add_stock
self.items.persist(item)
context.bot.send_message(chat_id=update.effective_chat.id,
text="Item {} has current stock: {}".format(item.name, item.stock))
def remove_stock(self, update, context):
if not self.is_admin(update, context):
return
command_split = update.message.text.split(" ")
if len(command_split) < 3:
context.bot.send_message(chat_id=update.effective_chat.id,
text="Format: /remove_stock product_name quantity")
return
item_name = command_split[1]
item_remove_stock = int(command_split[2])
item = self.items.get_by_item_name(item_name)
if not item:
context.bot.send_message(chat_id=update.effective_chat.id,
text="No product with name {} was found.".format(item_name))
item.stock -= item_remove_stock
self.items.persist(item)
context.bot.send_message(chat_id=update.effective_chat.id,
text="Item {} has current stock: {}".format(item.name, item.stock))
@staticmethod
def help(update, context):
public_commands = ["/balance", "/telegram_id", "/get_barcode", "/get_item_barcode", "/list_purchases", "/list_prices", "/help"]
admin_commands = ["/add_product", "/create_client", "/delete_client", "/list_stock", "/add_stock", "/reset_balance",
"/remove_product", "/list_balances", "/remove_stock", "/change_price"]
context.bot.send_message(chat_id=update.effective_chat.id,
text="Current public commands are: {}\nCurrent admin-only commands are: {}".format(
", ".join(public_commands), ", ".join(admin_commands)))
@staticmethod
def unknown(update, context):
context.bot.send_message(chat_id=update.effective_chat.id,
text="Sorry I don't know this command. "
"Enter /help for more information on how to use this bot.")
def run(self):
# Updater and dispatcher for Telegram bot
updater = Updater(os.getenv("telegram_token"), use_context=True)
dispatcher = updater.dispatcher
# Add handlers for commands
add_client_handler = CommandHandler('create_client', self.create_client)
dispatcher.add_handler(add_client_handler)
delete_client_handler = CommandHandler('delete_client', self.delete_client)
dispatcher.add_handler(delete_client_handler)
get_barcode_handler = CommandHandler('get_barcode', self.get_barcode)
dispatcher.add_handler(get_barcode_handler)
add_product_handler = CommandHandler('add_product', self.add_product)
dispatcher.add_handler(add_product_handler)
balance_handler = CommandHandler('balance', self.get_balance)
dispatcher.add_handler(balance_handler)
telegram_id_handler = CommandHandler('telegram_id', self.get_telegram_id)
dispatcher.add_handler(telegram_id_handler)
list_stock_handler = CommandHandler('list_stock', self.list_stock)
dispatcher.add_handler(list_stock_handler)
add_stock_handler = CommandHandler('add_stock', self.add_stock)
dispatcher.add_handler(add_stock_handler)
reset_balance_handler = CommandHandler('reset_balance', self.reset_balance)
dispatcher.add_handler(reset_balance_handler)
remove_product_handler = CommandHandler('remove_product', self.remove_product)
dispatcher.add_handler(remove_product_handler)
change_price_handler = CommandHandler('change_price', self.change_price)
dispatcher.add_handler(change_price_handler)
list_balances_handler = CommandHandler('list_balances', self.list_balances)
dispatcher.add_handler(list_balances_handler)
list_purchases_handler = CommandHandler('list_purchases', self.list_purchases)
dispatcher.add_handler(list_purchases_handler)
item_barcode_handler = CommandHandler('get_item_barcode', self.get_item_barcode)
dispatcher.add_handler(item_barcode_handler)
remove_stock_handler = CommandHandler('remove_stock', self.remove_stock)
dispatcher.add_handler(remove_stock_handler)
list_prices_handler = CommandHandler('list_prices', self.list_item_prices)
dispatcher.add_handler(list_prices_handler)
help_handler = CommandHandler('help', self.help)
dispatcher.add_handler(help_handler)
# Unknown command catchall handler
unknown_handler = MessageHandler(Filters.command, self.unknown)
dispatcher.add_handler(unknown_handler)
# To poll for incoming commands
updater.start_polling()
if __name__ == "__main__":
# Load environment variables
load_dotenv()
# Create database, retrieve admin id
db = Database(os.getenv("db_file"))
admin_id = os.getenv("admin_telegram_id")
# Make sure all tables are instantiated
create_tables(db)
# Create bot handler
handler = CoasterBotHandler(db, admin_id)
# Run the bot
handler.run()