Dec-01-2020, 05:42 PM
Hello, I am trying to better understand the regular expression module, specifically, how to get it to return a string that I can use elsewhere.
Preface to the code:
This is a script for an open source chat server called "Zulip," utilizing their API. The script is working fine until I get a "not None" response, and then I get the following error:
Here's the script:
Preface to the code:
This is a script for an open source chat server called "Zulip," utilizing their API. The script is working fine until I get a "not None" response, and then I get the following error:
Error:unning Snoozebot Bot:
The goal here is to have a snooze for messages, similar to the snooze function on Gmail. When you write
@snoozebot a message for a snooze reminder x minutes, hours, days, etc. from now @snoozebot
first replies by echoing back the requested snooze time interval. X time later, it sends a message to the
stream pinging the users in the snooze request. I am thinking of something like @snoozebot snooze for (x) (time)
for [list of users].
INFO:root:starting message handling...
INFO:root:waiting for next message
Traceback (most recent call last):
File "/home/zulip/.local/bin/zulip-run-bot", line 10, in <module>
sys.exit(main())
File "/home/zulip/.local/lib/python3.7/site-packages/zulip_bots/run.py", line 152, in main
bot_name=bot_name
File "/home/zulip/.local/lib/python3.7/site-packages/zulip_bots/lib.py", line 382, in run_message_handler_for_bot
client.call_on_each_event(event_callback, ['message'])
File "/usr/local/lib/python3.7/dist-packages/zulip/__init__.py", line 680, in call_on_each_event
callback(event)
File "/home/zulip/.local/lib/python3.7/site-packages/zulip_bots/lib.py", line 380, in event_callback
handle_message(event['message'], event['flags'])
File "/home/zulip/.local/lib/python3.7/site-packages/zulip_bots/lib.py", line 371, in handle_message
bot_handler=restricted_client
File "/home/zulip/python-zulip-api/zulip_bots/zulip_bots/bots/snoozebot/snoozebot.py", line 31, in handle_message
match.join(y)
AttributeError: 'list' object has no attribute 'join'
Is my syntax/understanding wrong for how to utilize the join method? I was thinking that match = the string I want, y = the list containing the base string I want to append to, so match.join(y) should join match to the list in y. Or, am I still not getting a string out of regex that can be appended to a list?Here's the script:
from typing import Any, Dict import re class SnoozeBotHandler: def usage(self) -> str: return ''' The goal here is to have a snooze for messages, similar to the snooze function on Gmail. When you write @snoozebot a message for a snooze reminder x minutes, hours, days, etc. from now @snoozebot first replies by echoing back the requested snooze time interval. X time later, it sends a message to the stream pinging the users in the snooze request. I am thinking of something like @snoozebot snooze for (x) (time) for [list of users]. ''' def handle_message(self, message: Dict[str, Any], bot_handler: Any) -> None: # setting up regex pattern here # hopefully this pattern object will work for any iteration of "snooze for x days/weeks/months...will have to test. pattern1 = re.compile(r'(\bday\b)', re.IGNORECASE) match = pattern1.findall(message['content']) # message handling if message['content'] == '': bot_response = "Please specify the **snooze interval** and **users** to be reminded. For help, message me with 'help.'" bot_handler.send_reply(message, bot_response) elif message['content'] == 'help': bot_handler.send_reply(message, self.usage()) else: if pattern1.search(message['content']) != None: y = ["Ok, I'll remind you in "] match.join(y) bot_handler.send_reply(message, y) emoji_name = 'alarm clock' bot_handler.react(message, emoji_name) else: bot_handler.send_reply(message, "Oops, that won't work. Message me with 'help' for help on how to use me.") return handler_class = SnoozeBotHandler