Allow setting message type in \!url

This commit is contained in:
Ville Ranki 2020-03-28 22:04:15 +02:00
parent b7bda46261
commit 18fea2501c
2 changed files with 31 additions and 3 deletions

View File

@ -171,10 +171,13 @@ See: https://github.com/taspinar/twitterscraper/tree/master/twitterscraper
#### Url #### Url
Watches all messages in a room and if a url is found tries to fetch it and Watches all messages in a room and if a url is found tries to fetch it and
spit out the title if found. spit out the title if found.
Defaults to off and needs to be activated on every room you want this. Defaults to off and needs to be activated on every room you want this.
You can choose to send titles as notices (as in Matrix spec) or normal
messages (IRC users might prefer this). This is a global setting currently.
Commands: Commands:
* !url status - show current status * !url status - show current status
@ -182,6 +185,8 @@ Commands:
* !url description - spam descriptions * !url description - spam descriptions
* !url both - spam both title and description * !url both - spam both title and description
* !url off - stop spamming * !url off - stop spamming
* !url text - send titles as normal text (must be owner)
* !url notice - sends titles as notices (must be owner)
Example: Example:

View File

@ -21,6 +21,7 @@ class MatrixModule(BotModule):
self.bot = None self.bot = None
self.status = dict() # room_id -> what to do with urls self.status = dict() # room_id -> what to do with urls
self.type = "m.notice" # notice or text
self.STATUSES = { self.STATUSES = {
"OFF": "Not spamming this channel", "OFF": "Not spamming this channel",
@ -87,7 +88,7 @@ class MatrixModule(BotModule):
msg = f"Description: {description}" msg = f"Description: {description}"
if msg is not None: if msg is not None:
await self.bot.send_text(room, msg) await self.bot.send_text(room, msg, self.type)
@lru_cache(maxsize=128) @lru_cache(maxsize=128)
def get_content_from_url(self, url): def get_content_from_url(self, url):
@ -150,10 +151,30 @@ class MatrixModule(BotModule):
) )
return return
# set type to notice
elif len(args) == 1 and args[0] == "notice":
bot.must_be_owner(event)
self.type = "m.notice"
bot.save_settings()
await bot.send_text(
room, "Sending titles as notices from now on."
)
return
# show status
elif len(args) == 1 and args[0] == "text":
bot.must_be_owner(event)
self.type = "m.text"
bot.save_settings()
await bot.send_text(
room, "Sending titles as text from now on."
)
return
# invalid command # invalid command
await bot.send_text( await bot.send_text(
room, room,
"Sorry, I did not understand. I only understand 'title', 'description', 'both' and 'status' commands", "Sorry, I did not understand. See README for command list.",
) )
return return
@ -167,6 +188,8 @@ class MatrixModule(BotModule):
super().set_settings(data) super().set_settings(data)
if data.get("status"): if data.get("status"):
self.status = data["status"] self.status = data["status"]
if data.get("type"):
self.type = data["type"]
def help(self): def help(self):
return "If I see a url in a message I will try to get the title from the page and spit it out" return "If I see a url in a message I will try to get the title from the page and spit it out"