From f1f7c332a3a331db8ebed6d76881a780b17fbbd1 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Mon, 25 Mar 2024 01:28:24 +0100 Subject: [PATCH] Added remote image fetch example --- docs/example_plugins/basic.py | 7 +++- docs/example_plugins/comic.py | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/docs/example_plugins/basic.py b/docs/example_plugins/basic.py index 9bc03f0..03f0fc9 100644 --- a/docs/example_plugins/basic.py +++ b/docs/example_plugins/basic.py @@ -29,8 +29,11 @@ class BasicCommandPlugin(SidebandCommandPlugin): response_content, lxm.source_hash, False, # Don't use propagation by default, try direct first - skip_fields=True, # Don't include any additional fields automatically - no_display=True # Dot't display this message in the message stream + skip_fields = True, # Don't include any additional fields automatically + no_display = True, # Don't display this message in the message stream + attachment = None, # Don't add any attachment field to this message + image = None, # Don't add any image field to this message + audio = None, # Don't add any audio field to this message ) # Finally, tell Sideband what class in this diff --git a/docs/example_plugins/comic.py b/docs/example_plugins/comic.py index e69de29..f475ffa 100644 --- a/docs/example_plugins/comic.py +++ b/docs/example_plugins/comic.py @@ -0,0 +1,70 @@ +import io +import RNS +import requests +from PIL import Image as PilImage + +class ComicCommandPlugin(SidebandCommandPlugin): + command_name = "comic" + + def start(self): + # Do any initialisation work here + RNS.log("Comic command plugin example starting...") + + # And finally call start on superclass + super().start() + + def stop(self): + # Do any teardown work here + pass + + # And finally call stop on superclass + super().stop() + + def handle_command(self, arguments, lxm): + comic_source = "https://imgs.xkcd.com/comics/tsp_vs_tbsp.png" + response_content = f"The source for this comic is:\n{comic_source}" + + try: + image_request = requests.get(comic_source, stream=True) + if image_request.status_code == 200: + max_size = 320, 320 + with PilImage.open(io.BytesIO(image_request.content)) as im: + im.thumbnail(max_size) + buf = io.BytesIO() + im.save(buf, format="webp", quality=22) + image_field = ["webp", buf.getvalue()] + + # Send the fetched comic as a message + self.get_sideband().send_message( + response_content, + lxm.source_hash, + False, # Don't use propagation by default, try direct first + skip_fields = True, # Don't include any additional fields automatically + no_display = True, # Don't display this message in the message stream + image = image_field, # Add the scaled and compressed image + ) + + else: + # Send an error message + self.get_sideband().send_message( + "The specified comic could not be downloaded", + lxm.source_hash, + False, # Don't use propagation by default, try direct first + skip_fields = True, # Don't include any additional fields automatically + no_display = True, # Don't display this message in the message stream + ) + + except Exception as e: + # Send an error message + self.get_sideband().send_message( + "An error occurred while trying to fetch the specified comic:\n\n"+str(e), + lxm.source_hash, + False, # Don't use propagation by default, try direct first + skip_fields = True, # Don't include any additional fields automatically + no_display = True, # Don't display this message in the message stream + ) + + +# Finally, tell Sideband what class in this +# file is the actual plugin class. +plugin_class = ComicCommandPlugin \ No newline at end of file