Ver código fonte

Implement lazy reply fetching in groupme driver

Kirk Trombley 3 anos atrás
pai
commit
6565813ef8
1 arquivos alterados com 23 adições e 1 exclusões
  1. 23 1
      drivers/groupme_driver.py

+ 23 - 1
drivers/groupme_driver.py

@@ -52,6 +52,28 @@ class GroupMeBot(rollbot.Rollbot[GroupMeMessage]):
                 return None
         return cfg
 
+    def _convert_attachment(self, group_id, att_type, att_body):
+        if att_type == "reply":
+            async def fetch_reply():
+                try:
+                    async with self.context.request.get(
+                        f"https://api.groupme.com/v3/groups/{group_id}/messages",
+                        headers={
+                            "Content-Type": "application/json",
+                        },
+                        params={
+                            "token": groupme_token,
+                            "limit": 1,
+                            "after_id": att_body["base_reply_id"],
+                        },
+                    ) as resp:
+                        msg = (await resp.json())["response"]["messages"][0]
+                    return json.dumps(msg)
+                except:
+                    self.context.logger.exception("Failed to look up attached message")
+            return rollbot.Attachment(att_type, fetch_reply)
+        return rollbot.Attachment(att_type, json.dumps(att_body))
+
     async def parse(self, msg: GroupMeMessage):
         return rollbot.Message(
             origin_id="GROUPME",
@@ -62,7 +84,7 @@ class GroupMeBot(rollbot.Rollbot[GroupMeMessage]):
             channel_admin=msg.sender_id in group_admins.get(msg.group_id, ()),
             sender_name=msg.name,
             text=msg.text,
-            attachments=[rollbot.Attachment(d["type"], json.dumps(d)) for d in msg.attachments],
+            attachments=[self._convert_attachment(msg.group_id, d["type"], d) for d in msg.attachments],
             message_id=msg.id,
         )