浏览代码

Add message_id as optional field of Message object, add support for tuple returns

Kirk Trombley 3 年之前
父节点
当前提交
503f476436
共有 4 个文件被更改,包括 19 次插入8 次删除
  1. 2 2
      commands/commands/simple.py
  2. 6 5
      drivers/groupme_driver.py
  3. 9 0
      rollbot/rollbot/decorators/as_command.py
  4. 2 1
      rollbot/rollbot/types.py

+ 2 - 2
commands/commands/simple.py

@@ -157,5 +157,5 @@ def finchat(cmd: Command):
 
 
 @as_command
-def reply():
-    return Attachment(name="reply", body="Replying!")
+def reply(msg: Message):
+    return "Replying!", Attachment(name="reply", body=msg.message_id)

+ 6 - 5
drivers/groupme_driver.py

@@ -30,6 +30,7 @@ msg_slice = max_msg_len - len(split_text)
 
 
 class GroupMeMessage(BaseModel):
+    id: str
     sender_id: str
     group_id: str
     name: str
@@ -61,6 +62,7 @@ class GroupMeBot(rollbot.Rollbot[GroupMeMessage]):
             sender_name=msg.name,
             text=msg.text,
             attachments=[rollbot.Attachment(d["type"], json.dumps(d)) for d in msg.attachments],
+            message_id=msg.id,
         )
 
     async def upload_image(self, data: bytes):
@@ -118,13 +120,12 @@ class GroupMeBot(rollbot.Rollbot[GroupMeMessage]):
                         else:
                             attachments.append({"type": "image", "url": att.body})
                     if att.name == "reply":
-                        if not isinstance(att.body, str):
-                            raise ValueError("Invalid reply body type")
-                        message += att.body # append reply text to response
+                        if att.body is None or not isinstance(att.body, str):
+                            raise ValueError("Invalid reply body type, must be message ID")
                         attachments.append({
                             "type": "reply",
-                            "base_reply_id": res.channel_id,
-                            "reply_id": res.channel_id,
+                            "base_reply_id": att.body,
+                            "reply_id": att.body,
                         })
                          
         except:

+ 9 - 0
rollbot/rollbot/decorators/as_command.py

@@ -76,9 +76,18 @@ def _get_injectors(fn: Callable[..., Any]) -> list[Injector]:
     return injectors
 
 
+def _is_valid_tuple_response(result: Any):
+    return (isinstance(result, tuple) and 
+            len(result) > 0 and 
+            isinstance(result[0], str) and 
+            all(isinstance(a, Attachment) for a in result[1:]))
+
+
 def _make_response(message: Message, result: Any) -> Response:
     if result is None or isinstance(result, Response):
         return result
+    if _is_valid_tuple_response(result):
+        return Response.from_message(message, text=result[0], attachments=list(result[1:]))
     if isinstance(result, str):
         return Response.from_message(message, text=result)
     if isinstance(result, Attachment):

+ 2 - 1
rollbot/rollbot/types.py

@@ -24,7 +24,7 @@ __all__ = [
 @dataclass
 class Attachment:
     name: str
-    body: Union[str, bytes]
+    body: Union[str, bytes, None]
 
 
 @dataclass
@@ -38,6 +38,7 @@ class Message:
     sender_name: Optional[str] = None
     text: Optional[str] = None
     attachments: list[Attachment] = field(default_factory=list)
+    message_id: Optional[str] = None
 
     def __post_init__(self):
         self.command = None