Skip to content

Commit

Permalink
Merge pull request #71 from filipre/release-candidate-2024-07-17
Browse files Browse the repository at this point in the history
Release candidate 2024 07 17
  • Loading branch information
filipre authored Aug 5, 2024
2 parents 9ab791e + bc6c863 commit 3e142cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions signalbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import traceback
from typing import Optional, Union, List, Callable
import re
import uuid

from .api import SignalAPI, ReceiveMessagesError
from .command import Command
Expand Down Expand Up @@ -240,6 +241,9 @@ def _resolve_receiver(self, receiver: str) -> str:
if self._is_phone_number(receiver):
return receiver

if self._is_valid_uuid(receiver):
return receiver

if self._is_group_id(receiver):
return receiver

Expand All @@ -259,6 +263,13 @@ def _is_phone_number(self, phone_number: str) -> bool:
return False
return True

def _is_valid_uuid(self, receiver_uuid: str):
try:
uuid.UUID(str(receiver_uuid))
return True
except ValueError:
return False

def _is_group_id(self, group_id: str) -> bool:
"""Check if group_id has the right format, e.g.
Expand Down
10 changes: 10 additions & 0 deletions signalbot/message.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from enum import Enum
from typing import Optional


class MessageType(Enum):
Expand All @@ -11,6 +12,8 @@ class Message:
def __init__(
self,
source: str,
source_number: Optional[str],
source_uuid: str,
timestamp: int,
type: MessageType,
text: str,
Expand All @@ -22,6 +25,8 @@ def __init__(
):
# required
self.source = source
self.source_number = source_number
self.source_uuid = source_uuid
self.timestamp = timestamp
self.type = type
self.text = text
Expand Down Expand Up @@ -65,10 +70,13 @@ def parse(cls, raw_message: str):
# General attributes
try:
source = raw_message["envelope"]["source"]
source_uuid = raw_message["envelope"]["sourceUuid"]
timestamp = raw_message["envelope"]["timestamp"]
except Exception:
raise UnknownMessageFormatError

source_number = raw_message["envelope"].get("sourceNumber")

# Option 1: syncMessage
if "syncMessage" in raw_message["envelope"]:
type = MessageType.SYNC_MESSAGE
Expand Down Expand Up @@ -99,6 +107,8 @@ def parse(cls, raw_message: str):

return cls(
source,
source_number,
source_uuid,
timestamp,
type,
text,
Expand Down

0 comments on commit 3e142cd

Please sign in to comment.