]>
us-delaporte.pagekite.me 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 - re-show past purchased items from STORE
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 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
63 .shop zone dairy sour cream
66 if match
.command('setup'):
67 result
= Data().setup()
70 if match
.command('stores'):
71 items
= Data().get_stores()
72 results
= ['List of Stores']
74 results
.append(str(item
))
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
))
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}']
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
)
96 if match
.command('zone'):
97 zone
= match
.args()[1]
98 item
= ' '.join(match
.args()[2:])
99 Data().assign_zone(item
, zone
)
102 if match
.command('assign'):
103 item_id
= int(match
.args()[1])
104 Data().assign_last_store(item_id
)
106 # Now print the list for the assigned store
107 items
= Data().get_last_shopping_list()
108 results
= ['Shopping List']
110 results
.append(str(item
))
113 if match
.command('buy'):
114 items
= ' '.join(match
.args()[1:])
115 results
= Data().purchase_items(items
)
117 # for item in Data().get_last_shopping_list():
118 # results.append(str(item))
121 if match
.command('clear'):
122 store_name
= ' '.join(match
.args()[1:])
123 results
= Data().clear_list(store_name
)
126 if match
.command('clear.all'):
127 store_name
= ' '.join(match
.args()[1:])
128 results
= Data().clear_list(store_name
, clear_all
=True)
131 if match
.command('at'):
132 store_name
= ' '.join(match
.args()[1:])
133 items
= Data().get_shopping_list(store_name
)
134 results
= ['Shopping List']
136 results
.append(str(item
))
139 if match
.command('list'):
140 items
= Data().get_last_shopping_list()
141 results
= ['Shopping List']
143 results
.append(str(item
))
147 if match
.command('past'):
148 store_name
= ' '.join(match
.args()[1:])
149 items
= Data().list_purchases(store_name
)
150 results
= ['Past purchases...']
152 results
.append(str(item
))
156 if match
.command('unsorted'):
157 items
= Data().get_unsorted_list()
158 results
= ['Items assigned to no store']
160 results
.append(str(item
))
163 if match
.command('remove'):
164 item_id
= ' '.join(match
.args()[1:])
165 Data().remove_item(item_id
)
168 if match
.command('remove.store'):
169 store_id
= ' '.join(match
.args()[1:])
170 Data().remove_store(store_id
)
174 if match
.command('extra.help'):
175 return [extended_help
]
177 return [do_shop
.__doc
__]
179 async def shop_bot(room
, message
, bot
):
180 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
182 if not match
.is_not_from_this_bot() and match
.prefix():
187 result_lines
= await do_shop(match
)
188 for line
in result_lines
:
189 await bot
.api
.send_text_message(
190 room
.room_id
, str(line
))
191 except Exception as ex
:
192 for line
in ['You have:', ' no tea', str(ex
)]:
193 await bot
.api
.send_text_message(
194 room
.room_id
, str(line
))