Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New accounts dont have phone numbers, _is_phone_number receives null and fails #48

Closed
ddevev opened this issue Mar 26, 2024 · 3 comments · Fixed by #65
Closed

New accounts dont have phone numbers, _is_phone_number receives null and fails #48

ddevev opened this issue Mar 26, 2024 · 3 comments · Fixed by #65

Comments

@ddevev
Copy link

ddevev commented Mar 26, 2024

After recent updates, account may not have phone number, sending message to it causes error " Error: Cannot resolve receiver."

INFO:root:[Raw Message] {"envelope":{"source":"d251b237-xxx-xxxx-xxxx-xxxxxxxxxxxx","sourceNumber":null,"sourceUuid":"d251b237-xxx-xxxx-xxxx-xxxxxxxxxxxx","sourceName":"<REDACTED>","sourceDevice":1,"timestamp":1711474552863,"dataMessage":{"timestamp":1711474552863,"message":"Test","expiresInSeconds":0,"viewOnce":false}},"account":"+184xxxxxxxx"}
INFO:root:[Bot] Consumer #1 got new job in 0.00005 seconds
ERROR:root:[PingCommand] Error: Cannot resolve receiver.

Probable fix: adjust function _resolve_receiver with:

def is_valid_uuid(val):
    try:
        uuid.UUID(str(val))
        return True
    except ValueError:
        return False

def _resolve_receiver(self, receiver: str) -> str:
    if is_valid_uuid(receiver):
        return receiver

    if self._is_phone_number(receiver):
        ...

But some other additional in changes bot.py might be needed

@Kariton
Copy link
Contributor

Kariton commented Jun 2, 2024

This does happen when the user is added through the new "username" feature - ether manually or by QR code.
Which is awesome for a "public" bot service.

have successfully tested this:

diff --git a/signalbot/bot.py b/signalbot/bot.py
index e8c8013..5011119 100644
--- a/signalbot/bot.py
+++ b/signalbot/bot.py
@@ -7,6 +7,7 @@ import traceback
 from typing import Optional, Union, List, Callable
 import re
 import phonenumbers
+import uuid

 from .api import SignalAPI, ReceiveMessagesError
 from .command import Command
@@ -238,6 +239,9 @@ class SignalBot:
         logging.info(f"[Bot] {len(self.groups)} groups detected")

     def _resolve_receiver(self, receiver: str) -> str:
+        if self._is_valid_uuid(receiver):
+            return receiver
+
         if self._is_phone_number(receiver):
             return receiver

@@ -251,6 +255,13 @@ class SignalBot:
         except Exception:
             raise SignalBotError(f"Cannot resolve receiver.")

+    def _is_valid_uuid(self, receiver_uuid: str):
+        try:
+            uuid.UUID(str(receiver_uuid))
+            return True
+        except ValueError:
+            return False
+
     def _is_phone_number(self, phone_number: str) -> bool:
         try:
             parsed_number = phonenumbers.parse(phone_number, None)

when a message is received via number the UUID is not used.
so maybe it is cleaner as second _resolve_receiver()

@filipre any chance to get this feature any time soon? <3

@filipre
Copy link
Owner

filipre commented Jun 2, 2024

Hey! Thanks for looking into this. Do you want to open a PR? Then I can create a pre-release for you to test, too.

@Kariton
Copy link
Contributor

Kariton commented Jun 2, 2024

I can create a PR.
but a pre-release will not help me because i depend on the redis.sadd() feature. ;)

but i can create the PR anyway.
and ill fix my quiet messy and mixed PR for redis.sadd() / redis.smember() as well as phonenumbers functionality.
then ull have it atomic in 3 different PRs.

maybe later this evening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants