blob: 244ba7ed997f6ceb0c0c49a6d95eeaa2f0674ee0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import simplematrixbotlib as botlib
import os
from dotenv import load_dotenv
import constants
import restaurants
load_dotenv()
HOMESERVER = os.environ.get("HOMESERVER", "")
USERNAME = os.environ.get("USERNAME", "")
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN", "")
TARGET_ROOM_ID = os.environ.get("TARGET_ROOM_ID", "")
creds = botlib.Creds(homeserver=HOMESERVER, username=USERNAME, access_token=ACCESS_TOKEN)
bot = botlib.Bot(creds)
restaurants.init_db()
@bot.listener.on_message_event
async def trigger_responses(room, message):
if message.sender == f"@{USERNAME}":
return
if room.room_id != TARGET_ROOM_ID:
return
if await restaurants.handle_restaurant_command(bot.api, room.room_id, message.sender, message.body):
return
msg_text = message.body.strip().lower()
cleaned = ''.join(ch if ch.isalnum() else ' ' for ch in msg_text)
words = cleaned.split()
normalized = ' ' + ' '.join(words) + ' '
match = None
for key in constants.TRIGGERS:
key_str = str(key).lower()
key_clean = ''.join(ch if ch.isalnum() else ' ' for ch in key_str)
key_norm = ' ' + ' '.join(key_clean.split()) + ' '
if key_norm and key_norm in normalized:
match = key
break
if match:
msg_text = match
await bot.api.send_text_message(room.room_id, f"{constants.TRIGGERS[msg_text]}")
bot.run()
|