]> us-delaporte.pagekite.me Git - matrix-fridge-bot.git/blob - matrix_fridge_bot/bot.py
prefix for phone keyboards
[matrix-fridge-bot.git] / matrix_fridge_bot / bot.py
1 '''
2 Prefix: !fridge
3
4 Synax:
5 !fridge COMMAND ARGS
6
7 Commands:
8 add - add an item
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
13 '''
14
15 from datetime import datetime as dt
16 import logging
17 import os
18
19 import simplematrixbotlib as botlib
20
21 from .db import Data
22
23 logger = logging.getLogger(__name__)
24 logging.getLogger('nio').setLevel(logging.WARNING)
25
26 MODULE='fridge'
27
28 class FridgeBot():
29 def __init__(self, matrix_server,
30 bot_name,
31 bot_pwd,
32 db_file=None,
33 bot_prefix='!fridge'):
34
35 logging.basicConfig(filename=f'matrix-{MODULE}-bot.log', encoding='utf-8', level=logging.DEBUG)
36
37
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)
42
43 def run(self):
44 self.bot.run()
45
46 async def handle_message(self, room, message):
47 match = botlib.MessageMatch(room, message, self.bot, self.prefix)
48
49 if not match.is_not_from_this_bot() and match.prefix():
50 return
51
52 if match.prefix():
53 try:
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))
62
63
64 async def _handle_match(self, match):
65
66 if match.command('list'):
67 results = ['## Listing fresh items...']
68 for item in self.data.list():
69 results.append(item)
70 results.append('-- End of List --')
71 return results
72
73
74 if match.command('toss'):
75 results = ['## Listing items to toss...']
76 for item in self.data.to_toss():
77 results.append(item)
78 results.append('-- End of List --')
79 return results
80
81
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):
86 results.append(item)
87 results.append('-- End of List --')
88 return results
89
90 if match.command('setup'):
91 results = []
92 results.append('Doing fridge setup...')
93 result = self.data.setup()
94 results.append(str(result))
95 return results
96
97 if match.command('add'):
98 new_fridge = ' '.join(match.args()[1:])
99 result = self.data.add(new_fridge)
100 return [result]
101
102
103 if match.command('remove'):
104 fridge_id = match.args()[1]
105 result = self.data.remove(fridge_id)
106 return [result]
107
108 return [__doc__]
109