Skip to content
This repository was archived by the owner on Jul 8, 2021. It is now read-only.

Commit

Permalink
Format phone numbers to be more human-readable
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Sep 22, 2019
1 parent 57810ea commit 54bb824
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RUN apk add --no-cache \

COPY . /opt/mautrix-twilio
WORKDIR /opt/mautrix-twilio
RUN pip3 install .
RUN pip3 install .[phonenumbers]

VOLUME /data

Expand Down
6 changes: 3 additions & 3 deletions example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ appservice:
# Bridge config
bridge:
# Localpart template of MXIDs for remote users.
# {userid} is replaced with the phone number of the user (international format without +).
# {userid} is replaced with the phone number of the user (plain/E.164 international format).
username_template: "twilio_whatsapp_{userid}"
# Displayname template for remote users.
# {displayname} is replaced with the phone number of the user (international format without +).
displayname_template: "+{displayname} (WhatsApp)"
# {displayname} is replaced with the phone number of the user (human-readable international format).
displayname_template: "{displayname} (WhatsApp)"

# The prefix for commands. Only required in non-management rooms.
command_prefix: "!tw"
Expand Down
3 changes: 2 additions & 1 deletion mautrix_twilio/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ async def _create_matrix_room(self) -> RoomID:
return self.mxid

async def handle_twilio_message(self, message: TwilioMessageEvent) -> None:
await self.create_matrix_room()
if not await self.create_matrix_room():
return
mxid = None

if message.media:
Expand Down
17 changes: 16 additions & 1 deletion mautrix_twilio/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
if TYPE_CHECKING:
from .context import Context

try:
import phonenumbers
except ImportError:
phonenumbers = None

config: Config


Expand All @@ -38,6 +43,7 @@ class Puppet(BasePuppet):
by_twid: Dict[TwilioUserID, 'Puppet'] = {}

twid: TwilioUserID
_formatted_number: Optional[str]

_db_instance: Optional[DBPuppet]

Expand All @@ -46,6 +52,7 @@ def __init__(self, twid: TwilioUserID, is_registered: bool = False,
super().__init__()
self.twid = twid
self.is_registered = is_registered
self._formatted_number = None
self._db_instance = db_instance
self.intent = self.az.intent.user(self.mxid)
self.log = self.log.getChild(self.twid)
Expand All @@ -55,13 +62,21 @@ def __init__(self, twid: TwilioUserID, is_registered: bool = False,
def phone_number(self) -> int:
return self.twid_template.parse(self.twid)

@property
def formatted_phone_number(self) -> str:
if not self._formatted_number:
parsed = phonenumbers.parse(f"+{self.phone_number}")
fmt = phonenumbers.PhoneNumberFormat.INTERNATIONAL
self._formatted_number = phonenumbers.format_number(parsed, fmt)
return self._formatted_number

@property
def mxid(self) -> UserID:
return UserID(self.mxid_template.format_full(str(self.phone_number)))

@property
def displayname(self) -> str:
return self.displayname_template.format_full(str(self.phone_number))
return self.displayname_template.format_full(self.formatted_phone_number)

@property
def db_instance(self) -> DBPuppet:
Expand Down
1 change: 1 addition & 0 deletions optional-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
phonenumbers
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"SQLAlchemy>=1.2,<2",
"alembic>=1,<2",
],
extras_require={
"phonenumbers": ["phonenumbers>=8,<9"],
},

classifiers=[
"Development Status :: 2 - Pre-Alpha",
Expand Down

0 comments on commit 54bb824

Please sign in to comment.