您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

45 行
1.5 KiB

  1. # Copyright 2017 Vector Creations Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import phonenumbers
  15. from synapse.api.errors import SynapseError
  16. def phone_number_to_msisdn(country: str, number: str) -> str:
  17. """
  18. Takes an ISO-3166-1 2 letter country code and phone number and
  19. returns an msisdn representing the canonical version of that
  20. phone number.
  21. As an example, if `country` is "GB" and `number` is "7470674927", this
  22. function will return "447470674927".
  23. Args:
  24. country: ISO-3166-1 2 letter country code
  25. number: Phone number in a national or international format
  26. Returns:
  27. The canonical form of the phone number, as an msisdn.
  28. Raises:
  29. SynapseError if the number could not be parsed.
  30. """
  31. try:
  32. phoneNumber = phonenumbers.parse(number, country)
  33. except phonenumbers.NumberParseException:
  34. raise SynapseError(400, "Unable to parse phone number")
  35. return phonenumbers.format_number(phoneNumber, phonenumbers.PhoneNumberFormat.E164)[
  36. 1:
  37. ]