]>
apps.delaporte.us Git - matrix-fridge-bot.git/blob - matrix_fridge_bot/bot.py
9 list - print the list of fresh items
10 remove - remove an item
11 to.toss - print the list of items to toss
12 filter TERM - list all items that include TERM
15 from datetime
import datetime
as dt
19 import simplematrixbotlib
as botlib
23 logger
= logging
.getLogger(__name__
)
24 logging
.getLogger('nio').setLevel(logging
.WARNING
)
29 def __init__(self
, matrix_server
,
33 bot_prefix
='.fridge'):
35 logging
.basicConfig(filename
=f
'matrix-{MODULE}-bot.log', encoding
='utf-8', level
=logging
.DEBUG
)
38 creds
= botlib
.Creds(matrix_server
, bot_name
, bot_pwd
)
39 self
.bot
= botlib
.Bot(creds
)
40 self
.prefix
= bot_prefix
41 self
.data
= Data(db_file
=db_file
)
46 async def handle_message(self
, room
, message
):
47 match
= botlib
.MessageMatch(room
, message
, self
.bot
, self
.prefix
)
49 if not match
.is_not_from_this_bot() and match
.prefix():
54 result_lines
= await self
._handle
_match
(match
)
57 for line
in result_lines
:
58 message_lines
.append(str(line
))
60 message
= '\n'.join(message_lines
)
62 await self
.bot
.api
.send_text_message(
63 room
.room_id
, message
)
65 except Exception as ex
:
66 line
= f
'Ye flask must be load bearing, or something... {str(ex)}'
67 await self
.bot
.api
.send_text_message(
68 room
.room_id
, str(line
))
71 async def _handle_match(self
, match
):
73 if match
.command('list'):
74 results
= ['## Listing fresh items...']
75 for item
in self
.data
.list():
77 results
.append('-- End of List --')
81 if match
.command('toss'):
82 results
= ['## Listing items to toss...']
83 for item
in self
.data
.to_toss():
85 results
.append('-- End of List --')
89 if match
.command('filter'):
90 term
= ' '.join(match
.args()[1:])
91 results
= [f
'## List matching {term}']
92 for item
in self
.data
.filter(term
):
94 results
.append('-- End of List --')
97 if match
.command('setup'):
99 results
.append('Doing fridge setup...')
100 result
= self
.data
.setup()
101 results
.append(str(result
))
104 if match
.command('add'):
105 new_fridge
= ' '.join(match
.args()[1:])
106 result
= self
.data
.add(new_fridge
)
110 if match
.command('remove'):
111 fridge_id
= match
.args()[1]
112 result
= self
.data
.remove(fridge_id
)