]>
apps.delaporte.us Git - matrix-fridge-bot.git/blob - matrix_fridge_bot/bot.py
14767f3721344f30c2f2b9a383af382107235422
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
)
55 for line
in result_lines
:
56 await self
.bot
.api
.send_text_message(
57 room
.room_id
, str(line
))
58 except Exception as ex
:
59 line
= f
'Ye flask must be load bearing, or something... {str(ex)}'
60 await self
.bot
.api
.send_text_message(
61 room
.room_id
, str(line
))
64 async def _handle_match(self
, match
):
66 if match
.command('list'):
67 results
= ['## Listing fresh items...']
68 for item
in self
.data
.list():
70 results
.append('-- End of List --')
74 if match
.command('toss'):
75 results
= ['## Listing items to toss...']
76 for item
in self
.data
.to_toss():
78 results
.append('-- End of List --')
82 if match
.command('filter'):
83 term
= ' '.join(match
.args()[1:])
84 results
= [f
'## List matching {term}']
85 for item
in self
.data
.filter(term
):
87 results
.append('-- End of List --')
90 if match
.command('setup'):
92 results
.append('Doing fridge setup...')
93 result
= self
.data
.setup()
94 results
.append(str(result
))
97 if match
.command('add'):
98 new_fridge
= ' '.join(match
.args()[1:])
99 result
= self
.data
.add(new_fridge
)
103 if match
.command('remove'):
104 fridge_id
= match
.args()[1]
105 result
= self
.data
.remove(fridge_id
)