Przeglądaj źródła

Implement imgur image upload as fallback

Kirk Trombley 3 lat temu
rodzic
commit
983507e3e7
1 zmienionych plików z 28 dodań i 2 usunięć
  1. 28 2
      drivers/groupme_driver.py

+ 28 - 2
drivers/groupme_driver.py

@@ -25,6 +25,8 @@ groupme_token = secrets["groupme"]["token"]
 replies_enabled = secrets["groupme"].get("replies_enabled", True)
 groupme_admins = secrets["admins"]["origin"]
 group_admins = secrets["admins"]["channel"]
+imgur_fallback = secrets["groupme"].get("imgur_fallback", False)
+imgur_token = secrets["imgur"]["token"] if imgur_fallback else None
 
 max_msg_len = 1000
 split_text = "\n..."
@@ -89,7 +91,18 @@ class GroupMeBot(rollbot.Rollbot[GroupMeMessage]):
             message_id=msg.id,
         )
 
-    async def upload_image(self, data: bytes):
+    async def upload_image(self, data: bytes) -> (str, str):
+        try:
+            return await self.upload_image_groupme(data), ""
+        except:
+            self.context.logger.exception("GroupMe image upload failure")
+            if not imgur_fallback:
+                self.context.logger.info("Imgur fallback disabled")
+                raise
+            self.context.logger.info("Attempting imgur fallback")
+            return await self.upload_image_imgur(data), "GroupMe rejected image upload, falling back to Imgur...\n"
+
+    async def upload_image_groupme(self, data: bytes) -> str:
         async with self.context.request.post(
             "https://image.groupme.com/pictures",
             headers={
@@ -101,6 +114,17 @@ class GroupMeBot(rollbot.Rollbot[GroupMeMessage]):
             upload.raise_for_status()
             return (await upload.json())["payload"]["url"]
 
+    async def upload_image_imgur(self, data: bytes) -> str:
+        async with self.context.request.post(
+            "https://api.imgur.com/3/image",
+            headers={
+                "Authorization": f"Client-ID {imgur_token}",
+            },
+            data=data,
+        ) as upload:
+            upload.raise_for_status()
+            return (await upload.json())["data"]["link"]
+
     async def post_message(self, bot_id: str, text: str, attachments: list[dict[str, str]]):
         body = {
             "bot_id": bot_id,
@@ -136,12 +160,14 @@ class GroupMeBot(rollbot.Rollbot[GroupMeMessage]):
                 for att in res.attachments:
                     if att.name == "image":
                         if isinstance(att.body, bytes):
+                            url, note = await self.upload_image(att.body)
                             attachments.append(
                                 {
                                     "type": "image",
-                                    "url": await self.upload_image(att.body),
+                                    "url": url,
                                 }
                             )
+                            message += note
                         else:
                             attachments.append({"type": "image", "url": att.body})
                     if replies_enabled and att.name == "reply":