Skip to content

Commit

Permalink
Add shorten command to urls plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmaguire committed Apr 9, 2024
1 parent b8fd76c commit bd726b8
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions plugins/urls/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from twisted.internet import defer
from twisted.internet.threads import deferToThread

from cardinal.decorators import regex
from cardinal.decorators import command, help, regex

# Some notes about this regex - it will attempt to capture URLs prefixed by a
# space, a control character (e.g. for formatting), or the beginning of the
Expand Down Expand Up @@ -162,8 +162,23 @@ def shorten_url(self, url):

return response['url']

def close(self, cardinal):
cardinal.event_manager.remove('urls.detection')
@command("shorten")
@help("Syntax: .shorten <url>")
def close(self, cardinal, user, channel, msg):
try:
url = msg.split(" ")[1]
except IndexError:
cardinal.sendMsg(channel, "Syntax: .shorten <url>")
return

try:
url = self.shorten_url("http://example.com")
except Exception as e:
self.logger.exception("Unable to shorten URL: %s" % url)
cardinal.sendMsg(channel, "Error shortening URL")
return

cardinal.sendMsg(channel, "Shortened URL: %s" % url)


entrypoint = URLsPlugin

0 comments on commit bd726b8

Please sign in to comment.