Skip to content

Commit

Permalink
Add update contacts method, fixes #79
Browse files Browse the repository at this point in the history
  • Loading branch information
Era Dorta committed Jan 3, 2025
1 parent 57d571c commit 70925b0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
37 changes: 36 additions & 1 deletion signalbot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import aiohttp
import websockets
from typing import Any
from typing import Any, Optional


class SignalAPI:
Expand Down Expand Up @@ -159,6 +159,34 @@ async def get_attachment(self, attachment_id: str) -> str:

return base64_string

async def update_contact(
self,
receiver: str,
expiration_in_seconds: Optional[int] = None,
name: Optional[str] = None,
):
uri = self._contacts_uri() + "/" + receiver
payload = {}
if expiration_in_seconds is not None:
payload["expiration_in_seconds"] = expiration_in_seconds

if name is not None:
payload["name"] = name

if payload == {}:
raise ContactUpdateError("Empty contact update")

try:
async with aiohttp.ClientSession() as session:
resp = await session.put(uri, json=payload)
resp.raise_for_status()
return resp
except (
aiohttp.ClientError,
aiohttp.http_exceptions.HttpProcessingError,
):
raise ContactUpdateError

def _attachment_rest_uri(self):
return f"http://{self.signal_service}/v1/attachments"

Expand All @@ -177,6 +205,9 @@ def _typing_indicator_uri(self):
def _groups_uri(self):
return f"http://{self.signal_service}/v1/groups/{self.phone_number}"

def _contacts_uri(self):
return f"http://{self.signal_service}/v1/contacts"


class ReceiveMessagesError(Exception):
pass
Expand Down Expand Up @@ -208,3 +239,7 @@ class GroupsError(Exception):

class GetAttachmentError(Exception):
pass


class ContactUpdateError(Exception):
pass
11 changes: 11 additions & 0 deletions signalbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ async def stop_typing(self, receiver: str):
receiver = self._resolve_receiver(receiver)
await self._signal.stop_typing(receiver)

async def update_contact(
self,
receiver: str,
expiration_in_seconds: Optional[int] = None,
name: Optional[str] = None,
) -> None:
receiver = self._resolve_receiver(receiver)
await self._signal.update_contact(
receiver, expiration_in_seconds=expiration_in_seconds, name=name
)

async def _detect_groups(self):
# reset group lookups to avoid stale data
self.groups = await self._signal.get_groups()
Expand Down

0 comments on commit 70925b0

Please sign in to comment.