]> apps.delaporte.us Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
Change prefix to shop
[matrix-shop-bot.git] / matrix_shop_bot / bot.py
1 from datetime import datetime as dt
2 import logging
3 import os
4
5 import simplematrixbotlib as botlib
6
7 from .db import Data
8
9 MODULE='shop'
10
11 DATE_FORMAT = '%Y-%m-%d'
12
13 def today():
14 return dt.now().strftime(DATE_FORMAT)
15
16 PREFIX = f'{MODULE}'
17 logger = logging.getLogger(__name__)
18 logging.basicConfig(filename=f'{MODULE}.log', encoding='utf-8', level=logging.DEBUG)
19 logging.getLogger('nio').setLevel(logging.WARNING)
20
21 async def do_shop(match):
22 '''
23 Prefix: shop
24
25 Synax:
26 shop COMMAND ARGS
27
28 Commands:
29 help - show this help
30 extra.help - show extended help
31 add - add an item to shop for
32 add.store - add a store
33 at STORE_ID - use this store for future commands
34 buy ID ID ID - mark items as purchased
35 clear STORE - clear all purchased items for STORE
36 list - re-show list from last used store
37 past STORE - show purchased items from STORE in last 90 days
38 stores - list all stores
39
40 Examples:
41 shop past costco
42 shop at costco
43
44 shop add sour cream
45 shop add milk
46
47 shop list
48
49 shop buy 1 2 5 7
50
51 '''
52 extended_help = '''
53 assign ITEM_ID - assign item to last used store id
54 assign.item.to.store ITEM_ID STORE_ID - plan to shop for at store for item
55 clear.all STORE - clear **all** items for STORE, including un-purchased
56 remove ITEM_ID - remove a shopping item from the datatbase
57 remove.store STORE_ID - remove a store from the datatbase (requires clearing the list first)
58 setup - run one time database setup
59 unsorted - list all items not assigned to any store
60 zone ZONE ITEM - list ITEM under given ZONE
61
62 Example:
63 shop zone dairy sour cream
64 '''
65
66 if match.command('setup'):
67 result = Data().setup()
68 return [result]
69
70 if match.command('stores'):
71 items = Data().get_stores()
72 results = ['List of Stores']
73 for item in items:
74 results.append(str(item))
75 return results
76
77 if match.command('add'):
78 new = ' '.join(match.args()[1:])
79 result = Data().add_item(new)
80 results = [f'Item added with id {result}']
81 for item in Data().get_last_shopping_list():
82 results.append(str(item))
83 return results
84
85 if match.command('add.store'):
86 new = ' '.join(match.args()[1:])
87 result = Data().add_store(new)
88 return [f'Store added with id {result}']
89
90 if match.command('assign.item.to.store'):
91 item_id = int(match.args()[1])
92 store_id = int(match.args()[2])
93 Data().assign_store(item_id, store_id)
94 return ['Success']
95
96 if match.command('zone'):
97 zone = match.args()[1]
98 item = ' '.join(match.args()[2:])
99 Data().assign_zone(item, zone)
100 return ['Success']
101
102 if match.command('assign'):
103 item_id = int(match.args()[1])
104 Data().assign_last_store(item_id)
105
106 # Now print the list for the assigned store
107 items = Data().get_last_shopping_list()
108 results = ['Shopping List']
109 for item in items:
110 results.append(str(item))
111 return results
112
113 if match.command('buy'):
114 items = ' '.join(match.args()[1:])
115 results = Data().purchase_items(items)
116 # results = []
117 # for item in Data().get_last_shopping_list():
118 # results.append(str(item))
119 return results
120
121 if match.command('clear'):
122 store_name = ' '.join(match.args()[1:])
123 results = Data().clear_list(store_name)
124 return results
125
126 if match.command('clear.all'):
127 store_name = ' '.join(match.args()[1:])
128 results = Data().clear_list(store_name, clear_all=True)
129 return results
130
131 if match.command('at'):
132 store_name = ' '.join(match.args()[1:])
133 items = Data().get_shopping_list(store_name)
134 results = ['Shopping List']
135 for item in items:
136 results.append(str(item))
137 return results
138
139 if match.command('list'):
140 items = Data().get_last_shopping_list()
141 results = ['Shopping List']
142 for item in items:
143 results.append(str(item))
144 return results
145
146
147 if match.command('past'):
148 store_name = ' '.join(match.args()[1:])
149 items = Data().list_purchases(store_name)
150 results = [f'# Previously purchased at {store_name}']
151 for item in items:
152 results.append(str(item))
153 return results
154
155
156 if match.command('unsorted'):
157 items = Data().get_unsorted_list()
158 results = ['Items assigned to no store']
159 for item in items:
160 results.append(str(item))
161 return results
162
163 if match.command('remove'):
164 item_id = ' '.join(match.args()[1:])
165 Data().remove_item(item_id)
166 return ['Success']
167
168 if match.command('remove.store'):
169 store_id = ' '.join(match.args()[1:])
170 Data().remove_store(store_id)
171 return ['Success']
172
173
174 if match.command('extra.help'):
175 return [extended_help]
176
177 return [do_shop.__doc__]
178
179 async def shop_bot(room, message, bot):
180 match = botlib.MessageMatch(room, message, bot, PREFIX)
181
182 if not match.is_not_from_this_bot() and match.prefix():
183 return
184
185 try:
186 if match.prefix():
187 result_lines = await do_shop(match)
188
189 message_lines = []
190 for line in result_lines:
191 message_lines.append(str(line))
192
193 message = '\n'.join(message_lines)
194
195 await bot.api.send_text_message(
196 room.room_id, message)
197
198 except Exception as ex:
199 for line in ['You have:', ' no tea', str(ex)]:
200 await bot.api.send_text_message(
201 room.room_id, str(line))
202
203
204