]>
apps.delaporte.us Git - matrix-shop-bot.git/blob - matrix_shop_bot/bot.py
1 from datetime
import datetime
as dt
5 import simplematrixbotlib
as botlib
11 DATE_FORMAT
= '%Y-%m-%d'
14 return dt
.now().strftime(DATE_FORMAT
)
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
)
21 async def do_shop(match
):
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
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 rename OLD,NEW - rename an item
57 rename.store OLD,NEW - rename a store
58 remove ITEM_ID - remove a shopping item from the datatbase
59 remove.store STORE_ID - remove a store from the datatbase (requires clearing the list first)
60 setup - run one time database setup
61 unsorted - list all items not assigned to any store
62 zone ZONE ITEM - list ITEM under given ZONE - ZONE may not contain spaces
65 shop zone dairy sour cream
68 if match
.command('setup'):
69 result
= Data().setup()
72 if match
.command('stores'):
73 items
= Data().get_stores()
74 results
= ['List of Stores']
76 results
.append(str(item
))
79 if match
.command('add'):
80 new
= ' '.join(match
.args()[1:])
81 result
= Data().add_item(new
)
82 results
= [f
'Item added with id {result}']
83 for item
in Data().get_last_shopping_list():
84 results
.append(str(item
))
87 if match
.command('add.store'):
88 new
= ' '.join(match
.args()[1:])
89 result
= Data().add_store(new
)
90 return [f
'Store added with id {result}']
92 if match
.command('assign.item.to.store'):
93 item_id
= int(match
.args()[1])
94 store_id
= int(match
.args()[2])
95 Data().assign_store(item_id
, store_id
)
98 if match
.command('zone'):
99 zone
= match
.args()[1]
100 item
= ' '.join(match
.args()[2:])
101 Data().assign_zone(item
, zone
)
104 if match
.command('assign'):
105 item_id
= int(match
.args()[1])
106 Data().assign_last_store(item_id
)
108 # Now print the list for the assigned store
109 items
= Data().get_last_shopping_list()
110 results
= ['Shopping List']
112 results
.append(str(item
))
115 if match
.command('buy'):
116 items
= ' '.join(match
.args()[1:])
117 results
= Data().purchase_items(items
)
119 # for item in Data().get_last_shopping_list():
120 # results.append(str(item))
123 if match
.command('clear'):
124 store_name
= ' '.join(match
.args()[1:])
125 results
= Data().clear_list(store_name
)
128 if match
.command('clear.all'):
129 store_name
= ' '.join(match
.args()[1:])
130 results
= Data().clear_list(store_name
, clear_all
=True)
133 if match
.command('at'):
134 store_name
= ' '.join(match
.args()[1:])
135 items
= Data().get_shopping_list(store_name
)
136 results
= ['Shopping List']
138 results
.append(str(item
))
141 if match
.command('list'):
142 items
= Data().get_last_shopping_list()
143 results
= ['Shopping List']
145 results
.append(str(item
))
149 if match
.command('past'):
150 store_name
= ' '.join(match
.args()[1:])
151 items
= Data().list_purchases(store_name
)
152 results
= [f
'# Previously purchased at {store_name}']
154 results
.append(str(item
))
158 if match
.command('unsorted'):
159 items
= Data().get_unsorted_list()
160 results
= ['Items assigned to no store']
162 results
.append(str(item
))
165 if match
.command('rename'):
166 old
,new
= ' '.join(match
.args()[1:]).split(',')
167 Data().rename_item(old
,new
)
170 if match
.command('rename.store'):
171 old
,new
= ' '.join(match
.args()[1:]).split(',')
172 Data().rename_store(old
,new
)
175 if match
.command('remove'):
176 item_id
= ' '.join(match
.args()[1:])
177 Data().remove_item(item_id
)
180 if match
.command('remove.store'):
181 store_id
= ' '.join(match
.args()[1:])
182 Data().remove_store(store_id
)
186 if match
.command('extra.help'):
187 return [extended_help
]
189 return [do_shop
.__doc
__]
191 async def shop_bot(room
, message
, bot
):
192 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
194 if not match
.is_not_from_this_bot() and match
.prefix():
199 result_lines
= await do_shop(match
)
202 for line
in result_lines
:
203 message_lines
.append(str(line
))
205 message
= '\n'.join(message_lines
)
207 await bot
.api
.send_text_message(
208 room
.room_id
, message
)
210 except Exception as ex
:
211 for line
in ['You have:', ' no tea', str(ex
)]:
212 await bot
.api
.send_text_message(
213 room
.room_id
, str(line
))